Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics |
| 3 | * Lukasz Majewski <l.majewski@samsung.com> |
| 4 | * |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 5 | * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved. |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 13 | #include <console.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 14 | #include <g_dnl.h> |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 15 | #include <part.h> |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 16 | #include <usb.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 17 | #include <usb_mass_storage.h> |
| 18 | |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 19 | static int ums_read_sector(struct ums *ums_dev, |
| 20 | ulong start, lbaint_t blkcnt, void *buf) |
| 21 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 22 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 23 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 24 | |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 25 | return block_dev->block_read(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static int ums_write_sector(struct ums *ums_dev, |
| 29 | ulong start, lbaint_t blkcnt, const void *buf) |
| 30 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 31 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 32 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 33 | |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 34 | return block_dev->block_write(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 35 | } |
| 36 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 37 | static struct ums *ums; |
| 38 | static int ums_count; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 39 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 40 | static void ums_fini(void) |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 41 | { |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < ums_count; i++) |
| 45 | free((void *)ums[i].name); |
| 46 | free(ums); |
| 47 | ums = 0; |
| 48 | ums_count = 0; |
| 49 | } |
| 50 | |
| 51 | #define UMS_NAME_LEN 16 |
| 52 | |
| 53 | static int ums_init(const char *devtype, const char *devnums) |
| 54 | { |
| 55 | char *s, *t, *devnum, *name; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 56 | struct blk_desc *block_dev; |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 57 | int ret; |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 58 | struct ums *ums_new; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 59 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 60 | s = strdup(devnums); |
| 61 | if (!s) |
| 62 | return -1; |
| 63 | |
| 64 | t = s; |
| 65 | ums_count = 0; |
| 66 | |
| 67 | for (;;) { |
| 68 | devnum = strsep(&t, ","); |
| 69 | if (!devnum) |
| 70 | break; |
| 71 | |
Simon Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 72 | ret = blk_get_device_by_str(devtype, devnum, &block_dev); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 73 | if (ret < 0) |
| 74 | goto cleanup; |
| 75 | |
| 76 | /* f_mass_storage.c assumes SECTOR_SIZE sectors */ |
| 77 | if (block_dev->blksz != SECTOR_SIZE) { |
| 78 | ret = -1; |
| 79 | goto cleanup; |
| 80 | } |
| 81 | |
| 82 | ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums)); |
| 83 | if (!ums_new) { |
| 84 | ret = -1; |
| 85 | goto cleanup; |
| 86 | } |
| 87 | ums = ums_new; |
| 88 | |
| 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); |
| 94 | if (!name) { |
| 95 | ret = -1; |
| 96 | goto cleanup; |
| 97 | } |
| 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; |
| 101 | |
| 102 | printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 103 | ums_count, ums[ums_count].block_dev.devnum, |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 104 | ums[ums_count].block_dev.hwpart, |
| 105 | ums[ums_count].start_sector, |
| 106 | ums[ums_count].num_sectors); |
| 107 | |
| 108 | ums_count++; |
| 109 | } |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 110 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 111 | if (!ums_count) |
| 112 | ret = -1; |
| 113 | else |
| 114 | ret = 0; |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 115 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 116 | cleanup: |
| 117 | free(s); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 118 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 119 | if (ret < 0) |
| 120 | ums_fini(); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 121 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 122 | return ret; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 123 | } |
| 124 | |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 125 | int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, |
| 126 | int argc, char * const argv[]) |
| 127 | { |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 128 | const char *usb_controller; |
| 129 | const char *devtype; |
| 130 | const char *devnum; |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 131 | unsigned int controller_index; |
| 132 | int rc; |
| 133 | int cable_ready_timeout __maybe_unused; |
| 134 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 135 | if (argc < 3) |
| 136 | return CMD_RET_USAGE; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 137 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 138 | usb_controller = argv[1]; |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 139 | if (argc >= 4) { |
| 140 | devtype = argv[2]; |
| 141 | devnum = argv[3]; |
| 142 | } else { |
| 143 | devtype = "mmc"; |
| 144 | devnum = argv[2]; |
| 145 | } |
Przemyslaw Marczak | 0e1e709 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 146 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 147 | rc = ums_init(devtype, devnum); |
| 148 | if (rc < 0) |
Przemyslaw Marczak | 0e1e709 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 149 | return CMD_RET_FAILURE; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 150 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 151 | controller_index = (unsigned int)(simple_strtoul( |
| 152 | usb_controller, NULL, 0)); |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 153 | if (board_usb_init(controller_index, USB_INIT_DEVICE)) { |
| 154 | error("Couldn't init USB controller."); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 155 | rc = CMD_RET_FAILURE; |
| 156 | goto cleanup_ums_init; |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 157 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 158 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 159 | rc = fsg_init(ums, ums_count); |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 160 | if (rc) { |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 161 | error("fsg_init failed"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 162 | rc = CMD_RET_FAILURE; |
| 163 | goto cleanup_board; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Stephen Warren | c611a6b | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 166 | rc = g_dnl_register("usb_dnl_ums"); |
| 167 | if (rc) { |
| 168 | error("g_dnl_register failed"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 169 | rc = CMD_RET_FAILURE; |
| 170 | goto cleanup_board; |
Stephen Warren | c611a6b | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 171 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 172 | |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 173 | /* Timeout unit: seconds */ |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 174 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 175 | |
Mateusz Zalega | 21fe3f7 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 176 | if (!g_dnl_board_usb_cable_connected()) { |
| 177 | /* |
| 178 | * Won't execute if we don't know whether the cable is |
| 179 | * connected. |
| 180 | */ |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 181 | puts("Please connect USB cable.\n"); |
| 182 | |
Mateusz Zalega | 21fe3f7 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 183 | while (!g_dnl_board_usb_cable_connected()) { |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 184 | if (ctrlc()) { |
| 185 | puts("\rCTRL+C - Operation aborted.\n"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 186 | rc = CMD_RET_SUCCESS; |
| 187 | goto cleanup_register; |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 188 | } |
| 189 | if (!cable_ready_timeout) { |
| 190 | puts("\rUSB cable not detected.\n" \ |
| 191 | "Command exit.\n"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 192 | rc = CMD_RET_SUCCESS; |
| 193 | goto cleanup_register; |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); |
| 197 | mdelay(1000); |
| 198 | cable_ready_timeout--; |
| 199 | } |
| 200 | puts("\r\n"); |
| 201 | } |
| 202 | |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 203 | while (1) { |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 204 | usb_gadget_handle_interrupts(controller_index); |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 205 | |
| 206 | rc = fsg_main_thread(NULL); |
| 207 | if (rc) { |
| 208 | /* Check I/O error */ |
| 209 | if (rc == -EIO) |
| 210 | printf("\rCheck USB cable connection\n"); |
| 211 | |
| 212 | /* Check CTRL+C */ |
| 213 | if (rc == -EPIPE) |
| 214 | printf("\rCTRL+C - Operation aborted\n"); |
| 215 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 216 | rc = CMD_RET_SUCCESS; |
| 217 | goto cleanup_register; |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 218 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 219 | } |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 220 | |
| 221 | cleanup_register: |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 222 | g_dnl_unregister(); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 223 | cleanup_board: |
Inha Song | 3ab2d77 | 2015-03-03 17:32:05 +0100 | [diff] [blame] | 224 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 225 | cleanup_ums_init: |
| 226 | ums_fini(); |
| 227 | |
| 228 | return rc; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 231 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
Fabio Estevam | 726c123 | 2015-02-28 15:11:46 -0300 | [diff] [blame] | 232 | "Use the UMS [USB Mass Storage]", |
Fabio Estevam | dec5dd6 | 2015-02-25 13:03:19 -0300 | [diff] [blame] | 233 | "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n" |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 234 | " devtype defaults to mmc" |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 235 | ); |