blob: 5f557d5f857df2916cc5f818abbd8ac1b5e08b1c [file] [log] [blame]
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01001/*
2 * Copyright (C) 2011 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01006 */
7
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +02008#include <errno.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01009#include <common.h>
10#include <command.h>
11#include <g_dnl.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020012#include <usb.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010013#include <usb_mass_storage.h>
14
15int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
16 int argc, char * const argv[])
17{
Mateusz Zalegad862f892013-10-04 19:22:26 +020018 if (argc < 3)
19 return CMD_RET_USAGE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010020
Mateusz Zalegad862f892013-10-04 19:22:26 +020021 const char *usb_controller = argv[1];
22 const char *mmc_devstring = argv[2];
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010023
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +020024 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 Majewski44a6ac92013-03-05 12:10:17 +010029
Mateusz Zalegad862f892013-10-04 19:22:26 +020030 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 Marczak674b1a62013-10-23 14:30:42 +020034 return CMD_RET_FAILURE;
Mateusz Zalegad862f892013-10-04 19:22:26 +020035 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010036
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +020037 int rc = fsg_init(ums);
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010038 if (rc) {
Mateusz Zalegad862f892013-10-04 19:22:26 +020039 error("fsg_init failed");
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +020040 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010041 }
42
Mateusz Zalegad862f892013-10-04 19:22:26 +020043 g_dnl_register("ums");
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010044
Przemyslaw Marczake92cc562014-01-07 15:08:37 +010045 /* Timeout unit: seconds */
46 int cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
47
48 if (!usb_cable_connected()) {
49 puts("Please connect USB cable.\n");
50
51 while (!usb_cable_connected()) {
52 if (ctrlc()) {
53 puts("\rCTRL+C - Operation aborted.\n");
54 goto exit;
55 }
56 if (!cable_ready_timeout) {
57 puts("\rUSB cable not detected.\n" \
58 "Command exit.\n");
59 goto exit;
60 }
61
62 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
63 mdelay(1000);
64 cable_ready_timeout--;
65 }
66 puts("\r\n");
67 }
68
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010069 while (1) {
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010070 usb_gadget_handle_interrupts();
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +020071
72 rc = fsg_main_thread(NULL);
73 if (rc) {
74 /* Check I/O error */
75 if (rc == -EIO)
76 printf("\rCheck USB cable connection\n");
77
78 /* Check CTRL+C */
79 if (rc == -EPIPE)
80 printf("\rCTRL+C - Operation aborted\n");
81
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010082 goto exit;
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +020083 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010084 }
85exit:
86 g_dnl_unregister();
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +020087 return CMD_RET_SUCCESS;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010088}
89
90U_BOOT_CMD(ums, CONFIG_SYS_MAXARGS, 1, do_usb_mass_storage,
91 "Use the UMS [User Mass Storage]",
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +020092 "ums <USB_controller> <mmc_dev> e.g. ums 0 0"
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010093);