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