dfu.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. board_usb_init(usbctrl_index, USB_INIT_DEVICE);
  25. g_dnl_clear_detach();
  26. ret = g_dnl_register(usb_dnl_gadget);
  27. if (ret) {
  28. error("g_dnl_register failed");
  29. return CMD_RET_FAILURE;
  30. }
  31. while (1) {
  32. if (g_dnl_detach()) {
  33. /*
  34. * Check if USB bus reset is performed after detach,
  35. * which indicates that -R switch has been passed to
  36. * dfu-util. In this case reboot the device
  37. */
  38. if (dfu_usb_get_reset()) {
  39. dfu_reset = true;
  40. goto exit;
  41. }
  42. /*
  43. * This extra number of usb_gadget_handle_interrupts()
  44. * calls is necessary to assure correct transmission
  45. * completion with dfu-util
  46. */
  47. if (++i == 10000)
  48. goto exit;
  49. }
  50. if (ctrlc())
  51. goto exit;
  52. if (dfu_get_defer_flush()) {
  53. /*
  54. * Call to usb_gadget_handle_interrupts() is necessary
  55. * to act on ZLP OUT transaction from HOST PC after
  56. * transmitting the whole file.
  57. *
  58. * If this ZLP OUT packet is NAK'ed, the HOST libusb
  59. * function fails after timeout (by default it is set to
  60. * 5 seconds). In such situation the dfu-util program
  61. * exits with error message.
  62. */
  63. usb_gadget_handle_interrupts(usbctrl_index);
  64. ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
  65. dfu_set_defer_flush(NULL);
  66. if (ret) {
  67. error("Deferred dfu_flush() failed!");
  68. goto exit;
  69. }
  70. }
  71. WATCHDOG_RESET();
  72. usb_gadget_handle_interrupts(usbctrl_index);
  73. }
  74. exit:
  75. g_dnl_unregister();
  76. board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE);
  77. if (dfu_reset)
  78. run_command("reset", 0);
  79. g_dnl_clear_detach();
  80. return ret;
  81. }