]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_usb_mass_storage.c
usb: ums: code refactoring to improve reusability on other boards.
[karo-tx-uboot.git] / common / cmd_usb_mass_storage.c
1 /*
2  * Copyright (C) 2011 Samsung Electronics
3  * Lukasz Majewski <l.majewski@samsung.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <g_dnl.h>
11 #include <usb.h>
12 #include <usb_mass_storage.h>
13
14 int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
15                                int argc, char * const argv[])
16 {
17         if (argc < 3)
18                 return CMD_RET_USAGE;
19
20         const char *usb_controller = argv[1];
21         const char *mmc_devstring  = argv[2];
22
23         unsigned int dev_num = (unsigned int)(simple_strtoul(mmc_devstring,
24                                 NULL, 0));
25         if (dev_num)
26                 return CMD_RET_USAGE;
27
28         unsigned int controller_index = (unsigned int)(simple_strtoul(
29                                         usb_controller, NULL, 0));
30         if (board_usb_init(controller_index, USB_INIT_DEVICE)) {
31                 error("Couldn't init USB controller.");
32                 return CMD_RET_FAILURE;
33         }
34
35         struct ums *ums = ums_init(dev_num);
36         if (!ums) {
37                 printf("MMC: %u no such device\n", dev_num);
38                 return CMD_RET_FAILURE;
39         }
40
41         int rc = fsg_init(ums);
42         if (rc) {
43                 error("fsg_init failed");
44                 return CMD_RET_FAILURE;
45         }
46
47         g_dnl_register("ums");
48
49         while (1) {
50                 /* Handle control-c and timeouts */
51                 if (ctrlc()) {
52                         error("The remote end did not respond in time.");
53                         goto exit;
54                 }
55
56                 usb_gadget_handle_interrupts();
57                 /* Check if USB cable has been detached */
58                 if (fsg_main_thread(NULL) == EIO)
59                         goto exit;
60         }
61 exit:
62         g_dnl_unregister();
63         return CMD_RET_SUCCESS;
64 }
65
66 U_BOOT_CMD(ums, CONFIG_SYS_MAXARGS, 1, do_usb_mass_storage,
67         "Use the UMS [User Mass Storage]",
68         "ums <USB_controller> <mmc_dev>  e.g. ums 0 0"
69 );