dfu.c 2.0 KB

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