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