blob: e0c1480d6d43705ce6cde47543db98414670fea4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Eddie Cai5e3020b2017-12-15 08:17:11 +08002/*
3 * Copyright (C) 2017 Eddie Cai <eddie.cai.linux@gmail.com>
Eddie Cai5e3020b2017-12-15 08:17:11 +08004 */
5
6#include <common.h>
7#include <command.h>
8#include <console.h>
9#include <g_dnl.h>
10#include <usb.h>
11#include <asm/arch/f_rockusb.h>
12
13static int do_rockusb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
14{
15 int controller_index, dev_index;
16 char *usb_controller;
17 char *devtype;
18 char *devnum;
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 if (argc >= 4) {
28 devtype = argv[2];
29 devnum = argv[3];
30 } else {
31 return CMD_RET_USAGE;
32 }
33 dev_index = simple_strtoul(devnum, NULL, 0);
34 rockusb_dev_init(devtype, dev_index);
35
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +010036 ret = usb_gadget_initialize(controller_index);
Eddie Cai5e3020b2017-12-15 08:17:11 +080037 if (ret) {
38 printf("USB init failed: %d\n", ret);
39 return CMD_RET_FAILURE;
40 }
41
42 g_dnl_clear_detach();
43 ret = g_dnl_register("usb_dnl_rockusb");
44 if (ret)
45 return CMD_RET_FAILURE;
46
47 if (!g_dnl_board_usb_cable_connected()) {
48 puts("\rUSB cable not detected, Command exit.\n");
49 ret = CMD_RET_FAILURE;
50 goto exit;
51 }
52
53 while (1) {
54 if (g_dnl_detach())
55 break;
56 if (ctrlc())
57 break;
58 usb_gadget_handle_interrupts(controller_index);
59 }
60 ret = CMD_RET_SUCCESS;
61
62exit:
63 g_dnl_unregister();
64 g_dnl_clear_detach();
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +010065 usb_gadget_release(controller_index);
Eddie Cai5e3020b2017-12-15 08:17:11 +080066
67 return ret;
68}
69
70U_BOOT_CMD(rockusb, 4, 1, do_rockusb,
71 "use the rockusb protocol",
72 "<USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0\n"
73);