2 * Copyright (C) 2011 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
5 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
7 * SPDX-License-Identifier: GPL-2.0+
17 #include <usb_mass_storage.h>
19 static int ums_read_sector(struct ums
*ums_dev
,
20 ulong start
, lbaint_t blkcnt
, void *buf
)
22 block_dev_desc_t
*block_dev
= &ums_dev
->block_dev
;
23 lbaint_t blkstart
= start
+ ums_dev
->start_sector
;
25 return block_dev
->block_read(block_dev
, blkstart
, blkcnt
, buf
);
28 static int ums_write_sector(struct ums
*ums_dev
,
29 ulong start
, lbaint_t blkcnt
, const void *buf
)
31 block_dev_desc_t
*block_dev
= &ums_dev
->block_dev
;
32 lbaint_t blkstart
= start
+ ums_dev
->start_sector
;
34 return block_dev
->block_write(block_dev
, blkstart
, blkcnt
, buf
);
37 static struct ums
*ums
;
40 static void ums_fini(void)
44 for (i
= 0; i
< ums_count
; i
++)
45 free((void *)ums
[i
].name
);
51 #define UMS_NAME_LEN 16
53 static int ums_init(const char *devtype
, const char *devnums
)
55 char *s
, *t
, *devnum
, *name
;
56 block_dev_desc_t
*block_dev
;
68 devnum
= strsep(&t
, ",");
72 ret
= get_device(devtype
, devnum
, &block_dev
);
76 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
77 if (block_dev
->blksz
!= SECTOR_SIZE
) {
82 ums_new
= realloc(ums
, (ums_count
+ 1) * sizeof(*ums
));
89 ums
[ums_count
].read_sector
= ums_read_sector
;
90 ums
[ums_count
].write_sector
= ums_write_sector
;
91 ums
[ums_count
].start_sector
= 0;
92 ums
[ums_count
].num_sectors
= block_dev
->lba
;
93 name
= malloc(UMS_NAME_LEN
);
98 snprintf(name
, UMS_NAME_LEN
, "UMS disk %d", ums_count
);
99 ums
[ums_count
].name
= name
;
100 ums
[ums_count
].block_dev
= *block_dev
;
102 printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n",
103 ums_count
, ums
[ums_count
].block_dev
.dev
,
104 ums
[ums_count
].block_dev
.hwpart
,
105 ums
[ums_count
].start_sector
,
106 ums
[ums_count
].num_sectors
);
125 int do_usb_mass_storage(cmd_tbl_t
*cmdtp
, int flag
,
126 int argc
, char * const argv
[])
128 const char *usb_controller
;
131 unsigned int controller_index
;
133 int cable_ready_timeout __maybe_unused
;
136 return CMD_RET_USAGE
;
138 usb_controller
= argv
[1];
147 rc
= ums_init(devtype
, devnum
);
149 return CMD_RET_FAILURE
;
151 controller_index
= (unsigned int)(simple_strtoul(
152 usb_controller
, NULL
, 0));
153 if (board_usb_init(controller_index
, USB_INIT_DEVICE
)) {
154 error("Couldn't init USB controller.");
155 rc
= CMD_RET_FAILURE
;
156 goto cleanup_ums_init
;
159 rc
= fsg_init(ums
, ums_count
);
161 error("fsg_init failed");
162 rc
= CMD_RET_FAILURE
;
166 rc
= g_dnl_register("usb_dnl_ums");
168 error("g_dnl_register failed");
169 rc
= CMD_RET_FAILURE
;
173 /* Timeout unit: seconds */
174 cable_ready_timeout
= UMS_CABLE_READY_TIMEOUT
;
176 if (!g_dnl_board_usb_cable_connected()) {
178 * Won't execute if we don't know whether the cable is
181 puts("Please connect USB cable.\n");
183 while (!g_dnl_board_usb_cable_connected()) {
185 puts("\rCTRL+C - Operation aborted.\n");
186 rc
= CMD_RET_SUCCESS
;
187 goto cleanup_register
;
189 if (!cable_ready_timeout
) {
190 puts("\rUSB cable not detected.\n" \
192 rc
= CMD_RET_SUCCESS
;
193 goto cleanup_register
;
196 printf("\rAuto exit in: %.2d s.", cable_ready_timeout
);
198 cable_ready_timeout
--;
204 usb_gadget_handle_interrupts(controller_index
);
206 rc
= fsg_main_thread(NULL
);
208 /* Check I/O error */
210 printf("\rCheck USB cable connection\n");
214 printf("\rCTRL+C - Operation aborted\n");
216 rc
= CMD_RET_SUCCESS
;
217 goto cleanup_register
;
224 board_usb_cleanup(controller_index
, USB_INIT_DEVICE
);
231 U_BOOT_CMD(ums
, 4, 1, do_usb_mass_storage
,
232 "Use the UMS [USB Mass Storage]",
233 "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n"
234 " devtype defaults to mmc"