1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * cmd_sdp.c -- sdp command
4  *
5  * Copyright (C) 2016 Toradex
6  * Author: Stefan Agner <stefan.agner@toradex.com>
7  */
8 
9 #include <command.h>
10 #include <g_dnl.h>
11 #include <sdp.h>
12 #include <usb.h>
13 #include <linux/printk.h>
14 
do_sdp(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])15 static int do_sdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
16 {
17 	int controller_index;
18 	struct udevice *udc;
19 	int ret;
20 
21 	if (argc < 2)
22 		return CMD_RET_USAGE;
23 
24 	controller_index = simple_strtoul(argv[1], NULL, 0);
25 	ret = udc_device_get_by_index(controller_index, &udc);
26 	if (ret)
27 		return ret;
28 
29 	g_dnl_clear_detach();
30 	ret = g_dnl_register("usb_dnl_sdp");
31 	if (ret) {
32 		pr_err("SDP dnl register failed: %d\n", ret);
33 		goto exit_register;
34 	}
35 
36 	ret = sdp_init(udc);
37 	if (ret) {
38 		pr_err("SDP init failed: %d\n", ret);
39 		goto exit;
40 	}
41 
42 	/* This command typically does not return but jumps to an image */
43 	sdp_handle(udc);
44 	pr_err("SDP ended\n");
45 
46 exit:
47 	g_dnl_unregister();
48 exit_register:
49 	udc_device_put(udc);
50 
51 	return CMD_RET_FAILURE;
52 }
53 
54 U_BOOT_CMD(sdp, 2, 1, do_sdp,
55 	"Serial Downloader Protocol",
56 	"<USB_controller>\n"
57 	"  - serial downloader protocol via <USB_controller>\n"
58 );
59