usb_device.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * Copyright (c) 2021-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <common/debug.h>
  9. #include <drivers/usb_device.h>
  10. /* Define for EP address */
  11. #define EP_DIR_MASK BIT(7)
  12. #define EP_DIR_IN BIT(7)
  13. #define EP_NUM_MASK GENMASK(3, 0)
  14. #define EP0_IN (0U | EP_DIR_IN)
  15. #define EP0_OUT 0U
  16. /* USB address between 1 through 127 = 0x7F mask */
  17. #define ADDRESS_MASK GENMASK(6, 0)
  18. /*
  19. * Set a STALL condition over an endpoint
  20. * pdev: USB handle
  21. * ep_addr: endpoint address
  22. * return : status
  23. */
  24. static enum usb_status usb_core_set_stall(struct usb_handle *pdev, uint8_t ep_addr)
  25. {
  26. struct usbd_ep *ep;
  27. struct pcd_handle *hpcd = (struct pcd_handle *)pdev->data;
  28. uint8_t num;
  29. num = ep_addr & EP_NUM_MASK;
  30. if (num >= USBD_EP_NB) {
  31. return USBD_FAIL;
  32. }
  33. if ((EP_DIR_MASK & ep_addr) == EP_DIR_IN) {
  34. ep = &hpcd->in_ep[num];
  35. ep->is_in = true;
  36. } else {
  37. ep = &hpcd->out_ep[num];
  38. ep->is_in = false;
  39. }
  40. ep->num = num;
  41. pdev->driver->ep_set_stall(hpcd->instance, ep);
  42. if (num == 0U) {
  43. pdev->driver->ep0_out_start(hpcd->instance);
  44. }
  45. return USBD_OK;
  46. }
  47. /*
  48. * usb_core_get_desc
  49. * Handle Get Descriptor requests
  50. * pdev : device instance
  51. * req : usb request
  52. */
  53. static void usb_core_get_desc(struct usb_handle *pdev, struct usb_setup_req *req)
  54. {
  55. uint16_t len;
  56. uint8_t *pbuf;
  57. uint8_t desc_type = HIBYTE(req->value);
  58. uint8_t desc_idx = LOBYTE(req->value);
  59. switch (desc_type) {
  60. case USB_DESC_TYPE_DEVICE:
  61. pbuf = pdev->desc->get_device_desc(&len);
  62. break;
  63. case USB_DESC_TYPE_CONFIGURATION:
  64. pbuf = pdev->desc->get_config_desc(&len);
  65. break;
  66. case USB_DESC_TYPE_STRING:
  67. switch (desc_idx) {
  68. case USBD_IDX_LANGID_STR:
  69. pbuf = pdev->desc->get_lang_id_desc(&len);
  70. break;
  71. case USBD_IDX_MFC_STR:
  72. pbuf = pdev->desc->get_manufacturer_desc(&len);
  73. break;
  74. case USBD_IDX_PRODUCT_STR:
  75. pbuf = pdev->desc->get_product_desc(&len);
  76. break;
  77. case USBD_IDX_SERIAL_STR:
  78. pbuf = pdev->desc->get_serial_desc(&len);
  79. break;
  80. case USBD_IDX_CONFIG_STR:
  81. pbuf = pdev->desc->get_configuration_desc(&len);
  82. break;
  83. case USBD_IDX_INTERFACE_STR:
  84. pbuf = pdev->desc->get_interface_desc(&len);
  85. break;
  86. /* For all USER string */
  87. case USBD_IDX_USER0_STR:
  88. default:
  89. pbuf = pdev->desc->get_usr_desc(desc_idx - USBD_IDX_USER0_STR, &len);
  90. break;
  91. }
  92. break;
  93. case USB_DESC_TYPE_DEVICE_QUALIFIER:
  94. pbuf = pdev->desc->get_device_qualifier_desc(&len);
  95. break;
  96. case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION:
  97. if (pdev->desc->get_other_speed_config_desc == NULL) {
  98. usb_core_ctl_error(pdev);
  99. return;
  100. }
  101. pbuf = pdev->desc->get_other_speed_config_desc(&len);
  102. break;
  103. default:
  104. ERROR("Unknown request %i\n", desc_type);
  105. usb_core_ctl_error(pdev);
  106. return;
  107. }
  108. if ((len != 0U) && (req->length != 0U)) {
  109. len = MIN(len, req->length);
  110. /* Start the transfer */
  111. usb_core_transmit_ep0(pdev, pbuf, len);
  112. }
  113. }
  114. /*
  115. * usb_core_set_config
  116. * Handle Set device configuration request
  117. * pdev : device instance
  118. * req : usb request
  119. */
  120. static void usb_core_set_config(struct usb_handle *pdev, struct usb_setup_req *req)
  121. {
  122. static uint8_t cfgidx;
  123. cfgidx = LOBYTE(req->value);
  124. if (cfgidx > USBD_MAX_NUM_CONFIGURATION) {
  125. usb_core_ctl_error(pdev);
  126. return;
  127. }
  128. switch (pdev->dev_state) {
  129. case USBD_STATE_ADDRESSED:
  130. if (cfgidx != 0U) {
  131. pdev->dev_config = cfgidx;
  132. pdev->dev_state = USBD_STATE_CONFIGURED;
  133. if (!pdev->class) {
  134. usb_core_ctl_error(pdev);
  135. return;
  136. }
  137. /* Set configuration and Start the Class */
  138. if (pdev->class->init(pdev, cfgidx) != 0U) {
  139. usb_core_ctl_error(pdev);
  140. return;
  141. }
  142. }
  143. break;
  144. case USBD_STATE_CONFIGURED:
  145. if (cfgidx == 0U) {
  146. pdev->dev_state = USBD_STATE_ADDRESSED;
  147. pdev->dev_config = cfgidx;
  148. pdev->class->de_init(pdev, cfgidx);
  149. } else if (cfgidx != pdev->dev_config) {
  150. if (pdev->class == NULL) {
  151. usb_core_ctl_error(pdev);
  152. return;
  153. }
  154. /* Clear old configuration */
  155. pdev->class->de_init(pdev, pdev->dev_config);
  156. /* Set new configuration */
  157. pdev->dev_config = cfgidx;
  158. /* Set configuration and start the USB class */
  159. if (pdev->class->init(pdev, cfgidx) != 0U) {
  160. usb_core_ctl_error(pdev);
  161. return;
  162. }
  163. }
  164. break;
  165. default:
  166. usb_core_ctl_error(pdev);
  167. return;
  168. }
  169. /* Send status */
  170. usb_core_transmit_ep0(pdev, NULL, 0U);
  171. }
  172. /*
  173. * usb_core_get_status
  174. * Handle Get Status request
  175. * pdev : device instance
  176. * req : usb request
  177. */
  178. static void usb_core_get_status(struct usb_handle *pdev,
  179. struct usb_setup_req *req)
  180. {
  181. if ((pdev->dev_state != USBD_STATE_ADDRESSED) &&
  182. (pdev->dev_state != USBD_STATE_CONFIGURED)) {
  183. usb_core_ctl_error(pdev);
  184. return;
  185. }
  186. pdev->dev_config_status = USB_CONFIG_SELF_POWERED;
  187. if (pdev->dev_remote_wakeup != 0U) {
  188. pdev->dev_config_status |= USB_CONFIG_REMOTE_WAKEUP;
  189. }
  190. /* Start the transfer */
  191. usb_core_transmit_ep0(pdev, (uint8_t *)&pdev->dev_config_status, 2U);
  192. }
  193. /*
  194. * usb_core_set_address
  195. * Set device address
  196. * pdev : device instance
  197. * req : usb request
  198. */
  199. static void usb_core_set_address(struct usb_handle *pdev,
  200. struct usb_setup_req *req)
  201. {
  202. uint8_t dev_addr;
  203. if ((req->index != 0U) || (req->length != 0U)) {
  204. usb_core_ctl_error(pdev);
  205. return;
  206. }
  207. dev_addr = req->value & ADDRESS_MASK;
  208. if (pdev->dev_state != USBD_STATE_DEFAULT) {
  209. usb_core_ctl_error(pdev);
  210. return;
  211. }
  212. pdev->dev_address = dev_addr;
  213. pdev->driver->set_address(((struct pcd_handle *)(pdev->data))->instance, dev_addr);
  214. /* Send status */
  215. usb_core_transmit_ep0(pdev, NULL, 0U);
  216. if (dev_addr != 0U) {
  217. pdev->dev_state = USBD_STATE_ADDRESSED;
  218. } else {
  219. pdev->dev_state = USBD_STATE_DEFAULT;
  220. }
  221. }
  222. /*
  223. * usb_core_dev_req
  224. * Handle standard usb device requests
  225. * pdev : device instance
  226. * req : usb request
  227. * return : status
  228. */
  229. static enum usb_status usb_core_dev_req(struct usb_handle *pdev,
  230. struct usb_setup_req *req)
  231. {
  232. VERBOSE("receive request %i\n", req->b_request);
  233. switch (req->b_request) {
  234. case USB_REQ_GET_DESCRIPTOR:
  235. usb_core_get_desc(pdev, req);
  236. break;
  237. case USB_REQ_SET_CONFIGURATION:
  238. usb_core_set_config(pdev, req);
  239. break;
  240. case USB_REQ_GET_STATUS:
  241. usb_core_get_status(pdev, req);
  242. break;
  243. case USB_REQ_SET_ADDRESS:
  244. usb_core_set_address(pdev, req);
  245. break;
  246. case USB_REQ_GET_CONFIGURATION:
  247. case USB_REQ_SET_FEATURE:
  248. case USB_REQ_CLEAR_FEATURE:
  249. default:
  250. ERROR("NOT SUPPORTED %i\n", req->b_request);
  251. usb_core_ctl_error(pdev);
  252. break;
  253. }
  254. return USBD_OK;
  255. }
  256. /*
  257. * usb_core_itf_req
  258. * Handle standard usb interface requests
  259. * pdev : device instance
  260. * req : usb request
  261. * return : status
  262. */
  263. static enum usb_status usb_core_itf_req(struct usb_handle *pdev,
  264. struct usb_setup_req *req)
  265. {
  266. if (pdev->dev_state != USBD_STATE_CONFIGURED) {
  267. usb_core_ctl_error(pdev);
  268. return USBD_OK;
  269. }
  270. if (LOBYTE(req->index) <= USBD_MAX_NUM_INTERFACES) {
  271. pdev->class->setup(pdev, req);
  272. if (req->length == 0U) {
  273. usb_core_transmit_ep0(pdev, NULL, 0U);
  274. }
  275. } else {
  276. usb_core_ctl_error(pdev);
  277. }
  278. return USBD_OK;
  279. }
  280. /*
  281. * usb_core_setup_stage
  282. * Handle the setup stage
  283. * pdev: device instance
  284. * psetup : setup buffer
  285. * return : status
  286. */
  287. static enum usb_status usb_core_setup_stage(struct usb_handle *pdev,
  288. uint8_t *psetup)
  289. {
  290. struct usb_setup_req *req = &pdev->request;
  291. /* Copy setup buffer into req structure */
  292. req->bm_request = psetup[0];
  293. req->b_request = psetup[1];
  294. req->value = psetup[2] + (psetup[3] << 8);
  295. req->index = psetup[4] + (psetup[5] << 8);
  296. req->length = psetup[6] + (psetup[7] << 8);
  297. pdev->ep0_state = USBD_EP0_SETUP;
  298. pdev->ep0_data_len = pdev->request.length;
  299. switch (pdev->request.bm_request & USB_REQ_RECIPIENT_MASK) {
  300. case USB_REQ_RECIPIENT_DEVICE:
  301. usb_core_dev_req(pdev, &pdev->request);
  302. break;
  303. case USB_REQ_RECIPIENT_INTERFACE:
  304. usb_core_itf_req(pdev, &pdev->request);
  305. break;
  306. case USB_REQ_RECIPIENT_ENDPOINT:
  307. default:
  308. ERROR("receive unsupported request %u",
  309. pdev->request.bm_request & USB_REQ_RECIPIENT_MASK);
  310. usb_core_set_stall(pdev, pdev->request.bm_request & USB_REQ_DIRECTION);
  311. return USBD_FAIL;
  312. }
  313. return USBD_OK;
  314. }
  315. /*
  316. * usb_core_data_out
  317. * Handle data OUT stage
  318. * pdev: device instance
  319. * epnum: endpoint index
  320. * pdata: buffer to sent
  321. * return : status
  322. */
  323. static enum usb_status usb_core_data_out(struct usb_handle *pdev, uint8_t epnum,
  324. uint8_t *pdata)
  325. {
  326. struct usb_endpoint *pep;
  327. if (epnum == 0U) {
  328. pep = &pdev->ep_out[0];
  329. if (pdev->ep0_state == USBD_EP0_DATA_OUT) {
  330. if (pep->rem_length > pep->maxpacket) {
  331. pep->rem_length -= pep->maxpacket;
  332. usb_core_receive(pdev, 0U, pdata,
  333. MIN(pep->rem_length,
  334. pep->maxpacket));
  335. } else {
  336. if (pdev->class->ep0_rx_ready &&
  337. (pdev->dev_state == USBD_STATE_CONFIGURED)) {
  338. pdev->class->ep0_rx_ready(pdev);
  339. }
  340. usb_core_transmit_ep0(pdev, NULL, 0U);
  341. }
  342. }
  343. } else if (pdev->class->data_out != NULL &&
  344. (pdev->dev_state == USBD_STATE_CONFIGURED)) {
  345. pdev->class->data_out(pdev, epnum);
  346. }
  347. return USBD_OK;
  348. }
  349. /*
  350. * usb_core_data_in
  351. * Handle data in stage
  352. * pdev: device instance
  353. * epnum: endpoint index
  354. * pdata: buffer to fill
  355. * return : status
  356. */
  357. static enum usb_status usb_core_data_in(struct usb_handle *pdev, uint8_t epnum,
  358. uint8_t *pdata)
  359. {
  360. if (epnum == 0U) {
  361. struct usb_endpoint *pep = &pdev->ep_in[0];
  362. if (pdev->ep0_state == USBD_EP0_DATA_IN) {
  363. if (pep->rem_length > pep->maxpacket) {
  364. pep->rem_length -= pep->maxpacket;
  365. usb_core_transmit(pdev, 0U, pdata,
  366. pep->rem_length);
  367. /* Prepare EP for premature end of transfer */
  368. usb_core_receive(pdev, 0U, NULL, 0U);
  369. } else {
  370. /* Last packet is MPS multiple, send ZLP packet */
  371. if ((pep->total_length % pep->maxpacket == 0U) &&
  372. (pep->total_length >= pep->maxpacket) &&
  373. (pep->total_length < pdev->ep0_data_len)) {
  374. usb_core_transmit(pdev, 0U, NULL, 0U);
  375. pdev->ep0_data_len = 0U;
  376. /* Prepare endpoint for premature end of transfer */
  377. usb_core_receive(pdev, 0U, NULL, 0U);
  378. } else {
  379. if (pdev->class->ep0_tx_sent != NULL &&
  380. (pdev->dev_state ==
  381. USBD_STATE_CONFIGURED)) {
  382. pdev->class->ep0_tx_sent(pdev);
  383. }
  384. /* Start the transfer */
  385. usb_core_receive_ep0(pdev, NULL, 0U);
  386. }
  387. }
  388. }
  389. } else if ((pdev->class->data_in != NULL) &&
  390. (pdev->dev_state == USBD_STATE_CONFIGURED)) {
  391. pdev->class->data_in(pdev, epnum);
  392. }
  393. return USBD_OK;
  394. }
  395. /*
  396. * usb_core_suspend
  397. * Handle suspend event
  398. * pdev : device instance
  399. * return : status
  400. */
  401. static enum usb_status usb_core_suspend(struct usb_handle *pdev)
  402. {
  403. INFO("USB Suspend mode\n");
  404. pdev->dev_old_state = pdev->dev_state;
  405. pdev->dev_state = USBD_STATE_SUSPENDED;
  406. return USBD_OK;
  407. }
  408. /*
  409. * usb_core_resume
  410. * Handle resume event
  411. * pdev : device instance
  412. * return : status
  413. */
  414. static enum usb_status usb_core_resume(struct usb_handle *pdev)
  415. {
  416. INFO("USB Resume\n");
  417. pdev->dev_state = pdev->dev_old_state;
  418. return USBD_OK;
  419. }
  420. /*
  421. * usb_core_sof
  422. * Handle SOF event
  423. * pdev : device instance
  424. * return : status
  425. */
  426. static enum usb_status usb_core_sof(struct usb_handle *pdev)
  427. {
  428. if (pdev->dev_state == USBD_STATE_CONFIGURED) {
  429. if (pdev->class->sof != NULL) {
  430. pdev->class->sof(pdev);
  431. }
  432. }
  433. return USBD_OK;
  434. }
  435. /*
  436. * usb_core_disconnect
  437. * Handle device disconnection event
  438. * pdev : device instance
  439. * return : status
  440. */
  441. static enum usb_status usb_core_disconnect(struct usb_handle *pdev)
  442. {
  443. /* Free class resources */
  444. pdev->dev_state = USBD_STATE_DEFAULT;
  445. pdev->class->de_init(pdev, pdev->dev_config);
  446. return USBD_OK;
  447. }
  448. enum usb_status usb_core_handle_it(struct usb_handle *pdev)
  449. {
  450. uint32_t param = 0U;
  451. uint32_t len = 0U;
  452. struct usbd_ep *ep;
  453. switch (pdev->driver->it_handler(pdev->data->instance, &param)) {
  454. case USB_DATA_OUT:
  455. usb_core_data_out(pdev, param,
  456. pdev->data->out_ep[param].xfer_buff);
  457. break;
  458. case USB_DATA_IN:
  459. usb_core_data_in(pdev, param,
  460. pdev->data->in_ep[param].xfer_buff);
  461. break;
  462. case USB_SETUP:
  463. usb_core_setup_stage(pdev, (uint8_t *)pdev->data->setup);
  464. break;
  465. case USB_ENUM_DONE:
  466. break;
  467. case USB_READ_DATA_PACKET:
  468. ep = &pdev->data->out_ep[param & USBD_OUT_EPNUM_MASK];
  469. len = (param & USBD_OUT_COUNT_MASK) >> USBD_OUT_COUNT_SHIFT;
  470. pdev->driver->read_packet(pdev->data->instance,
  471. ep->xfer_buff, len);
  472. ep->xfer_buff += len;
  473. ep->xfer_count += len;
  474. break;
  475. case USB_READ_SETUP_PACKET:
  476. ep = &pdev->data->out_ep[param & USBD_OUT_EPNUM_MASK];
  477. len = (param & USBD_OUT_COUNT_MASK) >> 0x10;
  478. pdev->driver->read_packet(pdev->data->instance,
  479. (uint8_t *)pdev->data->setup, 8);
  480. ep->xfer_count += len;
  481. break;
  482. case USB_RESET:
  483. pdev->dev_state = USBD_STATE_DEFAULT;
  484. break;
  485. case USB_RESUME:
  486. if (pdev->data->lpm_state == LPM_L1) {
  487. pdev->data->lpm_state = LPM_L0;
  488. } else {
  489. usb_core_resume(pdev);
  490. }
  491. break;
  492. case USB_SUSPEND:
  493. usb_core_suspend(pdev);
  494. break;
  495. case USB_LPM:
  496. if (pdev->data->lpm_state == LPM_L0) {
  497. pdev->data->lpm_state = LPM_L1;
  498. } else {
  499. usb_core_suspend(pdev);
  500. }
  501. break;
  502. case USB_SOF:
  503. usb_core_sof(pdev);
  504. break;
  505. case USB_DISCONNECT:
  506. usb_core_disconnect(pdev);
  507. break;
  508. case USB_WRITE_EMPTY:
  509. pdev->driver->write_empty_tx_fifo(pdev->data->instance, param,
  510. pdev->data->in_ep[param].xfer_len,
  511. (uint32_t *)&pdev->data->in_ep[param].xfer_count,
  512. pdev->data->in_ep[param].maxpacket,
  513. &pdev->data->in_ep[param].xfer_buff);
  514. break;
  515. case USB_NOTHING:
  516. default:
  517. break;
  518. }
  519. return USBD_OK;
  520. }
  521. static void usb_core_start_xfer(struct usb_handle *pdev,
  522. void *handle,
  523. struct usbd_ep *ep)
  524. {
  525. if (ep->num == 0U) {
  526. pdev->driver->ep0_start_xfer(handle, ep);
  527. } else {
  528. pdev->driver->ep_start_xfer(handle, ep);
  529. }
  530. }
  531. /*
  532. * usb_core_receive
  533. * Receive an amount of data
  534. * pdev: USB handle
  535. * ep_addr: endpoint address
  536. * buf: pointer to the reception buffer
  537. * len: amount of data to be received
  538. * return : status
  539. */
  540. enum usb_status usb_core_receive(struct usb_handle *pdev, uint8_t ep_addr,
  541. uint8_t *buf, uint32_t len)
  542. {
  543. struct usbd_ep *ep;
  544. struct pcd_handle *hpcd = (struct pcd_handle *)pdev->data;
  545. uint8_t num;
  546. num = ep_addr & EP_NUM_MASK;
  547. if (num >= USBD_EP_NB) {
  548. return USBD_FAIL;
  549. }
  550. ep = &hpcd->out_ep[num];
  551. /* Setup and start the Xfer */
  552. ep->xfer_buff = buf;
  553. ep->xfer_len = len;
  554. ep->xfer_count = 0U;
  555. ep->is_in = false;
  556. ep->num = num;
  557. usb_core_start_xfer(pdev, hpcd->instance, ep);
  558. return USBD_OK;
  559. }
  560. /*
  561. * usb_core_transmit
  562. * Send an amount of data
  563. * pdev: USB handle
  564. * ep_addr: endpoint address
  565. * buf: pointer to the transmission buffer
  566. * len: amount of data to be sent
  567. * return : status
  568. */
  569. enum usb_status usb_core_transmit(struct usb_handle *pdev, uint8_t ep_addr,
  570. uint8_t *buf, uint32_t len)
  571. {
  572. struct usbd_ep *ep;
  573. struct pcd_handle *hpcd = (struct pcd_handle *)pdev->data;
  574. uint8_t num;
  575. num = ep_addr & EP_NUM_MASK;
  576. if (num >= USBD_EP_NB) {
  577. return USBD_FAIL;
  578. }
  579. ep = &hpcd->in_ep[num];
  580. /* Setup and start the Xfer */
  581. ep->xfer_buff = buf;
  582. ep->xfer_len = len;
  583. ep->xfer_count = 0U;
  584. ep->is_in = true;
  585. ep->num = num;
  586. usb_core_start_xfer(pdev, hpcd->instance, ep);
  587. return USBD_OK;
  588. }
  589. /*
  590. * usb_core_receive_ep0
  591. * Receive an amount of data on ep0
  592. * pdev: USB handle
  593. * buf: pointer to the reception buffer
  594. * len: amount of data to be received
  595. * return : status
  596. */
  597. enum usb_status usb_core_receive_ep0(struct usb_handle *pdev, uint8_t *buf,
  598. uint32_t len)
  599. {
  600. /* Prepare the reception of the buffer over EP0 */
  601. if (len != 0U) {
  602. pdev->ep0_state = USBD_EP0_DATA_OUT;
  603. } else {
  604. pdev->ep0_state = USBD_EP0_STATUS_OUT;
  605. }
  606. pdev->ep_out[0].total_length = len;
  607. pdev->ep_out[0].rem_length = len;
  608. /* Start the transfer */
  609. return usb_core_receive(pdev, 0U, buf, len);
  610. }
  611. /*
  612. * usb_core_transmit_ep0
  613. * Send an amount of data on ep0
  614. * pdev: USB handle
  615. * buf: pointer to the transmission buffer
  616. * len: amount of data to be sent
  617. * return : status
  618. */
  619. enum usb_status usb_core_transmit_ep0(struct usb_handle *pdev, uint8_t *buf,
  620. uint32_t len)
  621. {
  622. /* Set EP0 State */
  623. if (len != 0U) {
  624. pdev->ep0_state = USBD_EP0_DATA_IN;
  625. } else {
  626. pdev->ep0_state = USBD_EP0_STATUS_IN;
  627. }
  628. pdev->ep_in[0].total_length = len;
  629. pdev->ep_in[0].rem_length = len;
  630. /* Start the transfer */
  631. return usb_core_transmit(pdev, 0U, buf, len);
  632. }
  633. /*
  634. * usb_core_ctl_error
  635. * Handle USB low level error
  636. * pdev: device instance
  637. * req: usb request
  638. * return : None
  639. */
  640. void usb_core_ctl_error(struct usb_handle *pdev)
  641. {
  642. ERROR("%s : Send an ERROR\n", __func__);
  643. usb_core_set_stall(pdev, EP0_IN);
  644. usb_core_set_stall(pdev, EP0_OUT);
  645. }
  646. /*
  647. * usb_core_start
  648. * Start the USB device core.
  649. * pdev: Device Handle
  650. * return : USBD Status
  651. */
  652. enum usb_status usb_core_start(struct usb_handle *pdev)
  653. {
  654. /* Start the low level driver */
  655. pdev->driver->start_device(pdev->data->instance);
  656. return USBD_OK;
  657. }
  658. /*
  659. * usb_core_stop
  660. * Stop the USB device core.
  661. * pdev: Device Handle
  662. * return : USBD Status
  663. */
  664. enum usb_status usb_core_stop(struct usb_handle *pdev)
  665. {
  666. /* Free class resources */
  667. pdev->class->de_init(pdev, pdev->dev_config);
  668. /* Stop the low level driver */
  669. pdev->driver->stop_device(pdev->data->instance);
  670. return USBD_OK;
  671. }
  672. /*
  673. * register_usb_driver
  674. * Stop the USB device core.
  675. * pdev: Device Handle
  676. * pcd_handle: PCD handle
  677. * driver: USB driver
  678. * driver_handle: USB driver handle
  679. * return : USBD Status
  680. */
  681. enum usb_status register_usb_driver(struct usb_handle *pdev,
  682. struct pcd_handle *pcd_handle,
  683. const struct usb_driver *driver,
  684. void *driver_handle)
  685. {
  686. uint8_t i;
  687. assert(pdev != NULL);
  688. assert(pcd_handle != NULL);
  689. assert(driver != NULL);
  690. assert(driver_handle != NULL);
  691. /* Free class resources */
  692. pdev->driver = driver;
  693. pdev->data = pcd_handle;
  694. pdev->data->instance = driver_handle;
  695. pdev->dev_state = USBD_STATE_DEFAULT;
  696. pdev->ep0_state = USBD_EP0_IDLE;
  697. /* Copy endpoint information */
  698. for (i = 0U; i < USBD_EP_NB; i++) {
  699. pdev->ep_in[i].maxpacket = pdev->data->in_ep[i].maxpacket;
  700. pdev->ep_out[i].maxpacket = pdev->data->out_ep[i].maxpacket;
  701. }
  702. return USBD_OK;
  703. }
  704. /*
  705. * register_platform
  706. * Register the USB device core.
  707. * pdev: Device Handle
  708. * plat_call_back: callback
  709. * return : USBD Status
  710. */
  711. enum usb_status register_platform(struct usb_handle *pdev,
  712. const struct usb_desc *plat_call_back)
  713. {
  714. assert(pdev != NULL);
  715. assert(plat_call_back != NULL);
  716. /* Save platform info in class resources */
  717. pdev->desc = plat_call_back;
  718. return USBD_OK;
  719. }