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