usbd_std.c 23 KB

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