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