blob: cb5260b558c4227b8e5e0018a7f4150e436db76f [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{
Simon Glass4101f682016-02-29 15:25:34 -070022 struct blk_desc *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
Simon Glassc9f3c5f2016-07-05 17:10:17 -060025 return blk_dread(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{
Simon Glass4101f682016-02-29 15:25:34 -070031 struct blk_desc *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
Simon Glassc9f3c5f2016-07-05 17:10:17 -060034 return blk_dwrite(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);
Fabio Estevamd4190262016-05-27 22:37:10 -030047 ums = NULL;
Stephen Warren02585eb2015-12-07 11:38:50 -070048 ums_count = 0;
49}
50
51#define UMS_NAME_LEN 16
52
John Tobiasa2e3a1d2016-04-13 12:06:51 -070053static int ums_init(const char *devtype, const char *devnums_part_str)
Stephen Warren02585eb2015-12-07 11:38:50 -070054{
John Tobiasa2e3a1d2016-04-13 12:06:51 -070055 char *s, *t, *devnum_part_str, *name;
Simon Glass4101f682016-02-29 15:25:34 -070056 struct blk_desc *block_dev;
John Tobiasa2e3a1d2016-04-13 12:06:51 -070057 disk_partition_t info;
58 int partnum;
Tom Rinia238b0d2016-04-20 10:59:32 -040059 int ret = -1;
Stephen Warren02585eb2015-12-07 11:38:50 -070060 struct ums *ums_new;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060061
John Tobiasa2e3a1d2016-04-13 12:06:51 -070062 s = strdup(devnums_part_str);
Stephen Warren02585eb2015-12-07 11:38:50 -070063 if (!s)
64 return -1;
65
66 t = s;
67 ums_count = 0;
68
69 for (;;) {
John Tobiasa2e3a1d2016-04-13 12:06:51 -070070 devnum_part_str = strsep(&t, ",");
71 if (!devnum_part_str)
Stephen Warren02585eb2015-12-07 11:38:50 -070072 break;
73
John Tobiasa2e3a1d2016-04-13 12:06:51 -070074 partnum = blk_get_device_part_str(devtype, devnum_part_str,
75 &block_dev, &info, 1);
76
77 if (partnum < 0)
Stephen Warren02585eb2015-12-07 11:38:50 -070078 goto cleanup;
79
John Tobiasa2e3a1d2016-04-13 12:06:51 -070080 /* Check if the argument is in legacy format. If yes,
81 * expose all partitions by setting the partnum = 0
82 * e.g. ums 0 mmc 0
83 */
84 if (!strchr(devnum_part_str, ':'))
85 partnum = 0;
86
Stephen Warren02585eb2015-12-07 11:38:50 -070087 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
Tom Rinia238b0d2016-04-20 10:59:32 -040088 if (block_dev->blksz != SECTOR_SIZE)
Stephen Warren02585eb2015-12-07 11:38:50 -070089 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -070090
91 ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
Tom Rinia238b0d2016-04-20 10:59:32 -040092 if (!ums_new)
Stephen Warren02585eb2015-12-07 11:38:50 -070093 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -070094 ums = ums_new;
95
John Tobiasa2e3a1d2016-04-13 12:06:51 -070096 /* if partnum = 0, expose all partitions */
97 if (partnum == 0) {
98 ums[ums_count].start_sector = 0;
99 ums[ums_count].num_sectors = block_dev->lba;
100 } else {
101 ums[ums_count].start_sector = info.start;
102 ums[ums_count].num_sectors = info.size;
103 }
104
Stephen Warren02585eb2015-12-07 11:38:50 -0700105 ums[ums_count].read_sector = ums_read_sector;
106 ums[ums_count].write_sector = ums_write_sector;
John Tobiasa2e3a1d2016-04-13 12:06:51 -0700107
Stephen Warren02585eb2015-12-07 11:38:50 -0700108 name = malloc(UMS_NAME_LEN);
Tom Rinia238b0d2016-04-20 10:59:32 -0400109 if (!name)
Stephen Warren02585eb2015-12-07 11:38:50 -0700110 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -0700111 snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count);
112 ums[ums_count].name = name;
113 ums[ums_count].block_dev = *block_dev;
114
115 printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700116 ums_count, ums[ums_count].block_dev.devnum,
Stephen Warren02585eb2015-12-07 11:38:50 -0700117 ums[ums_count].block_dev.hwpart,
118 ums[ums_count].start_sector,
119 ums[ums_count].num_sectors);
120
121 ums_count++;
122 }
123
Tom Rinia238b0d2016-04-20 10:59:32 -0400124 if (ums_count)
Stephen Warren02585eb2015-12-07 11:38:50 -0700125 ret = 0;
126
127cleanup:
128 free(s);
129
Stephen Warrend0cc4562014-05-05 10:40:16 -0600130 if (ret < 0)
Stephen Warren02585eb2015-12-07 11:38:50 -0700131 ums_fini();
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600132
Stephen Warren02585eb2015-12-07 11:38:50 -0700133 return ret;
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600134}
135
Lukasz Majewskib528f712013-03-05 12:10:17 +0100136int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
137 int argc, char * const argv[])
138{
Stephen Warren1725f122014-05-05 10:40:17 -0600139 const char *usb_controller;
140 const char *devtype;
141 const char *devnum;
Stephen Warren1725f122014-05-05 10:40:17 -0600142 unsigned int controller_index;
143 int rc;
144 int cable_ready_timeout __maybe_unused;
145
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200146 if (argc < 3)
147 return CMD_RET_USAGE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100148
Stephen Warren1725f122014-05-05 10:40:17 -0600149 usb_controller = argv[1];
Stephen Warren8c600452014-05-05 10:40:18 -0600150 if (argc >= 4) {
151 devtype = argv[2];
152 devnum = argv[3];
153 } else {
154 devtype = "mmc";
155 devnum = argv[2];
156 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100157
Stephen Warren02585eb2015-12-07 11:38:50 -0700158 rc = ums_init(devtype, devnum);
159 if (rc < 0)
Przemyslaw Marczakf4dacf72013-10-23 14:30:43 +0200160 return CMD_RET_FAILURE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100161
Stephen Warren1725f122014-05-05 10:40:17 -0600162 controller_index = (unsigned int)(simple_strtoul(
163 usb_controller, NULL, 0));
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200164 if (board_usb_init(controller_index, USB_INIT_DEVICE)) {
165 error("Couldn't init USB controller.");
Stephen Warren02585eb2015-12-07 11:38:50 -0700166 rc = CMD_RET_FAILURE;
167 goto cleanup_ums_init;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200168 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100169
Stephen Warren02585eb2015-12-07 11:38:50 -0700170 rc = fsg_init(ums, ums_count);
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200171 if (rc) {
172 error("fsg_init failed");
Stephen Warren02585eb2015-12-07 11:38:50 -0700173 rc = CMD_RET_FAILURE;
174 goto cleanup_board;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200175 }
176
Stephen Warren66b88b02014-05-01 15:42:10 -0600177 rc = g_dnl_register("usb_dnl_ums");
178 if (rc) {
179 error("g_dnl_register failed");
Stephen Warren02585eb2015-12-07 11:38:50 -0700180 rc = CMD_RET_FAILURE;
181 goto cleanup_board;
Stephen Warren66b88b02014-05-01 15:42:10 -0600182 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100183
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100184 /* Timeout unit: seconds */
Stephen Warren1725f122014-05-05 10:40:17 -0600185 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100186
Mateusz Zalega75504e92014-04-30 13:07:48 +0200187 if (!g_dnl_board_usb_cable_connected()) {
188 /*
189 * Won't execute if we don't know whether the cable is
190 * connected.
191 */
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100192 puts("Please connect USB cable.\n");
193
Mateusz Zalega75504e92014-04-30 13:07:48 +0200194 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100195 if (ctrlc()) {
196 puts("\rCTRL+C - Operation aborted.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700197 rc = CMD_RET_SUCCESS;
198 goto cleanup_register;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100199 }
200 if (!cable_ready_timeout) {
201 puts("\rUSB cable not detected.\n" \
202 "Command exit.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700203 rc = CMD_RET_SUCCESS;
204 goto cleanup_register;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100205 }
206
207 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
208 mdelay(1000);
209 cable_ready_timeout--;
210 }
211 puts("\r\n");
212 }
213
Lukasz Majewskib528f712013-03-05 12:10:17 +0100214 while (1) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +0530215 usb_gadget_handle_interrupts(controller_index);
Przemyslaw Marczak351e9b22013-10-23 14:30:46 +0200216
217 rc = fsg_main_thread(NULL);
218 if (rc) {
219 /* Check I/O error */
220 if (rc == -EIO)
221 printf("\rCheck USB cable connection\n");
222
223 /* Check CTRL+C */
224 if (rc == -EPIPE)
225 printf("\rCTRL+C - Operation aborted\n");
226
Stephen Warren02585eb2015-12-07 11:38:50 -0700227 rc = CMD_RET_SUCCESS;
228 goto cleanup_register;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100229 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100230 }
Stephen Warren02585eb2015-12-07 11:38:50 -0700231
232cleanup_register:
Lukasz Majewskib528f712013-03-05 12:10:17 +0100233 g_dnl_unregister();
Stephen Warren02585eb2015-12-07 11:38:50 -0700234cleanup_board:
Inha Song375f2d72015-03-03 17:32:05 +0100235 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Stephen Warren02585eb2015-12-07 11:38:50 -0700236cleanup_ums_init:
237 ums_fini();
238
239 return rc;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100240}
241
Stephen Warren8c600452014-05-05 10:40:18 -0600242U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevamee02a652015-02-28 15:11:46 -0300243 "Use the UMS [USB Mass Storage]",
John Tobiasa2e3a1d2016-04-13 12:06:51 -0700244 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
Stephen Warren8c600452014-05-05 10:40:18 -0600245 " devtype defaults to mmc"
Lukasz Majewskib528f712013-03-05 12:10:17 +0100246);