blob: c5c6899787c84a746afb30317433ab9c779f39e8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01002/*
3 * Copyright (C) 2011 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
5 *
Stephen Warren9e7d5882015-12-07 11:38:50 -07006 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01007 */
8
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +02009#include <errno.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010010#include <common.h>
11#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010013#include <g_dnl.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Stephen Warren803876d2014-05-05 10:40:15 -060015#include <part.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020016#include <usb.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010017#include <usb_mass_storage.h>
Patrick Delaunay77498e62019-04-18 17:18:39 +020018#include <watchdog.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010019
Stephen Warren803876d2014-05-05 10:40:15 -060020static int ums_read_sector(struct ums *ums_dev,
21 ulong start, lbaint_t blkcnt, void *buf)
22{
Simon Glasse3394752016-02-29 15:25:34 -070023 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warren803876d2014-05-05 10:40:15 -060024 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warren803876d2014-05-05 10:40:15 -060025
Simon Glass5ee57962016-07-05 17:10:17 -060026 return blk_dread(block_dev, blkstart, blkcnt, buf);
Stephen Warren803876d2014-05-05 10:40:15 -060027}
28
29static int ums_write_sector(struct ums *ums_dev,
30 ulong start, lbaint_t blkcnt, const void *buf)
31{
Simon Glasse3394752016-02-29 15:25:34 -070032 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warren803876d2014-05-05 10:40:15 -060033 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warren803876d2014-05-05 10:40:15 -060034
Simon Glass5ee57962016-07-05 17:10:17 -060035 return blk_dwrite(block_dev, blkstart, blkcnt, buf);
Stephen Warren803876d2014-05-05 10:40:15 -060036}
37
Stephen Warren9e7d5882015-12-07 11:38:50 -070038static struct ums *ums;
39static int ums_count;
Stephen Warren803876d2014-05-05 10:40:15 -060040
Stephen Warren9e7d5882015-12-07 11:38:50 -070041static void ums_fini(void)
Stephen Warren803876d2014-05-05 10:40:15 -060042{
Stephen Warren9e7d5882015-12-07 11:38:50 -070043 int i;
44
45 for (i = 0; i < ums_count; i++)
46 free((void *)ums[i].name);
47 free(ums);
Fabio Estevam9b9c3f32016-05-27 22:37:10 -030048 ums = NULL;
Stephen Warren9e7d5882015-12-07 11:38:50 -070049 ums_count = 0;
50}
51
52#define UMS_NAME_LEN 16
53
John Tobias9fc92452016-04-13 12:06:51 -070054static int ums_init(const char *devtype, const char *devnums_part_str)
Stephen Warren9e7d5882015-12-07 11:38:50 -070055{
John Tobias9fc92452016-04-13 12:06:51 -070056 char *s, *t, *devnum_part_str, *name;
Simon Glasse3394752016-02-29 15:25:34 -070057 struct blk_desc *block_dev;
John Tobias9fc92452016-04-13 12:06:51 -070058 disk_partition_t info;
59 int partnum;
Tom Rinib55b6fe2016-04-20 10:59:32 -040060 int ret = -1;
Stephen Warren9e7d5882015-12-07 11:38:50 -070061 struct ums *ums_new;
Stephen Warren803876d2014-05-05 10:40:15 -060062
John Tobias9fc92452016-04-13 12:06:51 -070063 s = strdup(devnums_part_str);
Stephen Warren9e7d5882015-12-07 11:38:50 -070064 if (!s)
65 return -1;
66
67 t = s;
68 ums_count = 0;
69
70 for (;;) {
John Tobias9fc92452016-04-13 12:06:51 -070071 devnum_part_str = strsep(&t, ",");
72 if (!devnum_part_str)
Stephen Warren9e7d5882015-12-07 11:38:50 -070073 break;
74
John Tobias9fc92452016-04-13 12:06:51 -070075 partnum = blk_get_device_part_str(devtype, devnum_part_str,
76 &block_dev, &info, 1);
77
78 if (partnum < 0)
Stephen Warren9e7d5882015-12-07 11:38:50 -070079 goto cleanup;
80
John Tobias9fc92452016-04-13 12:06:51 -070081 /* Check if the argument is in legacy format. If yes,
82 * expose all partitions by setting the partnum = 0
83 * e.g. ums 0 mmc 0
84 */
85 if (!strchr(devnum_part_str, ':'))
86 partnum = 0;
87
Stephen Warren9e7d5882015-12-07 11:38:50 -070088 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
Tom Rinib55b6fe2016-04-20 10:59:32 -040089 if (block_dev->blksz != SECTOR_SIZE)
Stephen Warren9e7d5882015-12-07 11:38:50 -070090 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -070091
92 ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
Tom Rinib55b6fe2016-04-20 10:59:32 -040093 if (!ums_new)
Stephen Warren9e7d5882015-12-07 11:38:50 -070094 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -070095 ums = ums_new;
96
John Tobias9fc92452016-04-13 12:06:51 -070097 /* if partnum = 0, expose all partitions */
98 if (partnum == 0) {
99 ums[ums_count].start_sector = 0;
100 ums[ums_count].num_sectors = block_dev->lba;
101 } else {
102 ums[ums_count].start_sector = info.start;
103 ums[ums_count].num_sectors = info.size;
104 }
105
Stephen Warren9e7d5882015-12-07 11:38:50 -0700106 ums[ums_count].read_sector = ums_read_sector;
107 ums[ums_count].write_sector = ums_write_sector;
John Tobias9fc92452016-04-13 12:06:51 -0700108
Stephen Warren9e7d5882015-12-07 11:38:50 -0700109 name = malloc(UMS_NAME_LEN);
Tom Rinib55b6fe2016-04-20 10:59:32 -0400110 if (!name)
Stephen Warren9e7d5882015-12-07 11:38:50 -0700111 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -0700112 snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count);
113 ums[ums_count].name = name;
114 ums[ums_count].block_dev = *block_dev;
115
116 printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700117 ums_count, ums[ums_count].block_dev.devnum,
Stephen Warren9e7d5882015-12-07 11:38:50 -0700118 ums[ums_count].block_dev.hwpart,
119 ums[ums_count].start_sector,
120 ums[ums_count].num_sectors);
121
122 ums_count++;
123 }
Stephen Warren803876d2014-05-05 10:40:15 -0600124
Tom Rinib55b6fe2016-04-20 10:59:32 -0400125 if (ums_count)
Stephen Warren9e7d5882015-12-07 11:38:50 -0700126 ret = 0;
Stephen Warren5c7648b2014-05-05 10:40:16 -0600127
Stephen Warren9e7d5882015-12-07 11:38:50 -0700128cleanup:
129 free(s);
Stephen Warren803876d2014-05-05 10:40:15 -0600130
Stephen Warren9e7d5882015-12-07 11:38:50 -0700131 if (ret < 0)
132 ums_fini();
Stephen Warren803876d2014-05-05 10:40:15 -0600133
Stephen Warren9e7d5882015-12-07 11:38:50 -0700134 return ret;
Stephen Warren803876d2014-05-05 10:40:15 -0600135}
136
Fabio Estevam0ec32442016-05-27 22:37:11 -0300137static int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100138 int argc, char * const argv[])
139{
Stephen Warren5e8c2272014-05-05 10:40:17 -0600140 const char *usb_controller;
141 const char *devtype;
142 const char *devnum;
Stephen Warren5e8c2272014-05-05 10:40:17 -0600143 unsigned int controller_index;
144 int rc;
145 int cable_ready_timeout __maybe_unused;
146
Mateusz Zalegad862f892013-10-04 19:22:26 +0200147 if (argc < 3)
148 return CMD_RET_USAGE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100149
Stephen Warren5e8c2272014-05-05 10:40:17 -0600150 usb_controller = argv[1];
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600151 if (argc >= 4) {
152 devtype = argv[2];
153 devnum = argv[3];
154 } else {
155 devtype = "mmc";
156 devnum = argv[2];
157 }
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +0200158
Stephen Warren9e7d5882015-12-07 11:38:50 -0700159 rc = ums_init(devtype, devnum);
160 if (rc < 0)
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +0200161 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100162
Stephen Warren5e8c2272014-05-05 10:40:17 -0600163 controller_index = (unsigned int)(simple_strtoul(
164 usb_controller, NULL, 0));
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +0100165 if (usb_gadget_initialize(controller_index)) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900166 pr_err("Couldn't init USB controller.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700167 rc = CMD_RET_FAILURE;
168 goto cleanup_ums_init;
Mateusz Zalegad862f892013-10-04 19:22:26 +0200169 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100170
Stephen Warren9e7d5882015-12-07 11:38:50 -0700171 rc = fsg_init(ums, ums_count);
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100172 if (rc) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900173 pr_err("fsg_init failed\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700174 rc = CMD_RET_FAILURE;
175 goto cleanup_board;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100176 }
177
Stephen Warrenc611a6b2014-05-01 15:42:10 -0600178 rc = g_dnl_register("usb_dnl_ums");
179 if (rc) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900180 pr_err("g_dnl_register failed\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700181 rc = CMD_RET_FAILURE;
182 goto cleanup_board;
Stephen Warrenc611a6b2014-05-01 15:42:10 -0600183 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100184
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100185 /* Timeout unit: seconds */
Stephen Warren5e8c2272014-05-05 10:40:17 -0600186 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100187
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200188 if (!g_dnl_board_usb_cable_connected()) {
189 /*
190 * Won't execute if we don't know whether the cable is
191 * connected.
192 */
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100193 puts("Please connect USB cable.\n");
194
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200195 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100196 if (ctrlc()) {
197 puts("\rCTRL+C - Operation aborted.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700198 rc = CMD_RET_SUCCESS;
199 goto cleanup_register;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100200 }
201 if (!cable_ready_timeout) {
202 puts("\rUSB cable not detected.\n" \
203 "Command exit.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700204 rc = CMD_RET_SUCCESS;
205 goto cleanup_register;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100206 }
207
208 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
209 mdelay(1000);
210 cable_ready_timeout--;
211 }
212 puts("\r\n");
213 }
214
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100215 while (1) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +0530216 usb_gadget_handle_interrupts(controller_index);
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200217
218 rc = fsg_main_thread(NULL);
219 if (rc) {
220 /* Check I/O error */
221 if (rc == -EIO)
222 printf("\rCheck USB cable connection\n");
223
224 /* Check CTRL+C */
225 if (rc == -EPIPE)
226 printf("\rCTRL+C - Operation aborted\n");
227
Stephen Warren9e7d5882015-12-07 11:38:50 -0700228 rc = CMD_RET_SUCCESS;
229 goto cleanup_register;
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200230 }
Patrick Delaunay77498e62019-04-18 17:18:39 +0200231
232 WATCHDOG_RESET();
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100233 }
Stephen Warren9e7d5882015-12-07 11:38:50 -0700234
235cleanup_register:
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100236 g_dnl_unregister();
Stephen Warren9e7d5882015-12-07 11:38:50 -0700237cleanup_board:
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +0100238 usb_gadget_release(controller_index);
Stephen Warren9e7d5882015-12-07 11:38:50 -0700239cleanup_ums_init:
240 ums_fini();
241
242 return rc;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100243}
244
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600245U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevam726c1232015-02-28 15:11:46 -0300246 "Use the UMS [USB Mass Storage]",
John Tobias9fc92452016-04-13 12:06:51 -0700247 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600248 " devtype defaults to mmc"
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100249);