Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Eddie Cai | 453c95e | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Eddie Cai <eddie.cai.linux@gmail.com> |
Eddie Cai | 453c95e | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <console.h> |
| 9 | #include <g_dnl.h> |
| 10 | #include <usb.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 11 | #include <asm/arch-rockchip/f_rockusb.h> |
Eddie Cai | 453c95e | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 12 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, |
| 14 | char *const argv[]) |
Eddie Cai | 453c95e | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 15 | { |
| 16 | int controller_index, dev_index; |
| 17 | char *usb_controller; |
| 18 | char *devtype; |
| 19 | char *devnum; |
| 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 | if (argc >= 4) { |
| 29 | devtype = argv[2]; |
| 30 | devnum = argv[3]; |
| 31 | } else { |
| 32 | return CMD_RET_USAGE; |
| 33 | } |
| 34 | dev_index = simple_strtoul(devnum, NULL, 0); |
| 35 | rockusb_dev_init(devtype, dev_index); |
| 36 | |
Jean-Jacques Hiblot | a06955a | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 37 | ret = usb_gadget_initialize(controller_index); |
Eddie Cai | 453c95e | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 38 | if (ret) { |
| 39 | printf("USB init failed: %d\n", ret); |
| 40 | return CMD_RET_FAILURE; |
| 41 | } |
| 42 | |
| 43 | g_dnl_clear_detach(); |
| 44 | ret = g_dnl_register("usb_dnl_rockusb"); |
| 45 | if (ret) |
| 46 | return CMD_RET_FAILURE; |
| 47 | |
| 48 | if (!g_dnl_board_usb_cable_connected()) { |
| 49 | puts("\rUSB cable not detected, Command exit.\n"); |
| 50 | ret = CMD_RET_FAILURE; |
| 51 | goto exit; |
| 52 | } |
| 53 | |
| 54 | while (1) { |
| 55 | if (g_dnl_detach()) |
| 56 | break; |
| 57 | if (ctrlc()) |
| 58 | break; |
| 59 | usb_gadget_handle_interrupts(controller_index); |
| 60 | } |
| 61 | ret = CMD_RET_SUCCESS; |
| 62 | |
| 63 | exit: |
| 64 | g_dnl_unregister(); |
| 65 | g_dnl_clear_detach(); |
Jean-Jacques Hiblot | a06955a | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 66 | usb_gadget_release(controller_index); |
Eddie Cai | 453c95e | 2017-12-15 08:17:11 +0800 | [diff] [blame] | 67 | |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | U_BOOT_CMD(rockusb, 4, 1, do_rockusb, |
| 72 | "use the rockusb protocol", |
| 73 | "<USB_controller> <devtype> <dev[:part]> e.g. rockusb 0 mmc 0\n" |
| 74 | ); |