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