usb_dfu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <common/debug.h>
  9. #include <platform_def.h>
  10. #include <usb_dfu.h>
  11. /* Device states as defined in DFU spec */
  12. #define STATE_APP_IDLE 0
  13. #define STATE_APP_DETACH 1
  14. #define STATE_DFU_IDLE 2
  15. #define STATE_DFU_DNLOAD_SYNC 3
  16. #define STATE_DFU_DNLOAD_BUSY 4
  17. #define STATE_DFU_DNLOAD_IDLE 5
  18. #define STATE_DFU_MANIFEST_SYNC 6
  19. #define STATE_DFU_MANIFEST 7
  20. #define STATE_DFU_MANIFEST_WAIT_RESET 8
  21. #define STATE_DFU_UPLOAD_IDLE 9
  22. #define STATE_DFU_ERROR 10
  23. /* DFU errors */
  24. #define DFU_ERROR_NONE 0x00
  25. #define DFU_ERROR_TARGET 0x01
  26. #define DFU_ERROR_FILE 0x02
  27. #define DFU_ERROR_WRITE 0x03
  28. #define DFU_ERROR_ERASE 0x04
  29. #define DFU_ERROR_CHECK_ERASED 0x05
  30. #define DFU_ERROR_PROG 0x06
  31. #define DFU_ERROR_VERIFY 0x07
  32. #define DFU_ERROR_ADDRESS 0x08
  33. #define DFU_ERROR_NOTDONE 0x09
  34. #define DFU_ERROR_FIRMWARE 0x0A
  35. #define DFU_ERROR_VENDOR 0x0B
  36. #define DFU_ERROR_USB 0x0C
  37. #define DFU_ERROR_POR 0x0D
  38. #define DFU_ERROR_UNKNOWN 0x0E
  39. #define DFU_ERROR_STALLEDPKT 0x0F
  40. /* DFU request */
  41. #define DFU_DETACH 0
  42. #define DFU_DNLOAD 1
  43. #define DFU_UPLOAD 2
  44. #define DFU_GETSTATUS 3
  45. #define DFU_CLRSTATUS 4
  46. #define DFU_GETSTATE 5
  47. #define DFU_ABORT 6
  48. static bool usb_dfu_detach_req;
  49. /*
  50. * usb_dfu_init
  51. * Initialize the DFU interface
  52. * pdev: device instance
  53. * cfgidx: Configuration index
  54. * return: status
  55. */
  56. static uint8_t usb_dfu_init(struct usb_handle *pdev, uint8_t cfgidx)
  57. {
  58. (void)pdev;
  59. (void)cfgidx;
  60. /* Nothing to do in this stage */
  61. return USBD_OK;
  62. }
  63. /*
  64. * usb_dfu_de_init
  65. * De-Initialize the DFU layer
  66. * pdev: device instance
  67. * cfgidx: Configuration index
  68. * return: status
  69. */
  70. static uint8_t usb_dfu_de_init(struct usb_handle *pdev, uint8_t cfgidx)
  71. {
  72. (void)pdev;
  73. (void)cfgidx;
  74. /* Nothing to do in this stage */
  75. return USBD_OK;
  76. }
  77. /*
  78. * usb_dfu_data_in
  79. * handle data IN Stage
  80. * pdev: device instance
  81. * epnum: endpoint index
  82. * return: status
  83. */
  84. static uint8_t usb_dfu_data_in(struct usb_handle *pdev, uint8_t epnum)
  85. {
  86. (void)pdev;
  87. (void)epnum;
  88. return USBD_OK;
  89. }
  90. /*
  91. * usb_dfu_ep0_rx_ready
  92. * handle EP0 Rx Ready event
  93. * pdev: device
  94. * return: status
  95. */
  96. static uint8_t usb_dfu_ep0_rx_ready(struct usb_handle *pdev)
  97. {
  98. (void)pdev;
  99. return USBD_OK;
  100. }
  101. /*
  102. * usb_dfu_ep0_tx_ready
  103. * handle EP0 TRx Ready event
  104. * pdev: device instance
  105. * return: status
  106. */
  107. static uint8_t usb_dfu_ep0_tx_ready(struct usb_handle *pdev)
  108. {
  109. (void)pdev;
  110. return USBD_OK;
  111. }
  112. /*
  113. * usb_dfu_sof
  114. * handle SOF event
  115. * pdev: device instance
  116. * return: status
  117. */
  118. static uint8_t usb_dfu_sof(struct usb_handle *pdev)
  119. {
  120. (void)pdev;
  121. return USBD_OK;
  122. }
  123. /*
  124. * usb_dfu_iso_in_incomplete
  125. * handle data ISO IN Incomplete event
  126. * pdev: device instance
  127. * epnum: endpoint index
  128. * return: status
  129. */
  130. static uint8_t usb_dfu_iso_in_incomplete(struct usb_handle *pdev, uint8_t epnum)
  131. {
  132. (void)pdev;
  133. (void)epnum;
  134. return USBD_OK;
  135. }
  136. /*
  137. * usb_dfu_iso_out_incomplete
  138. * handle data ISO OUT Incomplete event
  139. * pdev: device instance
  140. * epnum: endpoint index
  141. * return: status
  142. */
  143. static uint8_t usb_dfu_iso_out_incomplete(struct usb_handle *pdev,
  144. uint8_t epnum)
  145. {
  146. (void)pdev;
  147. (void)epnum;
  148. return USBD_OK;
  149. }
  150. /*
  151. * usb_dfu_data_out
  152. * handle data OUT Stage
  153. * pdev: device instance
  154. * epnum: endpoint index
  155. * return: status
  156. */
  157. static uint8_t usb_dfu_data_out(struct usb_handle *pdev, uint8_t epnum)
  158. {
  159. (void)pdev;
  160. (void)epnum;
  161. return USBD_OK;
  162. }
  163. /*
  164. * usb_dfu_detach
  165. * Handles the DFU DETACH request.
  166. * pdev: device instance
  167. * req: pointer to the request structure.
  168. */
  169. static void usb_dfu_detach(struct usb_handle *pdev, struct usb_setup_req *req)
  170. {
  171. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  172. INFO("Receive DFU Detach\n");
  173. if ((hdfu->dev_state == STATE_DFU_IDLE) ||
  174. (hdfu->dev_state == STATE_DFU_DNLOAD_SYNC) ||
  175. (hdfu->dev_state == STATE_DFU_DNLOAD_IDLE) ||
  176. (hdfu->dev_state == STATE_DFU_MANIFEST_SYNC) ||
  177. (hdfu->dev_state == STATE_DFU_UPLOAD_IDLE)) {
  178. /* Update the state machine */
  179. hdfu->dev_state = STATE_DFU_IDLE;
  180. hdfu->dev_status = DFU_ERROR_NONE;
  181. }
  182. usb_dfu_detach_req = true;
  183. }
  184. /*
  185. * usb_dfu_download
  186. * Handles the DFU DNLOAD request.
  187. * pdev: device instance
  188. * req: pointer to the request structure
  189. */
  190. static void usb_dfu_download(struct usb_handle *pdev, struct usb_setup_req *req)
  191. {
  192. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  193. uintptr_t data_ptr;
  194. uint32_t length;
  195. int ret;
  196. /* Data setup request */
  197. if (req->length > 0) {
  198. /* Unsupported state */
  199. if ((hdfu->dev_state != STATE_DFU_IDLE) &&
  200. (hdfu->dev_state != STATE_DFU_DNLOAD_IDLE)) {
  201. /* Call the error management function (command will be nacked) */
  202. usb_core_ctl_error(pdev);
  203. return;
  204. }
  205. /* Get the data address */
  206. length = req->length;
  207. ret = hdfu->callback->download(hdfu->alt_setting, &data_ptr,
  208. &length, pdev->user_data);
  209. if (ret == 0U) {
  210. /* Update the state machine */
  211. hdfu->dev_state = STATE_DFU_DNLOAD_SYNC;
  212. /* Start the transfer */
  213. usb_core_receive_ep0(pdev, (uint8_t *)data_ptr, length);
  214. } else {
  215. usb_core_ctl_error(pdev);
  216. }
  217. } else {
  218. /* End of DNLOAD operation*/
  219. if (hdfu->dev_state != STATE_DFU_DNLOAD_IDLE) {
  220. /* Call the error management function (command will be nacked) */
  221. usb_core_ctl_error(pdev);
  222. return;
  223. }
  224. /* End of DNLOAD operation*/
  225. hdfu->dev_state = STATE_DFU_MANIFEST_SYNC;
  226. ret = hdfu->callback->manifestation(hdfu->alt_setting, pdev->user_data);
  227. if (ret == 0U) {
  228. hdfu->dev_state = STATE_DFU_MANIFEST_SYNC;
  229. } else {
  230. usb_core_ctl_error(pdev);
  231. }
  232. }
  233. }
  234. /*
  235. * usb_dfu_upload
  236. * Handles the DFU UPLOAD request.
  237. * pdev: instance
  238. * req: pointer to the request structure
  239. */
  240. static void usb_dfu_upload(struct usb_handle *pdev, struct usb_setup_req *req)
  241. {
  242. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  243. uintptr_t data_ptr;
  244. uint32_t length;
  245. int ret;
  246. /* Data setup request */
  247. if (req->length == 0) {
  248. /* No Data setup request */
  249. hdfu->dev_state = STATE_DFU_IDLE;
  250. return;
  251. }
  252. /* Unsupported state */
  253. if ((hdfu->dev_state != STATE_DFU_IDLE) && (hdfu->dev_state != STATE_DFU_UPLOAD_IDLE)) {
  254. ERROR("UPLOAD : Unsupported State\n");
  255. /* Call the error management function (command will be nacked) */
  256. usb_core_ctl_error(pdev);
  257. return;
  258. }
  259. /* Update the data address */
  260. length = req->length;
  261. ret = hdfu->callback->upload(hdfu->alt_setting, &data_ptr, &length, pdev->user_data);
  262. if (ret == 0U) {
  263. /* Short frame */
  264. hdfu->dev_state = (req->length > length) ? STATE_DFU_IDLE : STATE_DFU_UPLOAD_IDLE;
  265. /* Start the transfer */
  266. usb_core_transmit_ep0(pdev, (uint8_t *)data_ptr, length);
  267. } else {
  268. ERROR("UPLOAD : bad block %i on alt %i\n", req->value, req->index);
  269. hdfu->dev_state = STATE_DFU_ERROR;
  270. hdfu->dev_status = DFU_ERROR_STALLEDPKT;
  271. /* Call the error management function (command will be nacked) */
  272. usb_core_ctl_error(pdev);
  273. }
  274. }
  275. /*
  276. * usb_dfu_get_status
  277. * Handles the DFU GETSTATUS request.
  278. * pdev: instance
  279. */
  280. static void usb_dfu_get_status(struct usb_handle *pdev)
  281. {
  282. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  283. hdfu->status[0] = hdfu->dev_status; /* bStatus */
  284. hdfu->status[1] = 0; /* bwPollTimeout[3] */
  285. hdfu->status[2] = 0;
  286. hdfu->status[3] = 0;
  287. hdfu->status[4] = hdfu->dev_state; /* bState */
  288. hdfu->status[5] = 0; /* iString */
  289. /* next step */
  290. switch (hdfu->dev_state) {
  291. case STATE_DFU_DNLOAD_SYNC:
  292. hdfu->dev_state = STATE_DFU_DNLOAD_IDLE;
  293. break;
  294. case STATE_DFU_MANIFEST_SYNC:
  295. /* the device is 'ManifestationTolerant' */
  296. hdfu->status[4] = STATE_DFU_MANIFEST;
  297. hdfu->status[1] = 1U; /* bwPollTimeout = 1ms */
  298. hdfu->dev_state = STATE_DFU_IDLE;
  299. break;
  300. default:
  301. break;
  302. }
  303. /* Start the transfer */
  304. usb_core_transmit_ep0(pdev, (uint8_t *)&hdfu->status[0], sizeof(hdfu->status));
  305. }
  306. /*
  307. * usb_dfu_clear_status
  308. * Handles the DFU CLRSTATUS request.
  309. * pdev: device instance
  310. */
  311. static void usb_dfu_clear_status(struct usb_handle *pdev)
  312. {
  313. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  314. if (hdfu->dev_state == STATE_DFU_ERROR) {
  315. hdfu->dev_state = STATE_DFU_IDLE;
  316. hdfu->dev_status = DFU_ERROR_NONE;
  317. } else {
  318. /* State Error */
  319. hdfu->dev_state = STATE_DFU_ERROR;
  320. hdfu->dev_status = DFU_ERROR_UNKNOWN;
  321. }
  322. }
  323. /*
  324. * usb_dfu_get_state
  325. * Handles the DFU GETSTATE request.
  326. * pdev: device instance
  327. */
  328. static void usb_dfu_get_state(struct usb_handle *pdev)
  329. {
  330. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  331. /* Return the current state of the DFU interface */
  332. usb_core_transmit_ep0(pdev, &hdfu->dev_state, 1);
  333. }
  334. /*
  335. * usb_dfu_abort
  336. * Handles the DFU ABORT request.
  337. * pdev: device instance
  338. */
  339. static void usb_dfu_abort(struct usb_handle *pdev)
  340. {
  341. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  342. if ((hdfu->dev_state == STATE_DFU_IDLE) ||
  343. (hdfu->dev_state == STATE_DFU_DNLOAD_SYNC) ||
  344. (hdfu->dev_state == STATE_DFU_DNLOAD_IDLE) ||
  345. (hdfu->dev_state == STATE_DFU_MANIFEST_SYNC) ||
  346. (hdfu->dev_state == STATE_DFU_UPLOAD_IDLE)) {
  347. hdfu->dev_state = STATE_DFU_IDLE;
  348. hdfu->dev_status = DFU_ERROR_NONE;
  349. }
  350. }
  351. /*
  352. * usb_dfu_setup
  353. * Handle the DFU specific requests
  354. * pdev: instance
  355. * req: usb requests
  356. * return: status
  357. */
  358. static uint8_t usb_dfu_setup(struct usb_handle *pdev, struct usb_setup_req *req)
  359. {
  360. uint8_t *pbuf = NULL;
  361. uint16_t len = 0U;
  362. uint8_t ret = USBD_OK;
  363. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  364. switch (req->bm_request & USB_REQ_TYPE_MASK) {
  365. case USB_REQ_TYPE_CLASS:
  366. switch (req->b_request) {
  367. case DFU_DNLOAD:
  368. usb_dfu_download(pdev, req);
  369. break;
  370. case DFU_UPLOAD:
  371. usb_dfu_upload(pdev, req);
  372. break;
  373. case DFU_GETSTATUS:
  374. usb_dfu_get_status(pdev);
  375. break;
  376. case DFU_CLRSTATUS:
  377. usb_dfu_clear_status(pdev);
  378. break;
  379. case DFU_GETSTATE:
  380. usb_dfu_get_state(pdev);
  381. break;
  382. case DFU_ABORT:
  383. usb_dfu_abort(pdev);
  384. break;
  385. case DFU_DETACH:
  386. usb_dfu_detach(pdev, req);
  387. break;
  388. default:
  389. ERROR("unknown request %x on alternate %i\n",
  390. req->b_request, hdfu->alt_setting);
  391. usb_core_ctl_error(pdev);
  392. ret = USBD_FAIL;
  393. break;
  394. }
  395. break;
  396. case USB_REQ_TYPE_STANDARD:
  397. switch (req->b_request) {
  398. case USB_REQ_GET_DESCRIPTOR:
  399. if (HIBYTE(req->value) == DFU_DESCRIPTOR_TYPE) {
  400. pbuf = pdev->desc->get_config_desc(&len);
  401. /* DFU descriptor at the end of the USB */
  402. pbuf += len - 9U;
  403. len = 9U;
  404. len = MIN(len, req->length);
  405. }
  406. /* Start the transfer */
  407. usb_core_transmit_ep0(pdev, pbuf, len);
  408. break;
  409. case USB_REQ_GET_INTERFACE:
  410. /* Start the transfer */
  411. usb_core_transmit_ep0(pdev, (uint8_t *)&hdfu->alt_setting, 1U);
  412. break;
  413. case USB_REQ_SET_INTERFACE:
  414. hdfu->alt_setting = LOBYTE(req->value);
  415. break;
  416. default:
  417. usb_core_ctl_error(pdev);
  418. ret = USBD_FAIL;
  419. break;
  420. }
  421. default:
  422. break;
  423. }
  424. return ret;
  425. }
  426. static const struct usb_class usb_dfu = {
  427. .init = usb_dfu_init,
  428. .de_init = usb_dfu_de_init,
  429. .setup = usb_dfu_setup,
  430. .ep0_tx_sent = usb_dfu_ep0_tx_ready,
  431. .ep0_rx_ready = usb_dfu_ep0_rx_ready,
  432. .data_in = usb_dfu_data_in,
  433. .data_out = usb_dfu_data_out,
  434. .sof = usb_dfu_sof,
  435. .iso_in_incomplete = usb_dfu_iso_in_incomplete,
  436. .iso_out_incomplete = usb_dfu_iso_out_incomplete,
  437. };
  438. void usb_dfu_register(struct usb_handle *pdev, struct usb_dfu_handle *phandle)
  439. {
  440. pdev->class = (struct usb_class *)&usb_dfu;
  441. pdev->class_data = phandle;
  442. phandle->dev_state = STATE_DFU_IDLE;
  443. phandle->dev_status = DFU_ERROR_NONE;
  444. }
  445. int usb_dfu_loop(struct usb_handle *pdev, const struct usb_dfu_media *pmedia)
  446. {
  447. uint32_t it_count;
  448. enum usb_status ret;
  449. struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
  450. hdfu->callback = pmedia;
  451. usb_dfu_detach_req = false;
  452. /* Continue to handle USB core IT to assure complete data transmission */
  453. it_count = 100U;
  454. /* DFU infinite loop until DETACH_REQ */
  455. while (it_count != 0U) {
  456. ret = usb_core_handle_it(pdev);
  457. if (ret != USBD_OK) {
  458. return -EIO;
  459. }
  460. /* Detach request received */
  461. if (usb_dfu_detach_req) {
  462. it_count--;
  463. }
  464. }
  465. return 0;
  466. }