Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics |
| 3 | * Lukasz Majewski <l.majewski@samsung.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Przemyslaw Marczak | 351e9b2 | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <g_dnl.h> |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 12 | #include <usb.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 13 | #include <usb_mass_storage.h> |
| 14 | |
| 15 | int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, |
| 16 | int argc, char * const argv[]) |
| 17 | { |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 18 | if (argc < 3) |
| 19 | return CMD_RET_USAGE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 20 | |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 21 | const char *usb_controller = argv[1]; |
| 22 | const char *mmc_devstring = argv[2]; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 23 | |
Przemyslaw Marczak | f4dacf7 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 24 | unsigned int dev_num = simple_strtoul(mmc_devstring, NULL, 0); |
| 25 | |
| 26 | struct ums *ums = ums_init(dev_num); |
| 27 | if (!ums) |
| 28 | return CMD_RET_FAILURE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 29 | |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 30 | unsigned int controller_index = (unsigned int)(simple_strtoul( |
| 31 | usb_controller, NULL, 0)); |
| 32 | if (board_usb_init(controller_index, USB_INIT_DEVICE)) { |
| 33 | error("Couldn't init USB controller."); |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 34 | return CMD_RET_FAILURE; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 35 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 36 | |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 37 | int rc = fsg_init(ums); |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 38 | if (rc) { |
| 39 | error("fsg_init failed"); |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 40 | return CMD_RET_FAILURE; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 43 | g_dnl_register("usb_dnl_ums"); |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 44 | |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 45 | /* Timeout unit: seconds */ |
| 46 | int cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
| 47 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 48 | if (!g_dnl_board_usb_cable_connected()) { |
| 49 | /* |
| 50 | * Won't execute if we don't know whether the cable is |
| 51 | * connected. |
| 52 | */ |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 53 | puts("Please connect USB cable.\n"); |
| 54 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 55 | while (!g_dnl_board_usb_cable_connected()) { |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 56 | if (ctrlc()) { |
| 57 | puts("\rCTRL+C - Operation aborted.\n"); |
| 58 | goto exit; |
| 59 | } |
| 60 | if (!cable_ready_timeout) { |
| 61 | puts("\rUSB cable not detected.\n" \ |
| 62 | "Command exit.\n"); |
| 63 | goto exit; |
| 64 | } |
| 65 | |
| 66 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); |
| 67 | mdelay(1000); |
| 68 | cable_ready_timeout--; |
| 69 | } |
| 70 | puts("\r\n"); |
| 71 | } |
| 72 | |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 73 | while (1) { |
Przemyslaw Marczak | 351e9b2 | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 74 | usb_gadget_handle_interrupts(); |
| 75 | |
| 76 | rc = fsg_main_thread(NULL); |
| 77 | if (rc) { |
| 78 | /* Check I/O error */ |
| 79 | if (rc == -EIO) |
| 80 | printf("\rCheck USB cable connection\n"); |
| 81 | |
| 82 | /* Check CTRL+C */ |
| 83 | if (rc == -EPIPE) |
| 84 | printf("\rCTRL+C - Operation aborted\n"); |
| 85 | |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 86 | goto exit; |
| 87 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 88 | } |
| 89 | exit: |
| 90 | g_dnl_unregister(); |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 91 | return CMD_RET_SUCCESS; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | U_BOOT_CMD(ums, CONFIG_SYS_MAXARGS, 1, do_usb_mass_storage, |
| 95 | "Use the UMS [User Mass Storage]", |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 96 | "ums <USB_controller> <mmc_dev> e.g. ums 0 0" |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 97 | ); |