blob: 2c879ea0837c5e13e69fb60333bb921e68b2d991 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskib528f712013-03-05 12:10:17 +01006 */
7
Przemyslaw Marczak351e9b22013-10-23 14:30:46 +02008#include <errno.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +01009#include <common.h>
10#include <command.h>
11#include <g_dnl.h>
Stephen Warrenabfe8af2014-05-05 10:40:15 -060012#include <part.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020013#include <usb.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010014#include <usb_mass_storage.h>
15
Stephen Warrenabfe8af2014-05-05 10:40:15 -060016static int ums_read_sector(struct ums *ums_dev,
17 ulong start, lbaint_t blkcnt, void *buf)
18{
19 block_dev_desc_t *block_dev = ums_dev->block_dev;
20 lbaint_t blkstart = start + ums_dev->start_sector;
21 int dev_num = block_dev->dev;
22
23 return block_dev->block_read(dev_num, blkstart, blkcnt, buf);
24}
25
26static int ums_write_sector(struct ums *ums_dev,
27 ulong start, lbaint_t blkcnt, const void *buf)
28{
29 block_dev_desc_t *block_dev = ums_dev->block_dev;
30 lbaint_t blkstart = start + ums_dev->start_sector;
31 int dev_num = block_dev->dev;
32
33 return block_dev->block_write(dev_num, blkstart, blkcnt, buf);
34}
35
36static struct ums ums_dev = {
37 .read_sector = ums_read_sector,
38 .write_sector = ums_write_sector,
39 .name = "UMS disk",
40};
41
Stephen Warrend0cc4562014-05-05 10:40:16 -060042struct ums *ums_init(const char *devtype, const char *devnum)
Stephen Warrenabfe8af2014-05-05 10:40:15 -060043{
Stephen Warrend0cc4562014-05-05 10:40:16 -060044 block_dev_desc_t *block_dev;
45 int ret;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060046
Stephen Warrend0cc4562014-05-05 10:40:16 -060047 ret = get_device(devtype, devnum, &block_dev);
48 if (ret < 0)
Stephen Warrenabfe8af2014-05-05 10:40:15 -060049 return NULL;
50
Stephen Warrend0cc4562014-05-05 10:40:16 -060051 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
52 if (block_dev->blksz != SECTOR_SIZE)
53 return NULL;
54
55 ums_dev.block_dev = block_dev;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060056 ums_dev.start_sector = 0;
Stephen Warrend0cc4562014-05-05 10:40:16 -060057 ums_dev.num_sectors = block_dev->lba;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060058
59 printf("UMS: disk start sector: %#x, count: %#x\n",
60 ums_dev.start_sector, ums_dev.num_sectors);
61
62 return &ums_dev;
63}
64
Lukasz Majewskib528f712013-03-05 12:10:17 +010065int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
66 int argc, char * const argv[])
67{
Stephen Warren1725f122014-05-05 10:40:17 -060068 const char *usb_controller;
69 const char *devtype;
70 const char *devnum;
71 struct ums *ums;
72 unsigned int controller_index;
73 int rc;
74 int cable_ready_timeout __maybe_unused;
75
Mateusz Zalega16297cf2013-10-04 19:22:26 +020076 if (argc < 3)
77 return CMD_RET_USAGE;
Lukasz Majewskib528f712013-03-05 12:10:17 +010078
Stephen Warren1725f122014-05-05 10:40:17 -060079 usb_controller = argv[1];
Stephen Warren8c600452014-05-05 10:40:18 -060080 if (argc >= 4) {
81 devtype = argv[2];
82 devnum = argv[3];
83 } else {
84 devtype = "mmc";
85 devnum = argv[2];
86 }
Lukasz Majewskib528f712013-03-05 12:10:17 +010087
Stephen Warren1725f122014-05-05 10:40:17 -060088 ums = ums_init(devtype, devnum);
Przemyslaw Marczakf4dacf72013-10-23 14:30:43 +020089 if (!ums)
90 return CMD_RET_FAILURE;
Lukasz Majewskib528f712013-03-05 12:10:17 +010091
Stephen Warren1725f122014-05-05 10:40:17 -060092 controller_index = (unsigned int)(simple_strtoul(
93 usb_controller, NULL, 0));
Mateusz Zalega16297cf2013-10-04 19:22:26 +020094 if (board_usb_init(controller_index, USB_INIT_DEVICE)) {
95 error("Couldn't init USB controller.");
Przemyslaw Marczak93c813b2013-10-23 14:30:42 +020096 return CMD_RET_FAILURE;
Mateusz Zalega16297cf2013-10-04 19:22:26 +020097 }
Lukasz Majewskib528f712013-03-05 12:10:17 +010098
Stephen Warren1725f122014-05-05 10:40:17 -060099 rc = fsg_init(ums);
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200100 if (rc) {
101 error("fsg_init failed");
Przemyslaw Marczak93c813b2013-10-23 14:30:42 +0200102 return CMD_RET_FAILURE;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200103 }
104
Stephen Warren66b88b02014-05-01 15:42:10 -0600105 rc = g_dnl_register("usb_dnl_ums");
106 if (rc) {
107 error("g_dnl_register failed");
108 return CMD_RET_FAILURE;
109 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100110
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100111 /* Timeout unit: seconds */
Stephen Warren1725f122014-05-05 10:40:17 -0600112 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100113
Mateusz Zalega75504e92014-04-30 13:07:48 +0200114 if (!g_dnl_board_usb_cable_connected()) {
115 /*
116 * Won't execute if we don't know whether the cable is
117 * connected.
118 */
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100119 puts("Please connect USB cable.\n");
120
Mateusz Zalega75504e92014-04-30 13:07:48 +0200121 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100122 if (ctrlc()) {
123 puts("\rCTRL+C - Operation aborted.\n");
124 goto exit;
125 }
126 if (!cable_ready_timeout) {
127 puts("\rUSB cable not detected.\n" \
128 "Command exit.\n");
129 goto exit;
130 }
131
132 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
133 mdelay(1000);
134 cable_ready_timeout--;
135 }
136 puts("\r\n");
137 }
138
Lukasz Majewskib528f712013-03-05 12:10:17 +0100139 while (1) {
Przemyslaw Marczak351e9b22013-10-23 14:30:46 +0200140 usb_gadget_handle_interrupts();
141
142 rc = fsg_main_thread(NULL);
143 if (rc) {
144 /* Check I/O error */
145 if (rc == -EIO)
146 printf("\rCheck USB cable connection\n");
147
148 /* Check CTRL+C */
149 if (rc == -EPIPE)
150 printf("\rCTRL+C - Operation aborted\n");
151
Lukasz Majewskib528f712013-03-05 12:10:17 +0100152 goto exit;
153 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100154 }
155exit:
156 g_dnl_unregister();
Przemyslaw Marczak93c813b2013-10-23 14:30:42 +0200157 return CMD_RET_SUCCESS;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100158}
159
Stephen Warren8c600452014-05-05 10:40:18 -0600160U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Lukasz Majewskib528f712013-03-05 12:10:17 +0100161 "Use the UMS [User Mass Storage]",
Stephen Warren8c600452014-05-05 10:40:18 -0600162 "ums <USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n"
163 " devtype defaults to mmc"
Lukasz Majewskib528f712013-03-05 12:10:17 +0100164);