xref: /openbmc/u-boot/cmd/fastboot.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2008 - 2009 Windriver, <www.windriver.com>
4  * Author: Tom Rix <Tom.Rix@windriver.com>
5  *
6  * (C) Copyright 2014 Linaro, Ltd.
7  * Rob Herring <robh@kernel.org>
8  */
9 #include <common.h>
10 #include <command.h>
11 #include <console.h>
12 #include <g_dnl.h>
13 #include <usb.h>
14 
15 static int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
16 {
17 	int controller_index;
18 	char *usb_controller;
19 	int ret;
20 
21 	if (argc < 2)
22 		return CMD_RET_USAGE;
23 
24 	usb_controller = argv[1];
25 	controller_index = simple_strtoul(usb_controller, NULL, 0);
26 
27 	ret = board_usb_init(controller_index, USB_INIT_DEVICE);
28 	if (ret) {
29 		pr_err("USB init failed: %d", ret);
30 		return CMD_RET_FAILURE;
31 	}
32 
33 	g_dnl_clear_detach();
34 	ret = g_dnl_register("usb_dnl_fastboot");
35 	if (ret)
36 		return ret;
37 
38 	if (!g_dnl_board_usb_cable_connected()) {
39 		puts("\rUSB cable not detected.\n" \
40 		     "Command exit.\n");
41 		ret = CMD_RET_FAILURE;
42 		goto exit;
43 	}
44 
45 	while (1) {
46 		if (g_dnl_detach())
47 			break;
48 		if (ctrlc())
49 			break;
50 		usb_gadget_handle_interrupts(controller_index);
51 	}
52 
53 	ret = CMD_RET_SUCCESS;
54 
55 exit:
56 	g_dnl_unregister();
57 	g_dnl_clear_detach();
58 	board_usb_cleanup(controller_index, USB_INIT_DEVICE);
59 
60 	return ret;
61 }
62 
63 U_BOOT_CMD(
64 	fastboot, 2, 1, do_fastboot,
65 	"use USB Fastboot protocol",
66 	"<USB_controller>\n"
67 	"    - run as a fastboot usb device"
68 );
69