]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
b528f713 ŁM |
2 | /* |
3 | * Copyright (C) 2011 Samsung Electronics | |
4 | * Lukasz Majewski <[email protected]> | |
5 | * | |
02585eb3 | 6 | * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved. |
b528f713 ŁM |
7 | */ |
8 | ||
351e9b20 | 9 | #include <errno.h> |
b528f713 ŁM |
10 | #include <common.h> |
11 | #include <command.h> | |
24b852a7 | 12 | #include <console.h> |
b528f713 | 13 | #include <g_dnl.h> |
abfe8afe | 14 | #include <part.h> |
16297cfb | 15 | #include <usb.h> |
b528f713 | 16 | #include <usb_mass_storage.h> |
85d0aec0 | 17 | #include <watchdog.h> |
b528f713 | 18 | |
abfe8afe SW |
19 | static int ums_read_sector(struct ums *ums_dev, |
20 | ulong start, lbaint_t blkcnt, void *buf) | |
21 | { | |
4101f687 | 22 | struct blk_desc *block_dev = &ums_dev->block_dev; |
abfe8afe | 23 | lbaint_t blkstart = start + ums_dev->start_sector; |
abfe8afe | 24 | |
c9f3c5f9 | 25 | return blk_dread(block_dev, blkstart, blkcnt, buf); |
abfe8afe SW |
26 | } |
27 | ||
28 | static int ums_write_sector(struct ums *ums_dev, | |
29 | ulong start, lbaint_t blkcnt, const void *buf) | |
30 | { | |
4101f687 | 31 | struct blk_desc *block_dev = &ums_dev->block_dev; |
abfe8afe | 32 | lbaint_t blkstart = start + ums_dev->start_sector; |
abfe8afe | 33 | |
c9f3c5f9 | 34 | return blk_dwrite(block_dev, blkstart, blkcnt, buf); |
abfe8afe SW |
35 | } |
36 | ||
02585eb3 SW |
37 | static struct ums *ums; |
38 | static int ums_count; | |
39 | ||
40 | static void ums_fini(void) | |
41 | { | |
42 | int i; | |
43 | ||
44 | for (i = 0; i < ums_count; i++) | |
45 | free((void *)ums[i].name); | |
46 | free(ums); | |
d419026a | 47 | ums = NULL; |
02585eb3 SW |
48 | ums_count = 0; |
49 | } | |
50 | ||
51 | #define UMS_NAME_LEN 16 | |
abfe8afe | 52 | |
a2e3a1d8 | 53 | static int ums_init(const char *devtype, const char *devnums_part_str) |
abfe8afe | 54 | { |
a2e3a1d8 | 55 | char *s, *t, *devnum_part_str, *name; |
4101f687 | 56 | struct blk_desc *block_dev; |
a2e3a1d8 JT |
57 | disk_partition_t info; |
58 | int partnum; | |
a238b0da | 59 | int ret = -1; |
02585eb3 | 60 | struct ums *ums_new; |
abfe8afe | 61 | |
a2e3a1d8 | 62 | s = strdup(devnums_part_str); |
02585eb3 SW |
63 | if (!s) |
64 | return -1; | |
65 | ||
66 | t = s; | |
67 | ums_count = 0; | |
68 | ||
69 | for (;;) { | |
a2e3a1d8 JT |
70 | devnum_part_str = strsep(&t, ","); |
71 | if (!devnum_part_str) | |
02585eb3 SW |
72 | break; |
73 | ||
a2e3a1d8 JT |
74 | partnum = blk_get_device_part_str(devtype, devnum_part_str, |
75 | &block_dev, &info, 1); | |
76 | ||
77 | if (partnum < 0) | |
02585eb3 SW |
78 | goto cleanup; |
79 | ||
a2e3a1d8 JT |
80 | /* Check if the argument is in legacy format. If yes, |
81 | * expose all partitions by setting the partnum = 0 | |
82 | * e.g. ums 0 mmc 0 | |
83 | */ | |
84 | if (!strchr(devnum_part_str, ':')) | |
85 | partnum = 0; | |
86 | ||
02585eb3 | 87 | /* f_mass_storage.c assumes SECTOR_SIZE sectors */ |
a238b0da | 88 | if (block_dev->blksz != SECTOR_SIZE) |
02585eb3 | 89 | goto cleanup; |
abfe8afe | 90 | |
02585eb3 | 91 | ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums)); |
a238b0da | 92 | if (!ums_new) |
02585eb3 | 93 | goto cleanup; |
02585eb3 SW |
94 | ums = ums_new; |
95 | ||
a2e3a1d8 JT |
96 | /* if partnum = 0, expose all partitions */ |
97 | if (partnum == 0) { | |
98 | ums[ums_count].start_sector = 0; | |
99 | ums[ums_count].num_sectors = block_dev->lba; | |
100 | } else { | |
101 | ums[ums_count].start_sector = info.start; | |
102 | ums[ums_count].num_sectors = info.size; | |
103 | } | |
104 | ||
02585eb3 SW |
105 | ums[ums_count].read_sector = ums_read_sector; |
106 | ums[ums_count].write_sector = ums_write_sector; | |
a2e3a1d8 | 107 | |
02585eb3 | 108 | name = malloc(UMS_NAME_LEN); |
a238b0da | 109 | if (!name) |
02585eb3 | 110 | goto cleanup; |
02585eb3 SW |
111 | snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count); |
112 | ums[ums_count].name = name; | |
113 | ums[ums_count].block_dev = *block_dev; | |
114 | ||
115 | printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n", | |
bcce53d0 | 116 | ums_count, ums[ums_count].block_dev.devnum, |
02585eb3 SW |
117 | ums[ums_count].block_dev.hwpart, |
118 | ums[ums_count].start_sector, | |
119 | ums[ums_count].num_sectors); | |
120 | ||
121 | ums_count++; | |
122 | } | |
123 | ||
a238b0da | 124 | if (ums_count) |
02585eb3 | 125 | ret = 0; |
d0cc456d | 126 | |
02585eb3 SW |
127 | cleanup: |
128 | free(s); | |
abfe8afe | 129 | |
02585eb3 SW |
130 | if (ret < 0) |
131 | ums_fini(); | |
abfe8afe | 132 | |
02585eb3 | 133 | return ret; |
abfe8afe SW |
134 | } |
135 | ||
b9203429 | 136 | static int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, |
b528f713 ŁM |
137 | int argc, char * const argv[]) |
138 | { | |
1725f128 SW |
139 | const char *usb_controller; |
140 | const char *devtype; | |
141 | const char *devnum; | |
1725f128 SW |
142 | unsigned int controller_index; |
143 | int rc; | |
144 | int cable_ready_timeout __maybe_unused; | |
145 | ||
16297cfb MZ |
146 | if (argc < 3) |
147 | return CMD_RET_USAGE; | |
b528f713 | 148 | |
1725f128 | 149 | usb_controller = argv[1]; |
8c600456 SW |
150 | if (argc >= 4) { |
151 | devtype = argv[2]; | |
152 | devnum = argv[3]; | |
153 | } else { | |
154 | devtype = "mmc"; | |
155 | devnum = argv[2]; | |
156 | } | |
f4dacf7b | 157 | |
02585eb3 SW |
158 | rc = ums_init(devtype, devnum); |
159 | if (rc < 0) | |
f4dacf7b | 160 | return CMD_RET_FAILURE; |
b528f713 | 161 | |
1725f128 SW |
162 | controller_index = (unsigned int)(simple_strtoul( |
163 | usb_controller, NULL, 0)); | |
a06955ae | 164 | if (usb_gadget_initialize(controller_index)) { |
71002b50 | 165 | pr_err("Couldn't init USB controller.\n"); |
02585eb3 SW |
166 | rc = CMD_RET_FAILURE; |
167 | goto cleanup_ums_init; | |
16297cfb | 168 | } |
b528f713 | 169 | |
02585eb3 | 170 | rc = fsg_init(ums, ums_count); |
b528f713 | 171 | if (rc) { |
71002b50 | 172 | pr_err("fsg_init failed\n"); |
02585eb3 SW |
173 | rc = CMD_RET_FAILURE; |
174 | goto cleanup_board; | |
b528f713 ŁM |
175 | } |
176 | ||
66b88b07 SW |
177 | rc = g_dnl_register("usb_dnl_ums"); |
178 | if (rc) { | |
71002b50 | 179 | pr_err("g_dnl_register failed\n"); |
02585eb3 SW |
180 | rc = CMD_RET_FAILURE; |
181 | goto cleanup_board; | |
66b88b07 | 182 | } |
b528f713 | 183 | |
3603e31d | 184 | /* Timeout unit: seconds */ |
1725f128 | 185 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
3603e31d | 186 | |
75504e95 MZ |
187 | if (!g_dnl_board_usb_cable_connected()) { |
188 | /* | |
189 | * Won't execute if we don't know whether the cable is | |
190 | * connected. | |
191 | */ | |
3603e31d PM |
192 | puts("Please connect USB cable.\n"); |
193 | ||
75504e95 | 194 | while (!g_dnl_board_usb_cable_connected()) { |
3603e31d PM |
195 | if (ctrlc()) { |
196 | puts("\rCTRL+C - Operation aborted.\n"); | |
02585eb3 SW |
197 | rc = CMD_RET_SUCCESS; |
198 | goto cleanup_register; | |
3603e31d PM |
199 | } |
200 | if (!cable_ready_timeout) { | |
201 | puts("\rUSB cable not detected.\n" \ | |
202 | "Command exit.\n"); | |
02585eb3 SW |
203 | rc = CMD_RET_SUCCESS; |
204 | goto cleanup_register; | |
3603e31d PM |
205 | } |
206 | ||
207 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); | |
208 | mdelay(1000); | |
209 | cable_ready_timeout--; | |
210 | } | |
211 | puts("\r\n"); | |
212 | } | |
213 | ||
b528f713 | 214 | while (1) { |
2d48aa69 | 215 | usb_gadget_handle_interrupts(controller_index); |
351e9b20 PM |
216 | |
217 | rc = fsg_main_thread(NULL); | |
218 | if (rc) { | |
219 | /* Check I/O error */ | |
220 | if (rc == -EIO) | |
221 | printf("\rCheck USB cable connection\n"); | |
222 | ||
223 | /* Check CTRL+C */ | |
224 | if (rc == -EPIPE) | |
225 | printf("\rCTRL+C - Operation aborted\n"); | |
226 | ||
02585eb3 SW |
227 | rc = CMD_RET_SUCCESS; |
228 | goto cleanup_register; | |
351e9b20 | 229 | } |
85d0aec0 PD |
230 | |
231 | WATCHDOG_RESET(); | |
b528f713 | 232 | } |
02585eb3 SW |
233 | |
234 | cleanup_register: | |
b528f713 | 235 | g_dnl_unregister(); |
02585eb3 | 236 | cleanup_board: |
a06955ae | 237 | usb_gadget_release(controller_index); |
02585eb3 SW |
238 | cleanup_ums_init: |
239 | ums_fini(); | |
240 | ||
241 | return rc; | |
b528f713 ŁM |
242 | } |
243 | ||
8c600456 | 244 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
ee02a65e | 245 | "Use the UMS [USB Mass Storage]", |
a2e3a1d8 | 246 | "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n" |
8c600456 | 247 | " devtype defaults to mmc" |
b528f713 | 248 | ); |