]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_usb_mass_storage.c
usb: ums: add error handling for failed registration
[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 <errno.h>
9 #include <common.h>
10 #include <command.h>
11 #include <g_dnl.h>
12 #include <usb.h>
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 {
18         if (argc < 3)
19                 return CMD_RET_USAGE;
20
21         const char *usb_controller = argv[1];
22         const char *mmc_devstring  = argv[2];
23
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;
29
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.");
34                 return CMD_RET_FAILURE;
35         }
36
37         int rc = fsg_init(ums);
38         if (rc) {
39                 error("fsg_init failed");
40                 return CMD_RET_FAILURE;
41         }
42
43         rc = g_dnl_register("usb_dnl_ums");
44         if (rc) {
45                 error("g_dnl_register failed");
46                 return CMD_RET_FAILURE;
47         }
48
49         /* Timeout unit: seconds */
50         int cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
51
52         if (!g_dnl_board_usb_cable_connected()) {
53                 /*
54                  * Won't execute if we don't know whether the cable is
55                  * connected.
56                  */
57                 puts("Please connect USB cable.\n");
58
59                 while (!g_dnl_board_usb_cable_connected()) {
60                         if (ctrlc()) {
61                                 puts("\rCTRL+C - Operation aborted.\n");
62                                 goto exit;
63                         }
64                         if (!cable_ready_timeout) {
65                                 puts("\rUSB cable not detected.\n" \
66                                      "Command exit.\n");
67                                 goto exit;
68                         }
69
70                         printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
71                         mdelay(1000);
72                         cable_ready_timeout--;
73                 }
74                 puts("\r\n");
75         }
76
77         while (1) {
78                 usb_gadget_handle_interrupts();
79
80                 rc = fsg_main_thread(NULL);
81                 if (rc) {
82                         /* Check I/O error */
83                         if (rc == -EIO)
84                                 printf("\rCheck USB cable connection\n");
85
86                         /* Check CTRL+C */
87                         if (rc == -EPIPE)
88                                 printf("\rCTRL+C - Operation aborted\n");
89
90                         goto exit;
91                 }
92         }
93 exit:
94         g_dnl_unregister();
95         return CMD_RET_SUCCESS;
96 }
97
98 U_BOOT_CMD(ums, CONFIG_SYS_MAXARGS, 1, do_usb_mass_storage,
99         "Use the UMS [User Mass Storage]",
100         "ums <USB_controller> <mmc_dev>  e.g. ums 0 0"
101 );