blob: 041559172d9560407df9249ef5de3d57ff1125e1 [file] [log] [blame]
Lukasz Majewskib528f712013-03-05 12:10:17 +01001/*
2 * Copyright (C) 2011 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
4 *
Stephen Warren02585eb2015-12-07 11:38:50 -07005 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskib528f712013-03-05 12:10:17 +01008 */
9
Przemyslaw Marczak351e9b22013-10-23 14:30:46 +020010#include <errno.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010011#include <common.h>
12#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070013#include <console.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010014#include <g_dnl.h>
Stephen Warrenabfe8af2014-05-05 10:40:15 -060015#include <part.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020016#include <usb.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010017#include <usb_mass_storage.h>
18
Stephen Warrenabfe8af2014-05-05 10:40:15 -060019static int ums_read_sector(struct ums *ums_dev,
20 ulong start, lbaint_t blkcnt, void *buf)
21{
Stephen Warren02585eb2015-12-07 11:38:50 -070022 block_dev_desc_t *block_dev = &ums_dev->block_dev;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060023 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060024
Stephen Warren7c4213f2015-12-07 11:38:48 -070025 return block_dev->block_read(block_dev, blkstart, blkcnt, buf);
Stephen Warrenabfe8af2014-05-05 10:40:15 -060026}
27
28static int ums_write_sector(struct ums *ums_dev,
29 ulong start, lbaint_t blkcnt, const void *buf)
30{
Stephen Warren02585eb2015-12-07 11:38:50 -070031 block_dev_desc_t *block_dev = &ums_dev->block_dev;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060032 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060033
Stephen Warren7c4213f2015-12-07 11:38:48 -070034 return block_dev->block_write(block_dev, blkstart, blkcnt, buf);
Stephen Warrenabfe8af2014-05-05 10:40:15 -060035}
36
Stephen Warren02585eb2015-12-07 11:38:50 -070037static struct ums *ums;
38static int ums_count;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060039
Stephen Warren02585eb2015-12-07 11:38:50 -070040static void ums_fini(void)
Stephen Warrenabfe8af2014-05-05 10:40:15 -060041{
Stephen Warren02585eb2015-12-07 11:38:50 -070042 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
53static int ums_init(const char *devtype, const char *devnums)
54{
55 char *s, *t, *devnum, *name;
Stephen Warrend0cc4562014-05-05 10:40:16 -060056 block_dev_desc_t *block_dev;
57 int ret;
Stephen Warren02585eb2015-12-07 11:38:50 -070058 struct ums *ums_new;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060059
Stephen Warren02585eb2015-12-07 11:38:50 -070060 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
72 ret = get_device(devtype, devnum, &block_dev);
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",
103 ums_count, ums[ums_count].block_dev.dev,
104 ums[ums_count].block_dev.hwpart,
105 ums[ums_count].start_sector,
106 ums[ums_count].num_sectors);
107
108 ums_count++;
109 }
110
111 if (!ums_count)
112 ret = -1;
113 else
114 ret = 0;
115
116cleanup:
117 free(s);
118
Stephen Warrend0cc4562014-05-05 10:40:16 -0600119 if (ret < 0)
Stephen Warren02585eb2015-12-07 11:38:50 -0700120 ums_fini();
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600121
Stephen Warren02585eb2015-12-07 11:38:50 -0700122 return ret;
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600123}
124
Lukasz Majewskib528f712013-03-05 12:10:17 +0100125int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
126 int argc, char * const argv[])
127{
Stephen Warren1725f122014-05-05 10:40:17 -0600128 const char *usb_controller;
129 const char *devtype;
130 const char *devnum;
Stephen Warren1725f122014-05-05 10:40:17 -0600131 unsigned int controller_index;
132 int rc;
133 int cable_ready_timeout __maybe_unused;
134
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200135 if (argc < 3)
136 return CMD_RET_USAGE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100137
Stephen Warren1725f122014-05-05 10:40:17 -0600138 usb_controller = argv[1];
Stephen Warren8c600452014-05-05 10:40:18 -0600139 if (argc >= 4) {
140 devtype = argv[2];
141 devnum = argv[3];
142 } else {
143 devtype = "mmc";
144 devnum = argv[2];
145 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100146
Stephen Warren02585eb2015-12-07 11:38:50 -0700147 rc = ums_init(devtype, devnum);
148 if (rc < 0)
Przemyslaw Marczakf4dacf72013-10-23 14:30:43 +0200149 return CMD_RET_FAILURE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100150
Stephen Warren1725f122014-05-05 10:40:17 -0600151 controller_index = (unsigned int)(simple_strtoul(
152 usb_controller, NULL, 0));
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200153 if (board_usb_init(controller_index, USB_INIT_DEVICE)) {
154 error("Couldn't init USB controller.");
Stephen Warren02585eb2015-12-07 11:38:50 -0700155 rc = CMD_RET_FAILURE;
156 goto cleanup_ums_init;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200157 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100158
Stephen Warren02585eb2015-12-07 11:38:50 -0700159 rc = fsg_init(ums, ums_count);
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200160 if (rc) {
161 error("fsg_init failed");
Stephen Warren02585eb2015-12-07 11:38:50 -0700162 rc = CMD_RET_FAILURE;
163 goto cleanup_board;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200164 }
165
Stephen Warren66b88b02014-05-01 15:42:10 -0600166 rc = g_dnl_register("usb_dnl_ums");
167 if (rc) {
168 error("g_dnl_register failed");
Stephen Warren02585eb2015-12-07 11:38:50 -0700169 rc = CMD_RET_FAILURE;
170 goto cleanup_board;
Stephen Warren66b88b02014-05-01 15:42:10 -0600171 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100172
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100173 /* Timeout unit: seconds */
Stephen Warren1725f122014-05-05 10:40:17 -0600174 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100175
Mateusz Zalega75504e92014-04-30 13:07:48 +0200176 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 Marczak3603e312014-01-07 15:08:37 +0100181 puts("Please connect USB cable.\n");
182
Mateusz Zalega75504e92014-04-30 13:07:48 +0200183 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100184 if (ctrlc()) {
185 puts("\rCTRL+C - Operation aborted.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700186 rc = CMD_RET_SUCCESS;
187 goto cleanup_register;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100188 }
189 if (!cable_ready_timeout) {
190 puts("\rUSB cable not detected.\n" \
191 "Command exit.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700192 rc = CMD_RET_SUCCESS;
193 goto cleanup_register;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100194 }
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 Majewskib528f712013-03-05 12:10:17 +0100203 while (1) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +0530204 usb_gadget_handle_interrupts(controller_index);
Przemyslaw Marczak351e9b22013-10-23 14:30:46 +0200205
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 Warren02585eb2015-12-07 11:38:50 -0700216 rc = CMD_RET_SUCCESS;
217 goto cleanup_register;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100218 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100219 }
Stephen Warren02585eb2015-12-07 11:38:50 -0700220
221cleanup_register:
Lukasz Majewskib528f712013-03-05 12:10:17 +0100222 g_dnl_unregister();
Stephen Warren02585eb2015-12-07 11:38:50 -0700223cleanup_board:
Inha Song375f2d72015-03-03 17:32:05 +0100224 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Stephen Warren02585eb2015-12-07 11:38:50 -0700225cleanup_ums_init:
226 ums_fini();
227
228 return rc;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100229}
230
Stephen Warren8c600452014-05-05 10:40:18 -0600231U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevamee02a652015-02-28 15:11:46 -0300232 "Use the UMS [USB Mass Storage]",
Fabio Estevame5d3e7f2015-02-25 13:03:19 -0300233 "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n"
Stephen Warren8c600452014-05-05 10:40:18 -0600234 " devtype defaults to mmc"
Lukasz Majewskib528f712013-03-05 12:10:17 +0100235);