]>
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 | 79 | usb_controller = argv[1]; |
8c600456 SW |
80 | if (argc >= 4) { |
81 | devtype = argv[2]; | |
82 | devnum = argv[3]; | |
83 | } else { | |
84 | devtype = "mmc"; | |
85 | devnum = argv[2]; | |
86 | } | |
f4dacf7b | 87 | |
1725f128 | 88 | ums = ums_init(devtype, devnum); |
f4dacf7b PM |
89 | if (!ums) |
90 | return CMD_RET_FAILURE; | |
b528f713 | 91 | |
1725f128 SW |
92 | controller_index = (unsigned int)(simple_strtoul( |
93 | usb_controller, NULL, 0)); | |
16297cfb MZ |
94 | if (board_usb_init(controller_index, USB_INIT_DEVICE)) { |
95 | error("Couldn't init USB controller."); | |
93c813b3 | 96 | return CMD_RET_FAILURE; |
16297cfb | 97 | } |
b528f713 | 98 | |
1725f128 | 99 | rc = fsg_init(ums); |
b528f713 | 100 | if (rc) { |
16297cfb | 101 | error("fsg_init failed"); |
93c813b3 | 102 | return CMD_RET_FAILURE; |
b528f713 ŁM |
103 | } |
104 | ||
66b88b07 SW |
105 | rc = g_dnl_register("usb_dnl_ums"); |
106 | if (rc) { | |
107 | error("g_dnl_register failed"); | |
108 | return CMD_RET_FAILURE; | |
109 | } | |
b528f713 | 110 | |
3603e31d | 111 | /* Timeout unit: seconds */ |
1725f128 | 112 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
3603e31d | 113 | |
75504e95 MZ |
114 | if (!g_dnl_board_usb_cable_connected()) { |
115 | /* | |
116 | * Won't execute if we don't know whether the cable is | |
117 | * connected. | |
118 | */ | |
3603e31d PM |
119 | puts("Please connect USB cable.\n"); |
120 | ||
75504e95 | 121 | while (!g_dnl_board_usb_cable_connected()) { |
3603e31d PM |
122 | if (ctrlc()) { |
123 | puts("\rCTRL+C - Operation aborted.\n"); | |
124 | goto exit; | |
125 | } | |
126 | if (!cable_ready_timeout) { | |
127 | puts("\rUSB cable not detected.\n" \ | |
128 | "Command exit.\n"); | |
129 | goto exit; | |
130 | } | |
131 | ||
132 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); | |
133 | mdelay(1000); | |
134 | cable_ready_timeout--; | |
135 | } | |
136 | puts("\r\n"); | |
137 | } | |
138 | ||
b528f713 | 139 | while (1) { |
2d48aa69 | 140 | usb_gadget_handle_interrupts(controller_index); |
351e9b20 PM |
141 | |
142 | rc = fsg_main_thread(NULL); | |
143 | if (rc) { | |
144 | /* Check I/O error */ | |
145 | if (rc == -EIO) | |
146 | printf("\rCheck USB cable connection\n"); | |
147 | ||
148 | /* Check CTRL+C */ | |
149 | if (rc == -EPIPE) | |
150 | printf("\rCTRL+C - Operation aborted\n"); | |
151 | ||
b528f713 | 152 | goto exit; |
351e9b20 | 153 | } |
b528f713 ŁM |
154 | } |
155 | exit: | |
156 | g_dnl_unregister(); | |
375f2d78 | 157 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
93c813b3 | 158 | return CMD_RET_SUCCESS; |
b528f713 ŁM |
159 | } |
160 | ||
8c600456 | 161 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
ee02a65e | 162 | "Use the UMS [USB Mass Storage]", |
e5d3e7fc | 163 | "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n" |
8c600456 | 164 | " devtype defaults to mmc" |
b528f713 | 165 | ); |