dfu.c 2.5 KB

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