blob: 040738911290c63f86715ba93b8df963424e459f [file] [log] [blame]
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01001/*
2 * Copyright (C) 2011 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01006 */
7
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +02008#include <errno.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01009#include <common.h>
10#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010012#include <g_dnl.h>
Stephen Warren803876d2014-05-05 10:40:15 -060013#include <part.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020014#include <usb.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010015#include <usb_mass_storage.h>
16
Stephen Warren803876d2014-05-05 10:40:15 -060017static int ums_read_sector(struct ums *ums_dev,
18 ulong start, lbaint_t blkcnt, void *buf)
19{
20 block_dev_desc_t *block_dev = ums_dev->block_dev;
21 lbaint_t blkstart = start + ums_dev->start_sector;
22 int dev_num = block_dev->dev;
23
24 return block_dev->block_read(dev_num, blkstart, blkcnt, buf);
25}
26
27static int ums_write_sector(struct ums *ums_dev,
28 ulong start, lbaint_t blkcnt, const void *buf)
29{
30 block_dev_desc_t *block_dev = ums_dev->block_dev;
31 lbaint_t blkstart = start + ums_dev->start_sector;
32 int dev_num = block_dev->dev;
33
34 return block_dev->block_write(dev_num, blkstart, blkcnt, buf);
35}
36
37static struct ums ums_dev = {
38 .read_sector = ums_read_sector,
39 .write_sector = ums_write_sector,
40 .name = "UMS disk",
41};
42
Stephen Warren5c7648b2014-05-05 10:40:16 -060043struct ums *ums_init(const char *devtype, const char *devnum)
Stephen Warren803876d2014-05-05 10:40:15 -060044{
Stephen Warren5c7648b2014-05-05 10:40:16 -060045 block_dev_desc_t *block_dev;
46 int ret;
Stephen Warren803876d2014-05-05 10:40:15 -060047
Stephen Warren5c7648b2014-05-05 10:40:16 -060048 ret = get_device(devtype, devnum, &block_dev);
49 if (ret < 0)
Stephen Warren803876d2014-05-05 10:40:15 -060050 return NULL;
51
Stephen Warren5c7648b2014-05-05 10:40:16 -060052 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
53 if (block_dev->blksz != SECTOR_SIZE)
54 return NULL;
55
56 ums_dev.block_dev = block_dev;
Stephen Warren803876d2014-05-05 10:40:15 -060057 ums_dev.start_sector = 0;
Stephen Warren5c7648b2014-05-05 10:40:16 -060058 ums_dev.num_sectors = block_dev->lba;
Stephen Warren803876d2014-05-05 10:40:15 -060059
60 printf("UMS: disk start sector: %#x, count: %#x\n",
61 ums_dev.start_sector, ums_dev.num_sectors);
62
63 return &ums_dev;
64}
65
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010066int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
67 int argc, char * const argv[])
68{
Stephen Warren5e8c2272014-05-05 10:40:17 -060069 const char *usb_controller;
70 const char *devtype;
71 const char *devnum;
72 struct ums *ums;
73 unsigned int controller_index;
74 int rc;
75 int cable_ready_timeout __maybe_unused;
76
Mateusz Zalegad862f892013-10-04 19:22:26 +020077 if (argc < 3)
78 return CMD_RET_USAGE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010079
Stephen Warren5e8c2272014-05-05 10:40:17 -060080 usb_controller = argv[1];
Stephen Warrene4e98cd2014-05-05 10:40:18 -060081 if (argc >= 4) {
82 devtype = argv[2];
83 devnum = argv[3];
84 } else {
85 devtype = "mmc";
86 devnum = argv[2];
87 }
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +020088
Stephen Warren5e8c2272014-05-05 10:40:17 -060089 ums = ums_init(devtype, devnum);
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +020090 if (!ums)
91 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010092
Stephen Warren5e8c2272014-05-05 10:40:17 -060093 controller_index = (unsigned int)(simple_strtoul(
94 usb_controller, NULL, 0));
Mateusz Zalegad862f892013-10-04 19:22:26 +020095 if (board_usb_init(controller_index, USB_INIT_DEVICE)) {
96 error("Couldn't init USB controller.");
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +020097 return CMD_RET_FAILURE;
Mateusz Zalegad862f892013-10-04 19:22:26 +020098 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010099
Stephen Warren5e8c2272014-05-05 10:40:17 -0600100 rc = fsg_init(ums);
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100101 if (rc) {
Mateusz Zalegad862f892013-10-04 19:22:26 +0200102 error("fsg_init failed");
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +0200103 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100104 }
105
Stephen Warrenc611a6b2014-05-01 15:42:10 -0600106 rc = g_dnl_register("usb_dnl_ums");
107 if (rc) {
108 error("g_dnl_register failed");
109 return CMD_RET_FAILURE;
110 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100111
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100112 /* Timeout unit: seconds */
Stephen Warren5e8c2272014-05-05 10:40:17 -0600113 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100114
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200115 if (!g_dnl_board_usb_cable_connected()) {
116 /*
117 * Won't execute if we don't know whether the cable is
118 * connected.
119 */
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100120 puts("Please connect USB cable.\n");
121
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200122 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100123 if (ctrlc()) {
124 puts("\rCTRL+C - Operation aborted.\n");
125 goto exit;
126 }
127 if (!cable_ready_timeout) {
128 puts("\rUSB cable not detected.\n" \
129 "Command exit.\n");
130 goto exit;
131 }
132
133 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
134 mdelay(1000);
135 cable_ready_timeout--;
136 }
137 puts("\r\n");
138 }
139
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100140 while (1) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +0530141 usb_gadget_handle_interrupts(controller_index);
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200142
143 rc = fsg_main_thread(NULL);
144 if (rc) {
145 /* Check I/O error */
146 if (rc == -EIO)
147 printf("\rCheck USB cable connection\n");
148
149 /* Check CTRL+C */
150 if (rc == -EPIPE)
151 printf("\rCTRL+C - Operation aborted\n");
152
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100153 goto exit;
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200154 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100155 }
156exit:
157 g_dnl_unregister();
Inha Song3ab2d772015-03-03 17:32:05 +0100158 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +0200159 return CMD_RET_SUCCESS;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100160}
161
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600162U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevam726c1232015-02-28 15:11:46 -0300163 "Use the UMS [USB Mass Storage]",
Fabio Estevamdec5dd62015-02-25 13:03:19 -0300164 "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n"
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600165 " devtype defaults to mmc"
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100166);