SPDX: Convert all of our single license tags to Linux Kernel style
[platform/kernel/u-boot.git] / drivers / usb / gadget / f_sdp.c
index 9d82abc..00a9f88 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * f_sdp.c -- USB HID Serial Download Protocol
  *
@@ -13,8 +14,6 @@
  * SKIP_DCD_HEADER are only stubs.
  *
  * Parts of the implementation are based on f_dfu and f_thor.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <errno.h>
 #include <asm/io.h>
 #include <g_dnl.h>
 #include <sdp.h>
+#include <spl.h>
+#include <image.h>
 #include <imximage.h>
+#include <watchdog.h>
 
 #define HID_REPORT_ID_MASK     0x000000ff
 
@@ -227,6 +229,11 @@ static struct usb_gadget_strings *sdp_generic_strings[] = {
        NULL,
 };
 
+static inline void *sdp_ptr(u32 val)
+{
+       return (void *)(uintptr_t)val;
+}
+
 static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
 {
        struct f_sdp *sdp = req->context;
@@ -235,12 +242,12 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
        u8 report = data[0];
 
        if (status != 0) {
-               error("Status: %d", status);
+               pr_err("Status: %d\n", status);
                return;
        }
 
        if (report != 1) {
-               error("Unexpected report %d", report);
+               pr_err("Unexpected report %d\n", report);
                return;
        }
 
@@ -307,7 +314,7 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
                sdp->next_state = SDP_STATE_IDLE;
                break;
        default:
-               error("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
+               pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
        }
 }
 
@@ -320,12 +327,12 @@ static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
        int datalen = req->length - 1;
 
        if (status != 0) {
-               error("Status: %d", status);
+               pr_err("Status: %d\n", status);
                return;
        }
 
        if (report != 2) {
-               error("Unexpected report %d", report);
+               pr_err("Unexpected report %d\n", report);
                return;
        }
 
@@ -341,7 +348,7 @@ static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
        }
 
        if (sdp->state == SDP_STATE_RX_FILE_DATA) {
-               memcpy((void *)sdp->dnl_address, req->buf + 1, datalen);
+               memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen);
                sdp->dnl_address += datalen;
        }
 
@@ -358,7 +365,7 @@ static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
                sdp->state = SDP_STATE_TX_SEC_CONF;
                break;
        default:
-               error("Invalid state: %d", sdp->state);
+               pr_err("Invalid state: %d\n", sdp->state);
        }
 }
 
@@ -368,7 +375,7 @@ static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
        int status = req->status;
 
        if (status != 0) {
-               error("Status: %d", status);
+               pr_err("Status: %d\n", status);
                return;
        }
 
@@ -391,7 +398,7 @@ static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
                        sdp->state = SDP_STATE_IDLE;
                break;
        default:
-               error("Wrong State: %d", sdp->state);
+               pr_err("Wrong State: %d\n", sdp->state);
                sdp->state = SDP_STATE_IDLE;
                break;
        }
@@ -600,6 +607,8 @@ int sdp_init(int controller_index)
                        puts("\rCTRL+C - Operation aborted.\n");
                        return 1;
                }
+
+               WATCHDOG_RESET();
                usb_gadget_handle_interrupts(controller_index);
        }
 
@@ -617,7 +626,7 @@ static u32 sdp_jump_imxheader(void *address)
        }
 
        printf("Jumping to 0x%08x\n", headerv2->entry);
-       entry = (void *)headerv2->entry;
+       entry = sdp_ptr(headerv2->entry);
        entry();
 
        /* The image probably never returns hence we won't reach that point */
@@ -660,7 +669,7 @@ static void sdp_handle_in_ep(void)
                if (datalen > 64)
                        datalen = 64;
 
-               memcpy(&data[1], (void *)sdp_func->dnl_address, datalen);
+               memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen);
                sdp_func->in_req->length = 65;
 
                sdp_func->dnl_bytes_remaining -= datalen;
@@ -670,8 +679,22 @@ static void sdp_handle_in_ep(void)
                sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
                break;
        case SDP_STATE_JUMP:
-               printf("Checking imxheader at 0x%08x\n", f_sdp->jmp_address);
-               status = sdp_jump_imxheader((void *)f_sdp->jmp_address);
+               printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
+               status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address));
+
+               /* If imx header fails, try some U-Boot specific headers */
+               if (status) {
+#ifdef CONFIG_SPL_BUILD
+                       /* In SPL, allow jumps to U-Boot images */
+                       struct spl_image_info spl_image = {};
+                       spl_parse_image_header(&spl_image,
+                               (struct image_header *)sdp_func->jmp_address);
+                       jump_to_image_no_args(&spl_image);
+#else
+                       /* In U-Boot, allow jumps to scripts */
+                       source(sdp_func->jmp_address, "script@1");
+#endif
+               }
 
                sdp_func->next_state = SDP_STATE_IDLE;
                sdp_func->error_status = status;
@@ -696,6 +719,7 @@ void sdp_handle(int controller_index)
                        return;
                }
 
+               WATCHDOG_RESET();
                usb_gadget_handle_interrupts(controller_index);
 
                sdp_handle_in_ep();