]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
b528f713 ŁM |
2 | /* |
3 | * Copyright (C) 2011 Samsung Electronics | |
4 | * Lukasz Majewski <l.majewski@samsung.com> | |
5 | * | |
02585eb3 | 6 | * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved. |
b528f713 ŁM |
7 | */ |
8 | ||
b528f713 | 9 | #include <common.h> |
e6f6f9e6 | 10 | #include <blk.h> |
b528f713 | 11 | #include <command.h> |
24b852a7 | 12 | #include <console.h> |
e6f6f9e6 | 13 | #include <errno.h> |
b528f713 | 14 | #include <g_dnl.h> |
336d4615 | 15 | #include <malloc.h> |
abfe8afe | 16 | #include <part.h> |
16297cfb | 17 | #include <usb.h> |
b528f713 | 18 | #include <usb_mass_storage.h> |
85d0aec0 | 19 | #include <watchdog.h> |
c05ed00a | 20 | #include <linux/delay.h> |
1e94b46f | 21 | #include <linux/printk.h> |
b528f713 | 22 | |
abfe8afe SW |
23 | static int ums_read_sector(struct ums *ums_dev, |
24 | ulong start, lbaint_t blkcnt, void *buf) | |
25 | { | |
4101f687 | 26 | struct blk_desc *block_dev = &ums_dev->block_dev; |
abfe8afe | 27 | lbaint_t blkstart = start + ums_dev->start_sector; |
abfe8afe | 28 | |
c9f3c5f9 | 29 | return blk_dread(block_dev, blkstart, blkcnt, buf); |
abfe8afe SW |
30 | } |
31 | ||
32 | static int ums_write_sector(struct ums *ums_dev, | |
33 | ulong start, lbaint_t blkcnt, const void *buf) | |
34 | { | |
4101f687 | 35 | struct blk_desc *block_dev = &ums_dev->block_dev; |
abfe8afe | 36 | lbaint_t blkstart = start + ums_dev->start_sector; |
abfe8afe | 37 | |
c9f3c5f9 | 38 | return blk_dwrite(block_dev, blkstart, blkcnt, buf); |
abfe8afe SW |
39 | } |
40 | ||
02585eb3 SW |
41 | static struct ums *ums; |
42 | static int ums_count; | |
43 | ||
44 | static void ums_fini(void) | |
45 | { | |
46 | int i; | |
47 | ||
48 | for (i = 0; i < ums_count; i++) | |
49 | free((void *)ums[i].name); | |
50 | free(ums); | |
d419026a | 51 | ums = NULL; |
02585eb3 SW |
52 | ums_count = 0; |
53 | } | |
54 | ||
55 | #define UMS_NAME_LEN 16 | |
abfe8afe | 56 | |
a2e3a1d8 | 57 | static int ums_init(const char *devtype, const char *devnums_part_str) |
abfe8afe | 58 | { |
a2e3a1d8 | 59 | char *s, *t, *devnum_part_str, *name; |
4101f687 | 60 | struct blk_desc *block_dev; |
0528979f | 61 | struct disk_partition info; |
a2e3a1d8 | 62 | int partnum; |
a238b0da | 63 | int ret = -1; |
02585eb3 | 64 | struct ums *ums_new; |
abfe8afe | 65 | |
a2e3a1d8 | 66 | s = strdup(devnums_part_str); |
02585eb3 SW |
67 | if (!s) |
68 | return -1; | |
69 | ||
70 | t = s; | |
71 | ums_count = 0; | |
72 | ||
73 | for (;;) { | |
a2e3a1d8 JT |
74 | devnum_part_str = strsep(&t, ","); |
75 | if (!devnum_part_str) | |
02585eb3 SW |
76 | break; |
77 | ||
1833e68c SA |
78 | partnum = part_get_info_by_dev_and_name_or_num(devtype, devnum_part_str, |
79 | &block_dev, &info, 1); | |
a2e3a1d8 JT |
80 | |
81 | if (partnum < 0) | |
02585eb3 SW |
82 | goto cleanup; |
83 | ||
a2e3a1d8 JT |
84 | /* Check if the argument is in legacy format. If yes, |
85 | * expose all partitions by setting the partnum = 0 | |
86 | * e.g. ums 0 mmc 0 | |
87 | */ | |
88 | if (!strchr(devnum_part_str, ':')) | |
89 | partnum = 0; | |
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 | ||
c954ff5f MS |
115 | printf("UMS: LUN %d, dev %s %d, hwpart %d, sector %#x, count %#x\n", |
116 | ums_count, devtype, 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 | ||
09140113 SG |
136 | static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag, |
137 | int argc, char *const argv[]) | |
b528f713 | 138 | { |
1725f128 SW |
139 | const char *usb_controller; |
140 | const char *devtype; | |
141 | const char *devnum; | |
1725f128 | 142 | unsigned int controller_index; |
f032260c | 143 | struct udevice *udc; |
1725f128 SW |
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)); | |
f032260c MV |
165 | rc = udc_device_get_by_index(controller_index, &udc); |
166 | if (rc) { | |
71002b50 | 167 | pr_err("Couldn't init USB controller.\n"); |
02585eb3 SW |
168 | rc = CMD_RET_FAILURE; |
169 | goto cleanup_ums_init; | |
16297cfb | 170 | } |
b528f713 | 171 | |
f032260c | 172 | rc = fsg_init(ums, ums_count, udc); |
b528f713 | 173 | if (rc) { |
71002b50 | 174 | pr_err("fsg_init failed\n"); |
02585eb3 SW |
175 | rc = CMD_RET_FAILURE; |
176 | goto cleanup_board; | |
b528f713 ŁM |
177 | } |
178 | ||
66b88b07 SW |
179 | rc = g_dnl_register("usb_dnl_ums"); |
180 | if (rc) { | |
71002b50 | 181 | pr_err("g_dnl_register failed\n"); |
02585eb3 SW |
182 | rc = CMD_RET_FAILURE; |
183 | goto cleanup_board; | |
66b88b07 | 184 | } |
b528f713 | 185 | |
3603e31d | 186 | /* Timeout unit: seconds */ |
1725f128 | 187 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
3603e31d | 188 | |
75504e95 MZ |
189 | if (!g_dnl_board_usb_cable_connected()) { |
190 | /* | |
191 | * Won't execute if we don't know whether the cable is | |
192 | * connected. | |
193 | */ | |
3603e31d PM |
194 | puts("Please connect USB cable.\n"); |
195 | ||
75504e95 | 196 | while (!g_dnl_board_usb_cable_connected()) { |
3603e31d PM |
197 | if (ctrlc()) { |
198 | puts("\rCTRL+C - Operation aborted.\n"); | |
02585eb3 SW |
199 | rc = CMD_RET_SUCCESS; |
200 | goto cleanup_register; | |
3603e31d PM |
201 | } |
202 | if (!cable_ready_timeout) { | |
203 | puts("\rUSB cable not detected.\n" \ | |
204 | "Command exit.\n"); | |
02585eb3 SW |
205 | rc = CMD_RET_SUCCESS; |
206 | goto cleanup_register; | |
3603e31d PM |
207 | } |
208 | ||
209 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); | |
210 | mdelay(1000); | |
211 | cable_ready_timeout--; | |
212 | } | |
213 | puts("\r\n"); | |
214 | } | |
215 | ||
b528f713 | 216 | while (1) { |
f032260c | 217 | dm_usb_gadget_handle_interrupts(udc); |
351e9b20 PM |
218 | |
219 | rc = fsg_main_thread(NULL); | |
220 | if (rc) { | |
221 | /* Check I/O error */ | |
222 | if (rc == -EIO) | |
223 | printf("\rCheck USB cable connection\n"); | |
224 | ||
225 | /* Check CTRL+C */ | |
226 | if (rc == -EPIPE) | |
227 | printf("\rCTRL+C - Operation aborted\n"); | |
228 | ||
02585eb3 SW |
229 | rc = CMD_RET_SUCCESS; |
230 | goto cleanup_register; | |
351e9b20 | 231 | } |
85d0aec0 | 232 | |
a57adacf SR |
233 | if (IS_ENABLED(CONFIG_CMD_UMS_ABORT_KEYED)) { |
234 | /* Abort by pressing any key */ | |
235 | if (tstc()) { | |
236 | getchar(); | |
237 | printf("\rOperation aborted.\n"); | |
238 | rc = CMD_RET_SUCCESS; | |
239 | goto cleanup_register; | |
240 | } | |
241 | } | |
242 | ||
29caf930 | 243 | schedule(); |
b528f713 | 244 | } |
02585eb3 SW |
245 | |
246 | cleanup_register: | |
b528f713 | 247 | g_dnl_unregister(); |
02585eb3 | 248 | cleanup_board: |
f032260c | 249 | udc_device_put(udc); |
02585eb3 SW |
250 | cleanup_ums_init: |
251 | ums_fini(); | |
252 | ||
253 | return rc; | |
b528f713 ŁM |
254 | } |
255 | ||
8c600456 | 256 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
ee02a65e | 257 | "Use the UMS [USB Mass Storage]", |
a2e3a1d8 | 258 | "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n" |
8c600456 | 259 | " devtype defaults to mmc" |
b528f713 | 260 | ); |