dfu.c 2.4 KB

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