usbd_std.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*!
  2. \file usbd_std.c
  3. \brief USB device stand routines
  4. \note about USB standard, please refer to the USB2.0 protocol
  5. */
  6. /*
  7. Copyright (C) 2017 GigaDevice
  8. 2017-02-10, V1.0.0, firmware for GD32F30x
  9. */
  10. #include "usbd_std.h"
  11. uint8_t g_device_address = 0U;
  12. /* USB enumeration handle functions */
  13. static void usbd_getdescriptor (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  14. static void usbd_setaddress (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  15. static void usbd_setconfiguration (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  16. static void usbd_getconfiguration (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  17. static void usbd_getstatus (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  18. static void usbd_setfeature (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  19. static void usbd_clearfeature (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  20. static void usbd_reserved (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  21. static void usbd_setdescriptor (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  22. static void usbd_getinterface (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  23. static void usbd_setinterface (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  24. static void usbd_synchframe (usbd_core_handle_struct *pudev, usb_device_req_struct *req);
  25. static uint8_t* usbd_device_descriptor_get (usbd_core_handle_struct *pudev,
  26. uint8_t index,
  27. uint16_t *pLen);
  28. static uint8_t* usbd_configuration_descriptor_get (usbd_core_handle_struct *pudev,
  29. uint8_t index,
  30. uint16_t *pLen);
  31. static uint8_t* usbd_string_descriptor_get (usbd_core_handle_struct *pudev,
  32. uint8_t index,
  33. uint16_t *pLen);
  34. #ifdef LPM_ENABLED
  35. static uint8_t* usbd_bos_descriptor_get (usbd_core_handle_struct *pudev, uint16_t *pLen);
  36. #endif /* LPM_ENABLED */
  37. /* standard device request handler */
  38. static void (*standard_device_request[])(usbd_core_handle_struct *pudev, usb_device_req_struct *req) =
  39. {
  40. usbd_getstatus,
  41. usbd_clearfeature,
  42. usbd_reserved,
  43. usbd_setfeature,
  44. usbd_reserved,
  45. usbd_setaddress,
  46. usbd_getdescriptor,
  47. usbd_setdescriptor,
  48. usbd_getconfiguration,
  49. usbd_setconfiguration,
  50. usbd_getinterface,
  51. usbd_setinterface,
  52. usbd_synchframe,
  53. };
  54. /* get standard descriptor handler */
  55. static uint8_t* (*standard_descriptor_get[])(usbd_core_handle_struct *pudev, uint8_t index, uint16_t *pLen) =
  56. {
  57. usbd_device_descriptor_get,
  58. usbd_configuration_descriptor_get,
  59. usbd_string_descriptor_get
  60. };
  61. /*!
  62. \brief USB setup stage processing
  63. \param[in] pudev: pointer to USB device instance
  64. \param[out] none
  65. \retval USB device operation status
  66. */
  67. uint8_t usbd_setup_transaction (usbd_core_handle_struct *pudev)
  68. {
  69. usb_device_req_struct req;
  70. usbd_setup_request_parse (pudev, &req);
  71. switch (req.bmRequestType & USB_REQ_MASK) {
  72. /* standard device request */
  73. case USB_STANDARD_REQ:
  74. usbd_standard_request(pudev, &req);
  75. break;
  76. /* device class request */
  77. case USB_CLASS_REQ:
  78. usbd_device_class_request(pudev, &req);
  79. break;
  80. /* vendor defined request */
  81. case USB_VENDOR_REQ:
  82. usbd_vendor_request(pudev, &req);
  83. break;
  84. default:
  85. usbd_ep_stall(pudev, 0x00U);
  86. break;
  87. }
  88. return USBD_OK;
  89. }
  90. /*!
  91. \brief data out stage processing
  92. \param[in] pudev: pointer to USB device instance
  93. \param[in] ep_num: endpoint identifier(0..7)
  94. \param[out] none
  95. \retval USB device operation status
  96. */
  97. uint8_t usbd_out_transaction (usbd_core_handle_struct *pudev, uint8_t ep_num)
  98. {
  99. usb_ep_struct *ep = &pudev->out_ep[ep_num];
  100. if (0U == ep_num) {
  101. if (0U != pudev->ctl_count) {
  102. if (ep->trs_len > ep->maxpacket) {
  103. /* one data packet has been received, update trs_len */
  104. ep->trs_len -= ep->maxpacket;
  105. /* continue to receive remain data */
  106. usbd_ep_rx(pudev, EP0_OUT, ep->trs_buf, (uint16_t)ep->trs_len);
  107. } else {
  108. if (USBD_CONFIGURED == pudev->status) {
  109. /* device class handle */
  110. pudev->class_data_handler(pudev, USBD_RX, EP0);
  111. }
  112. /* enter the control transaction status stage */
  113. USBD_CONTRL_STATUS_TX();
  114. pudev->ctl_count = 0U;
  115. }
  116. } else {
  117. /* clear endpoint status_out status */
  118. USBD_STATUS_OUT_CLEAR(EP0);
  119. }
  120. } else {
  121. if (USBD_CONFIGURED == pudev->status) {
  122. pudev->class_data_handler(pudev, USBD_RX, ep_num);
  123. }
  124. }
  125. return USBD_OK;
  126. }
  127. /*!
  128. \brief data in stage processing
  129. \param[in] pudev: pointer to USB device instance
  130. \param[in] ep_num: endpoint identifier(0..7)
  131. \param[out] none
  132. \retval USB device operation status
  133. */
  134. uint8_t usbd_in_transaction (usbd_core_handle_struct *pudev, uint8_t ep_num)
  135. {
  136. usb_ep_struct *ep = &pudev->in_ep[ep_num];
  137. if (0U == ep_num) {
  138. if (0U != pudev->ctl_count) {
  139. if (ep->trs_len > ep->maxpacket) {
  140. /* one data packet has been transmited, update trs_len */
  141. ep->trs_len -= ep->maxpacket;
  142. /* continue to transmit remain data */
  143. usbd_ep_tx (pudev, EP0_IN, ep->trs_buf, (uint16_t)ep->trs_len);
  144. // usbd_ep_rx (pudev, 0, NULL, 0);
  145. } else {
  146. #ifndef USB_DFU
  147. /* transmit length is maxpacket multiple, so send zero length packet */
  148. if ((0U == (ep->trs_len % ep->maxpacket)) && (ep->trs_len < pudev->ctl_count)) {
  149. usbd_ep_tx (pudev, EP0_IN, NULL, 0U);
  150. pudev->ctl_count = 0U;
  151. // usbd_ep_rx (pudev, 0, NULL, 0);
  152. } else
  153. #endif /* USB_DFU */
  154. {
  155. ep->trs_len = 0U;
  156. if (USBD_CONFIGURED == pudev->status) {
  157. pudev->class_data_handler(pudev, USBD_TX, EP0);
  158. }
  159. USBD_CONTRL_STATUS_RX();
  160. pudev->ctl_count = 0U;
  161. }
  162. }
  163. } else {
  164. if (0U != g_device_address) {
  165. USBD_REG_SET(USBD_DADDR, DADDR_USBEN | g_device_address);
  166. g_device_address = 0U;
  167. }
  168. }
  169. } else {
  170. ep->trs_len -= ep->trs_count;
  171. if (0U == ep->trs_len) {
  172. if (USBD_CONFIGURED == pudev->status) {
  173. pudev->class_data_handler(pudev, USBD_TX, ep_num);
  174. }
  175. } else {
  176. usbd_ep_tx(pudev, ep_num, ep->trs_buf, (uint16_t)ep->trs_len);
  177. }
  178. }
  179. return USBD_OK;
  180. }
  181. /*!
  182. \brief handle USB standard device request
  183. \param[in] pudev: pointer to USB device instance
  184. \param[in] req: pointer to USB device request
  185. \param[out] none
  186. \retval USB device operation status
  187. */
  188. uint8_t usbd_standard_request (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  189. {
  190. /* call device request handle function */
  191. (*standard_device_request[req->bRequest])(pudev, req);
  192. return USBD_OK;
  193. }
  194. /*!
  195. \brief handle USB device class request
  196. \param[in] pudev: pointer to USB device instance
  197. \param[in] req: pointer to USB device class request
  198. \param[out] none
  199. \retval USB device operation status
  200. */
  201. uint8_t usbd_device_class_request (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  202. {
  203. usbd_status_enum ret;
  204. switch (pudev->status) {
  205. case USBD_CONFIGURED:
  206. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  207. /* call device class handle function */
  208. ret = (usbd_status_enum)(pudev->class_req_handler(pudev, req));
  209. if ((0U == req->wLength) && (USBD_OK == ret)) {
  210. /* no data stage */
  211. USBD_CONTRL_STATUS_TX();
  212. }
  213. } else {
  214. usbd_enum_error(pudev, req);
  215. }
  216. break;
  217. default:
  218. usbd_enum_error(pudev, req);
  219. break;
  220. }
  221. return USBD_OK;
  222. }
  223. /*!
  224. \brief handle USB vendor request
  225. \param[in] pudev: pointer to USB device instance
  226. \param[in] req: pointer to USB vendor request
  227. \param[out] none
  228. \retval USB device operation status
  229. */
  230. uint8_t usbd_vendor_request (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  231. {
  232. /* added by user */
  233. return USBD_OK;
  234. }
  235. /*!
  236. \brief no operation, just for reserved
  237. \param[in] pudev: pointer to USB device instance
  238. \param[in] req: pointer to USB device request
  239. \param[out] none
  240. \retval none
  241. */
  242. static void usbd_reserved (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  243. {
  244. /* no operation */
  245. }
  246. #ifdef LPM_ENABLED
  247. static uint8_t* usbd_bos_descriptor_get (usbd_core_handle_struct *pudev, uint16_t *pLen)
  248. {
  249. *pLen = pudev->bos_desc[2] | ((uint16_t)pudev->bos_desc[3] << 8);
  250. return pudev->bos_desc;
  251. }
  252. #endif /* LPM_ENABLED */
  253. /*!
  254. \brief get the device descriptor
  255. \param[in] pudev: pointer to USB device instance
  256. \param[in] index: no use
  257. \param[out] pLen: data length pointer
  258. \retval descriptor buffer pointer
  259. */
  260. static uint8_t* usbd_device_descriptor_get (usbd_core_handle_struct *pudev, uint8_t index, uint16_t *pLen)
  261. {
  262. *pLen = pudev->dev_desc[0];
  263. return pudev->dev_desc;
  264. }
  265. /*!
  266. \brief get the configuration descriptor
  267. \brief[in] pudev: pointer to USB device instance
  268. \brief[in] index: no use
  269. \param[out] pLen: data length pointer
  270. \retval descriptor buffer pointer
  271. */
  272. static uint8_t* usbd_configuration_descriptor_get (usbd_core_handle_struct *pudev, uint8_t index, uint16_t *pLen)
  273. {
  274. *pLen = pudev->config_desc[2];
  275. return pudev->config_desc;
  276. }
  277. /*!
  278. \brief get string descriptor
  279. \param[in] pudev: pointer to USB device instance
  280. \param[in] index: string descriptor index
  281. \param[out] pLen: pointer to string length
  282. \retval none
  283. */
  284. static uint8_t* usbd_string_descriptor_get (usbd_core_handle_struct *pudev, uint8_t index, uint16_t *pLen)
  285. {
  286. uint8_t *desc = pudev->strings[index];
  287. *pLen = desc[0];
  288. return desc;
  289. }
  290. /*!
  291. \brief handle Get_Status request
  292. \param[in] pudev: pointer to USB device instance
  293. \param[in] req: pointer to USB device request
  294. \param[out] none
  295. \retval none
  296. */
  297. static void usbd_getstatus (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  298. {
  299. uint8_t ep_addr;
  300. uint16_t config_status = 0x0000U;
  301. uint16_t endp_status = 0x0000U;
  302. switch(req->bmRequestType & USB_REQ_RECIPIENT_MASK) {
  303. case USB_REQTYPE_DEVICE:
  304. switch (pudev->status) {
  305. case USBD_ADDRESSED:
  306. case USBD_CONFIGURED:
  307. #ifdef USBD_SELF_POWERED
  308. config_status = USB_STATUS_SELF_POWERED;
  309. #endif /* USBD_SELF_POWERED */
  310. if (pudev->remote_wakeup) {
  311. config_status |= USB_STATUS_REMOTE_WAKEUP;
  312. }
  313. usbd_ep_tx(pudev, EP0_IN, (uint8_t *)&config_status, 2U);
  314. break;
  315. default:
  316. break;
  317. }
  318. break;
  319. case USB_REQTYPE_INTERFACE:
  320. switch (pudev->status) {
  321. case USBD_ADDRESSED:
  322. usbd_enum_error(pudev, req);
  323. break;
  324. case USBD_CONFIGURED:
  325. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  326. usbd_ep_tx(pudev, EP0_IN, (uint8_t *)&config_status, 2U);
  327. } else {
  328. usbd_enum_error(pudev, req);
  329. }
  330. break;
  331. default:
  332. break;
  333. }
  334. break;
  335. case USB_REQTYPE_ENDPOINT:
  336. /* get enndpoint address */
  337. ep_addr = LOWBYTE(req->wIndex);
  338. switch (pudev->status) {
  339. case USBD_ADDRESSED:
  340. if (IS_NOT_EP0(ep_addr)) {
  341. usbd_enum_error(pudev, req);
  342. }
  343. break;
  344. case USBD_CONFIGURED:
  345. if ((ep_addr & 0x80U) == 0x80U) {
  346. if(pudev->in_ep[ep_addr & 0x7FU].stall) {
  347. endp_status = 0x0001U;
  348. }
  349. } else {
  350. if (pudev->out_ep[ep_addr].stall) {
  351. endp_status = 0x0001U;
  352. }
  353. }
  354. usbd_ep_tx(pudev, EP0_IN, (uint8_t *)&endp_status, 2U);
  355. break;
  356. default:
  357. break;
  358. }
  359. break;
  360. default:
  361. usbd_enum_error(pudev, req);
  362. break;
  363. }
  364. }
  365. /*!
  366. \brief handle USB Clear_Feature request
  367. \param[in] pudev: pointer to USB device instance
  368. \param[in] req: USB device request
  369. \param[out] none
  370. \retval none
  371. */
  372. static void usbd_clearfeature (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  373. {
  374. uint8_t ep_addr = 0U;
  375. switch (req->bmRequestType & USB_REQ_RECIPIENT_MASK) {
  376. case USB_REQTYPE_DEVICE:
  377. switch (pudev->status) {
  378. case USBD_ADDRESSED:
  379. case USBD_CONFIGURED:
  380. /* clear device remote wakeup feature */
  381. if (USB_FEATURE_REMOTE_WAKEUP == req->wValue) {
  382. pudev->remote_wakeup = 0U;
  383. pudev->class_req_handler(pudev, req);
  384. USBD_CONTRL_STATUS_TX();
  385. } else if (USB_FEATURE_TEST_MODE == req->wValue) {
  386. /* can not clear test_mode feature */
  387. usbd_enum_error(pudev, req);
  388. } else {
  389. /* no operation */
  390. }
  391. break;
  392. default:
  393. break;
  394. }
  395. break;
  396. case USB_REQTYPE_INTERFACE:
  397. switch (pudev->status) {
  398. case USBD_ADDRESSED:
  399. usbd_enum_error (pudev, req);
  400. break;
  401. case USBD_CONFIGURED:
  402. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  403. /* no operation */
  404. } else {
  405. usbd_enum_error(pudev, req);
  406. }
  407. break;
  408. default:
  409. break;
  410. }
  411. break;
  412. case USB_REQTYPE_ENDPOINT:
  413. /* get endpoint address */
  414. ep_addr = LOWBYTE(req->wIndex);
  415. switch (pudev->status) {
  416. case USBD_ADDRESSED:
  417. if (IS_NOT_EP0(ep_addr)) {
  418. usbd_enum_error(pudev, req);
  419. }
  420. break;
  421. case USBD_CONFIGURED:
  422. /* clear endpoint halt feature */
  423. if (USB_FEATURE_ENDP_HALT == req->wValue) {
  424. if (IS_NOT_EP0(ep_addr)) {
  425. usbd_ep_clear_stall(pudev, ep_addr);
  426. }
  427. }
  428. pudev->class_req_handler(pudev, req);
  429. USBD_CONTRL_STATUS_TX();
  430. break;
  431. default:
  432. break;
  433. }
  434. break;
  435. default:
  436. usbd_enum_error(pudev, req);
  437. break;
  438. }
  439. }
  440. /*!
  441. \brief handle USB Set_Feature 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_setfeature (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  448. {
  449. uint8_t ep_addr = 0U;
  450. switch (req->bmRequestType & USB_REQ_RECIPIENT_MASK) {
  451. case USB_REQTYPE_DEVICE:
  452. switch (pudev->status) {
  453. case USBD_ADDRESSED:
  454. case USBD_CONFIGURED:
  455. /* set device remote wakeup feature */
  456. if (USB_FEATURE_REMOTE_WAKEUP == req->wValue) {
  457. pudev->remote_wakeup = 1U;
  458. USBD_CONTRL_STATUS_TX();
  459. }
  460. break;
  461. default:
  462. break;
  463. }
  464. break;
  465. case USB_REQTYPE_INTERFACE:
  466. switch (pudev->status) {
  467. case USBD_ADDRESSED:
  468. usbd_enum_error(pudev, req);
  469. break;
  470. case USBD_CONFIGURED:
  471. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  472. /* no operation */
  473. } else {
  474. usbd_enum_error(pudev, req);
  475. }
  476. break;
  477. default:
  478. break;
  479. }
  480. break;
  481. case USB_REQTYPE_ENDPOINT:
  482. /* get endpoint address */
  483. ep_addr = LOWBYTE(req->wIndex);
  484. switch (pudev->status) {
  485. case USBD_ADDRESSED:
  486. if (IS_NOT_EP0(ep_addr)) {
  487. usbd_enum_error(pudev, req);
  488. }
  489. break;
  490. case USBD_CONFIGURED:
  491. /* set endpoint halt feature */
  492. if (USB_FEATURE_ENDP_HALT == req->wValue) {
  493. if (IS_NOT_EP0(ep_addr)) {
  494. usbd_ep_stall(pudev, ep_addr);
  495. }
  496. }
  497. USBD_CONTRL_STATUS_TX();
  498. break;
  499. default:
  500. break;
  501. }
  502. break;
  503. default:
  504. usbd_enum_error(pudev, req);
  505. break;
  506. }
  507. }
  508. /*!
  509. \brief handle USB Set_Address request
  510. \param[in] pudev: pointer to USB device instance
  511. \param[in] req: pointer to USB device request
  512. \param[out] none
  513. \retval none
  514. */
  515. static void usbd_setaddress (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  516. {
  517. if ((0U == req->wIndex) && (0U == req->wLength)) {
  518. g_device_address = (uint8_t)(req->wValue) & 0x7FU;
  519. if (USBD_CONFIGURED == pudev->status) {
  520. usbd_enum_error(pudev, req);
  521. } else {
  522. USBD_CONTRL_STATUS_TX();
  523. if (0U != g_device_address) {
  524. pudev->status = USBD_ADDRESSED;
  525. } else {
  526. pudev->status = USBD_DEFAULT;
  527. }
  528. }
  529. } else {
  530. usbd_enum_error(pudev, req);
  531. }
  532. }
  533. /*!
  534. \brief handle USB Get_Descriptor request
  535. \param[in] pudev: pointer to USB device instance
  536. \param[in] req: pointer to USB device request
  537. \param[out] none
  538. \retval none
  539. */
  540. static void usbd_getdescriptor (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  541. {
  542. if (USB_REQTYPE_DEVICE == (req->bmRequestType & USB_REQ_RECIPIENT_MASK)) {
  543. uint8_t *pbuf = NULL;
  544. uint8_t desc_index = (uint8_t)(req->wValue >> 8);
  545. uint16_t len = 0U;
  546. if (desc_index <= 0x03U) {
  547. /* call corresponding descriptor get function */
  548. pbuf = standard_descriptor_get[desc_index - 1U](pudev, (uint8_t)(req->wValue) & 0xFFU, &len);
  549. }
  550. #ifdef LPM_ENABLED
  551. else if (USB_DESCTYPE_BOS == desc_index) {
  552. pbuf = usbd_bos_descriptor_get(pudev, &len);
  553. }
  554. #endif /* LPM_ENABLED */
  555. else {
  556. usbd_enum_error(pudev, req);
  557. }
  558. if ((0U != len) && (0U != req->wLength)) {
  559. len = MIN(len, req->wLength);
  560. usbd_ep_tx (pudev, EP0_IN, pbuf, len);
  561. }
  562. } else if (USB_REQTYPE_INTERFACE == (req->bmRequestType & USB_REQ_RECIPIENT_MASK)) {
  563. if (NULL != pudev->class_req_handler) {
  564. /* get device class special descriptor */
  565. pudev->class_req_handler(pudev, req);
  566. }
  567. } else {
  568. }
  569. }
  570. /*!
  571. \brief handle USB Set_Descriptor request
  572. \param[in] pudev: pointer to USB device instance
  573. \param[in] req: pointer to USB device request
  574. \param[out] none
  575. \retval none
  576. */
  577. static void usbd_setdescriptor (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  578. {
  579. /* no handle */
  580. }
  581. /*!
  582. \brief handle USB Get_Configuration request
  583. \param[in] pudev: pointer to USB device instance
  584. \param[in] req: pointer to USB device request
  585. \param[out] none
  586. \retval none
  587. */
  588. static void usbd_getconfiguration (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  589. {
  590. uint32_t usbd_default_config = 0U;
  591. if (req->wLength != 1) {
  592. usbd_enum_error(pudev, req);
  593. } else {
  594. switch (pudev->status) {
  595. case USBD_ADDRESSED:
  596. usbd_ep_tx (pudev, EP0_IN, (uint8_t *)&usbd_default_config, 1U);
  597. break;
  598. case USBD_CONFIGURED:
  599. usbd_ep_tx (pudev, EP0_IN, &pudev->config_num, 1);
  600. break;
  601. default:
  602. usbd_enum_error(pudev, req);
  603. break;
  604. }
  605. }
  606. }
  607. /*!
  608. \brief handle USB Set_Configuration request
  609. \param[in] pudev: pointer to USB device instance
  610. \param[in] req: pointer to USB device request
  611. \param[out] none
  612. \retval none
  613. */
  614. static void usbd_setconfiguration (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  615. {
  616. static uint8_t cfgidx;
  617. cfgidx = (uint8_t)(req->wValue);
  618. if (cfgidx > USBD_CFG_MAX_NUM) {
  619. usbd_enum_error(pudev, req);
  620. } else {
  621. switch (pudev->status) {
  622. case USBD_ADDRESSED:
  623. if (cfgidx){
  624. pudev->config_num = cfgidx;
  625. pudev->status = USBD_CONFIGURED;
  626. pudev->class_init(pudev, cfgidx);
  627. USBD_CONTRL_STATUS_TX();
  628. } else {
  629. USBD_CONTRL_STATUS_TX();
  630. }
  631. break;
  632. case USBD_CONFIGURED:
  633. if (0U == cfgidx) {
  634. pudev->status = USBD_ADDRESSED;
  635. pudev->config_num = cfgidx;
  636. pudev->class_deinit(pudev, cfgidx);
  637. USBD_CONTRL_STATUS_TX();
  638. } else if (cfgidx != pudev->config_num) {
  639. /* clear old configuration */
  640. pudev->class_deinit(pudev, pudev->config_num);
  641. /* set new configuration */
  642. pudev->config_num = cfgidx;
  643. pudev->class_init(pudev, cfgidx);
  644. USBD_CONTRL_STATUS_TX();
  645. } else {
  646. USBD_CONTRL_STATUS_TX();
  647. }
  648. break;
  649. default:
  650. break;
  651. }
  652. }
  653. }
  654. /*!
  655. \brief handle USB Get_Interface request
  656. \param[in] pudev: pointer to USB device instance
  657. \param[in] req: pointer to USB device request
  658. \param[out] none
  659. \retval none
  660. */
  661. static void usbd_getinterface (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  662. {
  663. switch (pudev->status) {
  664. case USBD_ADDRESSED:
  665. usbd_enum_error(pudev, req);
  666. break;
  667. case USBD_CONFIGURED:
  668. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  669. if (NULL != pudev->class_req_handler) {
  670. pudev->class_req_handler(pudev, req);
  671. }
  672. } else {
  673. usbd_enum_error(pudev, req);
  674. }
  675. break;
  676. default:
  677. break;
  678. }
  679. }
  680. /*!
  681. \brief handle USB Set_Interface request
  682. \param[in] pudev: pointer to USB device instance
  683. \param[in] req: pointer to USB device request
  684. \param[out] none
  685. \retval none
  686. */
  687. static void usbd_setinterface (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  688. {
  689. switch (pudev->status) {
  690. case USBD_ADDRESSED:
  691. usbd_enum_error(pudev, req);
  692. break;
  693. case USBD_CONFIGURED:
  694. if (LOWBYTE(req->wIndex) <= USBD_ITF_MAX_NUM) {
  695. if (NULL != pudev->class_req_handler) {
  696. pudev->class_req_handler(pudev, req);
  697. }
  698. USBD_CONTRL_STATUS_TX();
  699. } else {
  700. usbd_enum_error(pudev, req);
  701. }
  702. break;
  703. default:
  704. break;
  705. }
  706. }
  707. /*!
  708. \brief handle USB SynchFrame request
  709. \param[in] pudev: pointer to USB device instance
  710. \param[in] req: pointer to USB device request
  711. \param[out] none
  712. \retval none
  713. */
  714. static void usbd_synchframe (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  715. {
  716. /* no handle */
  717. }
  718. /*!
  719. \brief decode setup data packet
  720. \param[in] pudev: pointer to USB device instance
  721. \param[in] req: pointer to USB device request
  722. \param[out] none
  723. \retval none
  724. */
  725. void usbd_setup_request_parse (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  726. {
  727. uint8_t *setup_data = pudev->setup_packet;
  728. req->bmRequestType = *setup_data;
  729. req->bRequest = *(uint8_t *)(setup_data + 1U);
  730. req->wValue = SWAPBYTE (setup_data + 2U);
  731. req->wIndex = SWAPBYTE (setup_data + 4U);
  732. req->wLength = SWAPBYTE (setup_data + 6U);
  733. pudev->ctl_count = req->wLength;
  734. }
  735. /*!
  736. \brief handle USB enumeration error event
  737. \param[in] pudev: pointer to USB device instance
  738. \param[in] req: pointer to USB device request
  739. \param[out] none
  740. \retval none
  741. */
  742. void usbd_enum_error (usbd_core_handle_struct *pudev, usb_device_req_struct *req)
  743. {
  744. usbd_ep_stall(pudev, EP0);
  745. }