usbd_std.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*!
  2. \file usbd_std.c
  3. \brief USB 2.0 standard handler driver
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "usbd_std.h"
  10. #include "usb_core.h"
  11. static usbd_status_enum usbd_standard_request (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  12. static usbd_status_enum usbd_device_class_request (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  13. static usbd_status_enum usbd_vendor_request (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  14. static void usbd_setup_request_parse(usb_core_handle_struct *pudev, usb_device_req_struct *req);
  15. static void usbd_getdescriptor (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  16. static void usbd_setaddress (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  17. static void usbd_setconfig (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  18. static void usbd_getconfig (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  19. static void usbd_getstatus (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  20. static void usbd_setfeature (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  21. static void usbd_clrfeature (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  22. static void usbd_reserved (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  23. static void usbd_setdescriptor (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  24. static void usbd_getinterface (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  25. static void usbd_setinterface (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  26. static void usbd_synchframe (usb_core_handle_struct *pudev, usb_device_req_struct *req);
  27. static uint8_t* usbd_device_descriptor_get (usb_core_handle_struct *pudev, uint8_t index, uint16_t *pLen);
  28. static uint8_t* usbd_configuration_descriptor_get (usb_core_handle_struct *pudev, uint8_t index, uint16_t *pLen);
  29. static uint8_t* usbd_string_descriptor_get (usb_core_handle_struct *pudev, uint8_t index, uint16_t *pLen);
  30. static void (*StandardDeviceRequest[])(usb_core_handle_struct *pudev, usb_device_req_struct *req) =
  31. {
  32. usbd_getstatus,
  33. usbd_clrfeature,
  34. usbd_reserved,
  35. usbd_setfeature,
  36. usbd_reserved,
  37. usbd_setaddress,
  38. usbd_getdescriptor,
  39. usbd_setdescriptor,
  40. usbd_getconfig,
  41. usbd_setconfig,
  42. usbd_getinterface,
  43. usbd_setinterface,
  44. usbd_synchframe,
  45. };
  46. /* get standard descriptor handler */
  47. static uint8_t* (*standard_descriptor_get[])(usb_core_handle_struct *pudev, uint8_t index, uint16_t *pLen) =
  48. {
  49. usbd_device_descriptor_get,
  50. usbd_configuration_descriptor_get,
  51. usbd_string_descriptor_get
  52. };
  53. /*!
  54. \brief USB setup stage processing
  55. \param[in] pudev: pointer to USB device instance
  56. \param[out] none
  57. \retval USB device operation status
  58. */
  59. usbd_status_enum usbd_setup_transaction(usb_core_handle_struct *pudev)
  60. {
  61. usb_device_req_struct req;
  62. usbd_setup_request_parse(pudev, &req);
  63. switch (req.bmRequestType & USB_REQ_MASK) {
  64. /* standard device request */
  65. case USB_STANDARD_REQ:
  66. usbd_standard_request(pudev, &req);
  67. break;
  68. /* device class request */
  69. case USB_CLASS_REQ:
  70. usbd_device_class_request(pudev, &req);
  71. break;
  72. /* vendor defined request */
  73. case USB_VENDOR_REQ:
  74. usbd_vendor_request(pudev, &req);
  75. break;
  76. default:
  77. usbd_ep_stall(pudev, req.bmRequestType & 0x80U);
  78. break;
  79. }
  80. return USBD_OK;
  81. }
  82. /*!
  83. \brief data out stage processing
  84. \param[in] pudev: pointer to USB device instance
  85. \param[in] ep_id: endpoint identifier(0..7)
  86. \param[out] none
  87. \retval USB device operation status
  88. */
  89. usbd_status_enum usbd_out_transaction (usb_core_handle_struct *pudev, uint8_t endp_num)
  90. {
  91. usb_ep_struct *ep;
  92. if (0U == endp_num) {
  93. ep = &pudev->dev.out_ep[0];
  94. if (USB_CTRL_DATA_OUT == pudev->dev.ctl_status) {
  95. if (pudev->dev.remain_len > ep->endp_mps) {
  96. pudev->dev.remain_len -= ep->endp_mps;
  97. if (1U == pudev->cfg.dma_enable) {
  98. /* update buffer location */
  99. ep->xfer_buff += ep->endp_mps;
  100. }
  101. usbd_ep_rx (pudev,
  102. 0U,
  103. ep->xfer_buff,
  104. (uint16_t)USB_MIN(pudev->dev.remain_len, ep->endp_mps));
  105. } else {
  106. if (USB_STATUS_CONFIGURED == pudev->dev.status) {
  107. pudev->dev.class_data_handler(pudev, USBD_RX, 0U);
  108. }
  109. usbd_ctlstatus_tx(pudev);
  110. }
  111. }
  112. } else if (USB_STATUS_CONFIGURED == pudev->dev.status) {
  113. pudev->dev.class_data_handler(pudev, USBD_RX, endp_num);
  114. } else {
  115. /* no operation */
  116. }
  117. return USBD_OK;
  118. }
  119. /*!
  120. \brief data in stage processing
  121. \param[in] pudev: pointer to USB device instance
  122. \param[in] ep_id: endpoint identifier(0..7)
  123. \param[out] none
  124. \retval USB device operation status
  125. */
  126. usbd_status_enum usbd_in_transaction (usb_core_handle_struct *pudev, uint8_t endp_num)
  127. {
  128. usb_ep_struct *ep;
  129. if (0U == endp_num) {
  130. ep = &pudev->dev.in_ep[0];
  131. if (USB_CTRL_DATA_IN == pudev->dev.ctl_status) {
  132. if (pudev->dev.remain_len > ep->endp_mps) {
  133. pudev->dev.remain_len -= ep->endp_mps;
  134. if (1U == pudev->cfg.dma_enable) {
  135. /* update buffer location */
  136. ep->xfer_buff += ep->endp_mps;
  137. }
  138. usbd_ep_tx (pudev, 0U, ep->xfer_buff, pudev->dev.remain_len);
  139. } else {
  140. /* last packet is MPS multiple, so send ZLP packet */
  141. if ((pudev->dev.sum_len % ep->endp_mps == 0U) &&
  142. (pudev->dev.sum_len >= ep->endp_mps) &&
  143. (pudev->dev.sum_len < pudev->dev.ctl_len)) {
  144. usbd_ep_tx (pudev, 0U, NULL, 0U);
  145. pudev->dev.ctl_len = 0U;
  146. } else {
  147. if (USB_STATUS_CONFIGURED == pudev->dev.status) {
  148. pudev->dev.class_data_handler(pudev, USBD_TX, 0U);
  149. }
  150. usbd_ctlstatus_rx(pudev);
  151. }
  152. }
  153. }
  154. } else if (USB_STATUS_CONFIGURED == pudev->dev.status) {
  155. pudev->dev.class_data_handler(pudev, USBD_TX, endp_num);
  156. } else {
  157. /* no operation */
  158. }
  159. return USBD_OK;
  160. }
  161. /*!
  162. \brief handle USB standard device request
  163. \param[in] pudev: pointer to USB device instance
  164. \param[in] req: pointer to USB device request
  165. \param[out] none
  166. \retval USB device operation status
  167. */
  168. static usbd_status_enum usbd_standard_request (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  169. {
  170. /* call device request handle function */
  171. (*StandardDeviceRequest[req->bRequest])(pudev, req);
  172. return USBD_OK;
  173. }
  174. /*!
  175. \brief handle USB device class request
  176. \param[in] pudev: pointer to USB device instance
  177. \param[in] req: pointer to USB device class request
  178. \param[out] none
  179. \retval USB device operation status
  180. */
  181. static usbd_status_enum usbd_device_class_request (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  182. {
  183. usbd_status_enum ret = USBD_OK;
  184. switch (pudev->dev.status) {
  185. case USB_STATUS_CONFIGURED:
  186. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  187. ret = (usbd_status_enum)(pudev->dev.class_req_handler(pudev, req));
  188. if ((0U == req->wLength) && (USBD_OK == ret)) {
  189. /* no data stage */
  190. usbd_ctlstatus_tx(pudev);
  191. }
  192. } else {
  193. usbd_enum_error(pudev, req);
  194. }
  195. break;
  196. default:
  197. usbd_enum_error(pudev, req);
  198. break;
  199. }
  200. return ret;
  201. }
  202. /*!
  203. \brief handle USB vendor request
  204. \param[in] pudev: pointer to USB device instance
  205. \param[in] req: pointer to USB vendor request
  206. \param[out] none
  207. \retval USB device operation status
  208. */
  209. static usbd_status_enum usbd_vendor_request (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  210. {
  211. /* added by user... */
  212. return USBD_OK;
  213. }
  214. /*!
  215. \brief no operation, just for reserved
  216. \param[in] pudev: pointer to USB device instance
  217. \param[in] req: pointer to USB device request
  218. \param[out] none
  219. \retval none
  220. */
  221. static void usbd_reserved (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  222. {
  223. /* no operation... */
  224. }
  225. /*!
  226. \brief get the device descriptor
  227. \brief[in] index: no use
  228. \param[in] none
  229. \param[out] pLen: data length pointer
  230. \retval descriptor buffer pointer
  231. */
  232. static uint8_t* usbd_device_descriptor_get (usb_core_handle_struct *pudev, uint8_t index, uint16_t *pLen)
  233. {
  234. *pLen = pudev->dev.dev_desc[0];
  235. return pudev->dev.dev_desc;
  236. }
  237. /*!
  238. \brief get the configuration descriptor
  239. \brief[in] index: no use
  240. \param[in] none
  241. \param[out] pLen: data length pointer
  242. \retval descriptor buffer pointer
  243. */
  244. static uint8_t* usbd_configuration_descriptor_get (usb_core_handle_struct *pudev, uint8_t index, uint16_t *pLen)
  245. {
  246. *pLen = pudev->dev.config_desc[2];
  247. return pudev->dev.config_desc;
  248. }
  249. /*!
  250. \brief get string descriptor
  251. \param[in] index: string descriptor index
  252. \param[in] pLen: pointer to string length
  253. \param[out] none
  254. \retval none
  255. */
  256. static uint8_t* usbd_string_descriptor_get (usb_core_handle_struct *pudev, uint8_t index, uint16_t *pLen)
  257. {
  258. uint8_t *desc = pudev->dev.strings[index];
  259. *pLen = desc[0];
  260. return desc;
  261. }
  262. /*!
  263. \brief handle Get_Status request
  264. \param[in] pudev: pointer to USB device instance
  265. \param[in] req: pointer to USB device request
  266. \param[out] none
  267. \retval none
  268. */
  269. static void usbd_getstatus (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  270. {
  271. }
  272. /*!
  273. \brief handle USB Clear_Feature request
  274. \param[in] pudev: pointer to USB device instance
  275. \param[in] req: USB device request
  276. \param[out] none
  277. \retval none
  278. */
  279. static void usbd_clrfeature (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  280. {
  281. uint8_t ep_addr = 0U;
  282. switch (req->bmRequestType & USB_REQTYPE_MASK) {
  283. case USB_REQTYPE_DEVICE:
  284. switch (pudev->dev.status) {
  285. case USB_STATUS_ADDRESSED:
  286. case USB_STATUS_CONFIGURED:
  287. if (USB_FEATURE_REMOTE_WAKEUP == req->wValue) {
  288. pudev->dev.remote_wakeup = 0U;
  289. pudev->dev.class_req_handler(pudev, req);
  290. usbd_ctlstatus_tx(pudev);
  291. }
  292. break;
  293. default:
  294. usbd_enum_error(pudev, req);
  295. break;
  296. }
  297. break;
  298. case USB_REQTYPE_INTERFACE:
  299. switch (pudev->dev.status) {
  300. case USB_STATUS_ADDRESSED:
  301. usbd_enum_error(pudev, req);
  302. break;
  303. case USB_STATUS_CONFIGURED:
  304. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  305. /* no operation */
  306. } else {
  307. usbd_enum_error(pudev, req);
  308. }
  309. break;
  310. default:
  311. break;
  312. }
  313. break;
  314. case USB_REQTYPE_ENDPOINT:
  315. ep_addr = LOWBYTE(req->wIndex);
  316. switch (pudev->dev.status) {
  317. case USB_STATUS_ADDRESSED:
  318. if (IS_NOT_EP0(ep_addr)) {
  319. usbd_ep_stall(pudev, ep_addr);
  320. }
  321. break;
  322. case USB_STATUS_CONFIGURED:
  323. if (USB_FEATURE_ENDP_HALT == req->wValue) {
  324. if (IS_NOT_EP0(ep_addr)) {
  325. usbd_ep_clear_stall(pudev, ep_addr);
  326. pudev->dev.class_req_handler(pudev, req);
  327. }
  328. }
  329. usbd_ctlstatus_tx(pudev);
  330. break;
  331. default:
  332. break;
  333. }
  334. break;
  335. default:
  336. usbd_enum_error(pudev, req);
  337. break;
  338. }
  339. }
  340. /*!
  341. \brief handle USB Set_Feature request
  342. \param[in] pudev: pointer to USB device instance
  343. \param[in] req: pointer to USB device request
  344. \param[out] none
  345. \retval none
  346. */
  347. static void usbd_setfeature (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  348. {
  349. uint8_t ep_addr = 0U;
  350. __IO uint32_t DctlrStatus;
  351. switch (req->bmRequestType & USB_REQ_MASK) {
  352. case USB_REQTYPE_DEVICE:
  353. switch (pudev->dev.status) {
  354. case USB_STATUS_ADDRESSED:
  355. case USB_STATUS_CONFIGURED:
  356. if (USB_FEATURE_REMOTE_WAKEUP == req->wValue) {
  357. pudev->dev.remote_wakeup = 1U;
  358. pudev->dev.class_req_handler(pudev, req);
  359. usbd_ctlstatus_tx(pudev);
  360. } else if ((req->wValue == USB_FEATURE_TEST_MODE) &&
  361. (0U == (req->wIndex & 0xFFU))) {
  362. DctlrStatus = USB_DCTL;
  363. usbd_ctlstatus_tx(pudev);
  364. } else {
  365. /* no operation */
  366. }
  367. break;
  368. default:
  369. break;
  370. }
  371. break;
  372. case USB_REQTYPE_INTERFACE:
  373. switch (pudev->dev.status) {
  374. case USB_STATUS_ADDRESSED:
  375. usbd_enum_error(pudev, req);
  376. break;
  377. case USB_STATUS_CONFIGURED:
  378. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  379. /* no operation */
  380. } else {
  381. usbd_enum_error(pudev, req);
  382. }
  383. break;
  384. default:
  385. break;
  386. }
  387. break;
  388. case USB_REQTYPE_ENDPOINT:
  389. switch (pudev->dev.status) {
  390. case USB_STATUS_ADDRESSED:
  391. if (IS_NOT_EP0(ep_addr)) {
  392. usbd_ep_stall(pudev, ep_addr);
  393. }
  394. break;
  395. case USB_STATUS_CONFIGURED:
  396. if (USB_FEATURE_ENDP_HALT == req->wValue) {
  397. if (IS_NOT_EP0(ep_addr)) {
  398. usbd_ep_stall(pudev, ep_addr);
  399. }
  400. }
  401. pudev->dev.class_req_handler(pudev, req);
  402. usbd_ctlstatus_tx(pudev);
  403. break;
  404. default:
  405. break;
  406. }
  407. break;
  408. default:
  409. usbd_enum_error(pudev, req);
  410. break;
  411. }
  412. }
  413. /*!
  414. \brief handle USB Set_Address request
  415. \param[in] pudev: pointer to USB device instance
  416. \param[in] req: pointer to USB device request
  417. \param[out] none
  418. \retval none
  419. */
  420. static void usbd_setaddress (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  421. {
  422. uint8_t DevAddr;
  423. if ((0U == req->wIndex) && (0U == req->wLength)) {
  424. DevAddr = (uint8_t)(req->wValue) & 0x7FU;
  425. if (USB_STATUS_CONFIGURED == pudev->dev.status) {
  426. usbd_enum_error(pudev, req);
  427. } else {
  428. USB_SET_DEVADDR((uint32_t)DevAddr);
  429. usbd_ctlstatus_tx(pudev);
  430. if (0U != DevAddr) {
  431. pudev->dev.status = USB_STATUS_ADDRESSED;
  432. } else {
  433. pudev->dev.status = USB_STATUS_DEFAULT;
  434. }
  435. }
  436. } else {
  437. usbd_enum_error(pudev, req);
  438. }
  439. }
  440. /*!
  441. \brief handle USB Get_Descriptor request
  442. \param[in] pudev: pointer to USB device instance
  443. \param[in] req: pointer to USB device request
  444. \param[out] none
  445. \retval none
  446. */
  447. static void usbd_getdescriptor (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  448. {
  449. if (USB_REQTYPE_DEVICE == (req->bmRequestType & USB_REQTYPE_MASK)) {
  450. uint8_t desc_index = (uint8_t)(req->wValue >> 8U);
  451. if (desc_index <= 0x03U) {
  452. uint16_t len;
  453. uint8_t *pbuf;
  454. /* call corresponding descriptor get function */
  455. pbuf = standard_descriptor_get[desc_index - 1U](pudev, (uint8_t)(req->wValue) & 0xFFU, &len);
  456. if ((0U != len) && (0U != req->wLength)) {
  457. len = USB_MIN(len, req->wLength);
  458. if ((1U == desc_index) && (64U == req->wLength)) {
  459. len = 8U;
  460. }
  461. usbd_ctltx(pudev, pbuf, len);
  462. }
  463. } else {
  464. usbd_enum_error(pudev, req);
  465. }
  466. } else if (USB_REQTYPE_INTERFACE == (req->bmRequestType & USB_REQTYPE_MASK)) {
  467. pudev->dev.class_req_handler(pudev, req);
  468. } else {
  469. /* no operation */
  470. }
  471. }
  472. /*!
  473. \brief handle USB Set_Descriptor request
  474. \param[in] pudev: pointer to USB device instance
  475. \param[in] req: pointer to USB device request
  476. \param[out] none
  477. \retval none
  478. */
  479. static void usbd_setdescriptor (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  480. {
  481. /* no handle... */
  482. }
  483. /*!
  484. \brief handle USB Get_Configuration request
  485. \param[in] pudev: pointer to USB device instance
  486. \param[in] req: pointer to USB device request
  487. \param[out] none
  488. \retval none
  489. */
  490. static void usbd_getconfig (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  491. {
  492. uint32_t USBD_default_config = 0U;
  493. if (1U != req->wLength) {
  494. usbd_enum_error(pudev, req);
  495. } else {
  496. switch (pudev->dev.status) {
  497. case USB_STATUS_ADDRESSED:
  498. usbd_ctltx(pudev, (uint8_t *)&USBD_default_config, 1U);
  499. break;
  500. case USB_STATUS_CONFIGURED:
  501. usbd_ctltx(pudev, &pudev->dev.config_num, 1U);
  502. break;
  503. default:
  504. usbd_enum_error(pudev, req);
  505. break;
  506. }
  507. }
  508. }
  509. /*!
  510. \brief handle USB Set_Configuration request
  511. \param[in] pudev: pointer to USB device instance
  512. \param[in] req: pointer to USB device request
  513. \param[out] none
  514. \retval none
  515. */
  516. static void usbd_setconfig (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  517. {
  518. static uint8_t cfgidx;
  519. cfgidx = (uint8_t)(req->wValue);
  520. if (cfgidx > USBD_CFG_MAX_NUM) {
  521. usbd_enum_error(pudev, req);
  522. } else {
  523. switch (pudev->dev.status) {
  524. case USB_STATUS_ADDRESSED:
  525. if (cfgidx) {
  526. pudev->dev.config_num = cfgidx;
  527. pudev->dev.status = USB_STATUS_CONFIGURED;
  528. pudev->dev.class_init(pudev, cfgidx);
  529. }
  530. usbd_ctlstatus_tx(pudev);
  531. break;
  532. case USB_STATUS_CONFIGURED:
  533. if (0U == cfgidx) {
  534. pudev->dev.status = USB_STATUS_ADDRESSED;
  535. pudev->dev.config_num = cfgidx;
  536. pudev->dev.class_deinit(pudev, cfgidx);
  537. } else if (cfgidx != pudev->dev.config_num) {
  538. /* clear old configuration */
  539. pudev->dev.class_deinit(pudev, pudev->dev.config_num);
  540. /* set new configuration */
  541. pudev->dev.config_num = cfgidx;
  542. pudev->dev.class_init(pudev, cfgidx);
  543. } else {
  544. /* no operation */
  545. }
  546. usbd_ctlstatus_tx(pudev);
  547. break;
  548. default:
  549. usbd_enum_error(pudev, req);
  550. break;
  551. }
  552. }
  553. }
  554. /*!
  555. \brief handle USB Get_Interface request
  556. \param[in] pudev: pointer to USB device instance
  557. \param[in] req: pointer to USB device request
  558. \param[out] none
  559. \retval none
  560. */
  561. static void usbd_getinterface (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  562. {
  563. pudev->dev.class_req_handler(pudev, req);
  564. }
  565. /*!
  566. \brief handle USB Set_Interface request
  567. \param[in] pudev: pointer to USB device instance
  568. \param[in] req: pointer to USB device request
  569. \param[out] none
  570. \retval none
  571. */
  572. static void usbd_setinterface (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  573. {
  574. pudev->dev.class_req_handler(pudev, req);
  575. }
  576. /*!
  577. \brief handle USB SynchFrame request
  578. \param[in] pudev: pointer to USB device instance
  579. \param[in] req: pointer to USB device request
  580. \param[out] none
  581. \retval none
  582. */
  583. static void usbd_synchframe (usb_core_handle_struct *pudev, usb_device_req_struct *req)
  584. {
  585. /* no handle... */
  586. }
  587. /*!
  588. \brief decode setup data packet
  589. \param[in] pudev: pointer to USB device instance
  590. \param[in] req: pointer to USB device request
  591. \param[out] none
  592. \retval none
  593. */
  594. static void usbd_setup_request_parse(usb_core_handle_struct *pudev, usb_device_req_struct *req)
  595. {
  596. uint8_t *psetup = pudev->dev.setup_packet;
  597. req->bmRequestType = *psetup;
  598. req->bRequest = *(uint8_t *)(psetup + 1U);
  599. req->wValue = SWAPBYTE (psetup + 2U);
  600. req->wIndex = SWAPBYTE (psetup + 4U);
  601. req->wLength = SWAPBYTE (psetup + 6U);
  602. pudev->dev.ctl_len = req->wLength;
  603. }
  604. /*!
  605. \brief handle USB low level error event
  606. \param[in] pudev: pointer to USB device instance
  607. \param[in] req: pointer to USB device request
  608. \param[out] none
  609. \retval none
  610. */
  611. void usbd_enum_error(usb_core_handle_struct *pudev, usb_device_req_struct *req)
  612. {
  613. usbd_ep_stall(pudev, 0x80U);
  614. usbd_ep_stall(pudev, 0x00U);
  615. usb_ep0_startout(pudev);
  616. }