dfu.c 2.1 KB

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