Blame view
common/cmd_usb_mass_storage.c
3.52 KB
b528f7139
|
1 2 3 4 |
/* * Copyright (C) 2011 Samsung Electronics * Lukasz Majewski <l.majewski@samsung.com> * |
1a4596601
|
5 |
* SPDX-License-Identifier: GPL-2.0+ |
b528f7139
|
6 |
*/ |
351e9b206
|
7 |
#include <errno.h> |
b528f7139
|
8 9 10 |
#include <common.h> #include <command.h> #include <g_dnl.h> |
abfe8afe8
|
11 |
#include <part.h> |
16297cfb2
|
12 |
#include <usb.h> |
b528f7139
|
13 |
#include <usb_mass_storage.h> |
abfe8afe8
|
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
static int ums_read_sector(struct ums *ums_dev, ulong start, lbaint_t blkcnt, void *buf) { block_dev_desc_t *block_dev = ums_dev->block_dev; lbaint_t blkstart = start + ums_dev->start_sector; int dev_num = block_dev->dev; return block_dev->block_read(dev_num, blkstart, blkcnt, buf); } static int ums_write_sector(struct ums *ums_dev, ulong start, lbaint_t blkcnt, const void *buf) { block_dev_desc_t *block_dev = ums_dev->block_dev; lbaint_t blkstart = start + ums_dev->start_sector; int dev_num = block_dev->dev; return block_dev->block_write(dev_num, blkstart, blkcnt, buf); } static struct ums ums_dev = { .read_sector = ums_read_sector, .write_sector = ums_write_sector, .name = "UMS disk", }; |
d0cc456dc
|
39 |
struct ums *ums_init(const char *devtype, const char *devnum) |
abfe8afe8
|
40 |
{ |
d0cc456dc
|
41 42 |
block_dev_desc_t *block_dev; int ret; |
abfe8afe8
|
43 |
|
d0cc456dc
|
44 45 |
ret = get_device(devtype, devnum, &block_dev); if (ret < 0) |
abfe8afe8
|
46 |
return NULL; |
d0cc456dc
|
47 48 49 50 51 |
/* f_mass_storage.c assumes SECTOR_SIZE sectors */ if (block_dev->blksz != SECTOR_SIZE) return NULL; ums_dev.block_dev = block_dev; |
abfe8afe8
|
52 |
ums_dev.start_sector = 0; |
d0cc456dc
|
53 |
ums_dev.num_sectors = block_dev->lba; |
abfe8afe8
|
54 55 56 57 58 59 60 |
printf("UMS: disk start sector: %#x, count: %#x ", ums_dev.start_sector, ums_dev.num_sectors); return &ums_dev; } |
b528f7139
|
61 62 63 |
int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { |
1725f1288
|
64 65 66 67 68 69 70 |
const char *usb_controller; const char *devtype; const char *devnum; struct ums *ums; unsigned int controller_index; int rc; int cable_ready_timeout __maybe_unused; |
16297cfb2
|
71 72 |
if (argc < 3) return CMD_RET_USAGE; |
b528f7139
|
73 |
|
1725f1288
|
74 |
usb_controller = argv[1]; |
8c6004568
|
75 76 77 78 79 80 81 |
if (argc >= 4) { devtype = argv[2]; devnum = argv[3]; } else { devtype = "mmc"; devnum = argv[2]; } |
f4dacf7b9
|
82 |
|
1725f1288
|
83 |
ums = ums_init(devtype, devnum); |
f4dacf7b9
|
84 85 |
if (!ums) return CMD_RET_FAILURE; |
b528f7139
|
86 |
|
1725f1288
|
87 88 |
controller_index = (unsigned int)(simple_strtoul( usb_controller, NULL, 0)); |
16297cfb2
|
89 90 |
if (board_usb_init(controller_index, USB_INIT_DEVICE)) { error("Couldn't init USB controller."); |
93c813b3a
|
91 |
return CMD_RET_FAILURE; |
16297cfb2
|
92 |
} |
b528f7139
|
93 |
|
1725f1288
|
94 |
rc = fsg_init(ums); |
b528f7139
|
95 |
if (rc) { |
16297cfb2
|
96 |
error("fsg_init failed"); |
93c813b3a
|
97 |
return CMD_RET_FAILURE; |
b528f7139
|
98 |
} |
66b88b07c
|
99 100 101 102 103 |
rc = g_dnl_register("usb_dnl_ums"); if (rc) { error("g_dnl_register failed"); return CMD_RET_FAILURE; } |
b528f7139
|
104 |
|
3603e31db
|
105 |
/* Timeout unit: seconds */ |
1725f1288
|
106 |
cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
3603e31db
|
107 |
|
75504e959
|
108 109 110 111 112 |
if (!g_dnl_board_usb_cable_connected()) { /* * Won't execute if we don't know whether the cable is * connected. */ |
3603e31db
|
113 114 |
puts("Please connect USB cable. "); |
75504e959
|
115 |
while (!g_dnl_board_usb_cable_connected()) { |
3603e31db
|
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
if (ctrlc()) { puts("\rCTRL+C - Operation aborted. "); goto exit; } if (!cable_ready_timeout) { puts("\rUSB cable not detected. " \ "Command exit. "); goto exit; } printf("\rAuto exit in: %.2d s.", cable_ready_timeout); mdelay(1000); cable_ready_timeout--; } puts("\r "); } |
b528f7139
|
136 |
while (1) { |
b528f7139
|
137 |
usb_gadget_handle_interrupts(); |
351e9b206
|
138 139 140 141 142 143 144 145 146 147 148 149 |
rc = fsg_main_thread(NULL); if (rc) { /* Check I/O error */ if (rc == -EIO) printf("\rCheck USB cable connection "); /* Check CTRL+C */ if (rc == -EPIPE) printf("\rCTRL+C - Operation aborted "); |
b528f7139
|
150 |
goto exit; |
351e9b206
|
151 |
} |
b528f7139
|
152 153 154 |
} exit: g_dnl_unregister(); |
93c813b3a
|
155 |
return CMD_RET_SUCCESS; |
b528f7139
|
156 |
} |
8c6004568
|
157 |
U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
b528f7139
|
158 |
"Use the UMS [User Mass Storage]", |
e5d3e7fcb
|
159 160 |
"<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0 " |
8c6004568
|
161 |
" devtype defaults to mmc" |
b528f7139
|
162 |
); |