1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
6 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
18 #include <usb_mass_storage.h>
21 static int ums_read_sector(struct ums
*ums_dev
,
22 ulong start
, lbaint_t blkcnt
, void *buf
)
24 struct blk_desc
*block_dev
= &ums_dev
->block_dev
;
25 lbaint_t blkstart
= start
+ ums_dev
->start_sector
;
27 return blk_dread(block_dev
, blkstart
, blkcnt
, buf
);
30 static int ums_write_sector(struct ums
*ums_dev
,
31 ulong start
, lbaint_t blkcnt
, const void *buf
)
33 struct blk_desc
*block_dev
= &ums_dev
->block_dev
;
34 lbaint_t blkstart
= start
+ ums_dev
->start_sector
;
36 return blk_dwrite(block_dev
, blkstart
, blkcnt
, buf
);
39 static struct ums
*ums
;
42 static void ums_fini(void)
46 for (i
= 0; i
< ums_count
; i
++)
47 free((void *)ums
[i
].name
);
53 #define UMS_NAME_LEN 16
55 static int ums_init(const char *devtype
, const char *devnums_part_str
)
57 char *s
, *t
, *devnum_part_str
, *name
;
58 struct blk_desc
*block_dev
;
59 struct disk_partition info
;
64 s
= strdup(devnums_part_str
);
72 devnum_part_str
= strsep(&t
, ",");
76 partnum
= blk_get_device_part_str(devtype
, devnum_part_str
,
77 &block_dev
, &info
, 1);
82 /* Check if the argument is in legacy format. If yes,
83 * expose all partitions by setting the partnum = 0
86 if (!strchr(devnum_part_str
, ':'))
89 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
90 if (block_dev
->blksz
!= SECTOR_SIZE
)
93 ums_new
= realloc(ums
, (ums_count
+ 1) * sizeof(*ums
));
98 /* if partnum = 0, expose all partitions */
100 ums
[ums_count
].start_sector
= 0;
101 ums
[ums_count
].num_sectors
= block_dev
->lba
;
103 ums
[ums_count
].start_sector
= info
.start
;
104 ums
[ums_count
].num_sectors
= info
.size
;
107 ums
[ums_count
].read_sector
= ums_read_sector
;
108 ums
[ums_count
].write_sector
= ums_write_sector
;
110 name
= malloc(UMS_NAME_LEN
);
113 snprintf(name
, UMS_NAME_LEN
, "UMS disk %d", ums_count
);
114 ums
[ums_count
].name
= name
;
115 ums
[ums_count
].block_dev
= *block_dev
;
117 printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n",
118 ums_count
, ums
[ums_count
].block_dev
.devnum
,
119 ums
[ums_count
].block_dev
.hwpart
,
120 ums
[ums_count
].start_sector
,
121 ums
[ums_count
].num_sectors
);
138 static int do_usb_mass_storage(struct cmd_tbl
*cmdtp
, int flag
,
139 int argc
, char *const argv
[])
141 const char *usb_controller
;
144 unsigned int controller_index
;
146 int cable_ready_timeout __maybe_unused
;
149 return CMD_RET_USAGE
;
151 usb_controller
= argv
[1];
160 rc
= ums_init(devtype
, devnum
);
162 return CMD_RET_FAILURE
;
164 controller_index
= (unsigned int)(simple_strtoul(
165 usb_controller
, NULL
, 0));
166 if (usb_gadget_initialize(controller_index
)) {
167 pr_err("Couldn't init USB controller.\n");
168 rc
= CMD_RET_FAILURE
;
169 goto cleanup_ums_init
;
172 rc
= fsg_init(ums
, ums_count
);
174 pr_err("fsg_init failed\n");
175 rc
= CMD_RET_FAILURE
;
179 rc
= g_dnl_register("usb_dnl_ums");
181 pr_err("g_dnl_register failed\n");
182 rc
= CMD_RET_FAILURE
;
186 /* Timeout unit: seconds */
187 cable_ready_timeout
= UMS_CABLE_READY_TIMEOUT
;
189 if (!g_dnl_board_usb_cable_connected()) {
191 * Won't execute if we don't know whether the cable is
194 puts("Please connect USB cable.\n");
196 while (!g_dnl_board_usb_cable_connected()) {
198 puts("\rCTRL+C - Operation aborted.\n");
199 rc
= CMD_RET_SUCCESS
;
200 goto cleanup_register
;
202 if (!cable_ready_timeout
) {
203 puts("\rUSB cable not detected.\n" \
205 rc
= CMD_RET_SUCCESS
;
206 goto cleanup_register
;
209 printf("\rAuto exit in: %.2d s.", cable_ready_timeout
);
211 cable_ready_timeout
--;
217 usb_gadget_handle_interrupts(controller_index
);
219 rc
= fsg_main_thread(NULL
);
221 /* Check I/O error */
223 printf("\rCheck USB cable connection\n");
227 printf("\rCTRL+C - Operation aborted\n");
229 rc
= CMD_RET_SUCCESS
;
230 goto cleanup_register
;
239 usb_gadget_release(controller_index
);
246 U_BOOT_CMD(ums
, 4, 1, do_usb_mass_storage
,
247 "Use the UMS [USB Mass Storage]",
248 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
249 " devtype defaults to mmc"