dfu.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * dfu.c -- dfu command
  3. *
  4. * Copyright (C) 2015
  5. * Lukasz Majewski <l.majewski@majess.pl>
  6. *
  7. * Copyright (C) 2012 Samsung Electronics
  8. * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  9. * Lukasz Majewski <l.majewski@samsung.com>
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <watchdog.h>
  15. #include <dfu.h>
  16. #include <console.h>
  17. #include <g_dnl.h>
  18. #include <usb.h>
  19. #include <net.h>
  20. int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
  21. {
  22. bool dfu_reset = false;
  23. int ret, i = 0;
  24. ret = board_usb_init(usbctrl_index, USB_INIT_DEVICE);
  25. if (ret) {
  26. error("board usb init failed\n");
  27. return CMD_RET_FAILURE;
  28. }
  29. g_dnl_clear_detach();
  30. ret = g_dnl_register(usb_dnl_gadget);
  31. if (ret) {
  32. error("g_dnl_register failed");
  33. return CMD_RET_FAILURE;
  34. }
  35. while (1) {
  36. if (g_dnl_detach()) {
  37. /*
  38. * Check if USB bus reset is performed after detach,
  39. * which indicates that -R switch has been passed to
  40. * dfu-util. In this case reboot the device
  41. */
  42. if (dfu_usb_get_reset()) {
  43. dfu_reset = true;
  44. goto exit;
  45. }
  46. /*
  47. * This extra number of usb_gadget_handle_interrupts()
  48. * calls is necessary to assure correct transmission
  49. * completion with dfu-util
  50. */
  51. if (++i == 10000)
  52. goto exit;
  53. }
  54. if (ctrlc())
  55. goto exit;
  56. if (dfu_get_defer_flush()) {
  57. /*
  58. * Call to usb_gadget_handle_interrupts() is necessary
  59. * to act on ZLP OUT transaction from HOST PC after
  60. * transmitting the whole file.
  61. *
  62. * If this ZLP OUT packet is NAK'ed, the HOST libusb
  63. * function fails after timeout (by default it is set to
  64. * 5 seconds). In such situation the dfu-util program
  65. * exits with error message.
  66. */
  67. usb_gadget_handle_interrupts(usbctrl_index);
  68. ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
  69. dfu_set_defer_flush(NULL);
  70. if (ret) {
  71. error("Deferred dfu_flush() failed!");
  72. goto exit;
  73. }
  74. }
  75. WATCHDOG_RESET();
  76. usb_gadget_handle_interrupts(usbctrl_index);
  77. }
  78. exit:
  79. g_dnl_unregister();
  80. board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE);
  81. if (dfu_reset)
  82. do_reset(NULL, 0, 0, NULL);
  83. g_dnl_clear_detach();
  84. return ret;
  85. }