blob: 198dab15baf0335b7746a055fd1e3ecf2a2d0e70 [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>
11#include <g_dnl.h>
Stephen Warren803876d2014-05-05 10:40:15 -060012#include <part.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020013#include <usb.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010014#include <usb_mass_storage.h>
15
Stephen Warren803876d2014-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 Warren5c7648b2014-05-05 10:40:16 -060042struct ums *ums_init(const char *devtype, const char *devnum)
Stephen Warren803876d2014-05-05 10:40:15 -060043{
Stephen Warren5c7648b2014-05-05 10:40:16 -060044 block_dev_desc_t *block_dev;
45 int ret;
Stephen Warren803876d2014-05-05 10:40:15 -060046
Stephen Warren5c7648b2014-05-05 10:40:16 -060047 ret = get_device(devtype, devnum, &block_dev);
48 if (ret < 0)
Stephen Warren803876d2014-05-05 10:40:15 -060049 return NULL;
50
Stephen Warren5c7648b2014-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 Warren803876d2014-05-05 10:40:15 -060056 ums_dev.start_sector = 0;
Stephen Warren5c7648b2014-05-05 10:40:16 -060057 ums_dev.num_sectors = block_dev->lba;
Stephen Warren803876d2014-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 Majewski44a6ac92013-03-05 12:10:17 +010065int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
66 int argc, char * const argv[])
67{
Stephen Warren5e8c2272014-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 Zalegad862f892013-10-04 19:22:26 +020076 if (argc < 3)
77 return CMD_RET_USAGE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010078
Stephen Warren5e8c2272014-05-05 10:40:17 -060079 usb_controller = argv[1];
Stephen Warrene4e98cd2014-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 }
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +020087
Stephen Warren5e8c2272014-05-05 10:40:17 -060088 ums = ums_init(devtype, devnum);
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +020089 if (!ums)
90 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010091
Stephen Warren5e8c2272014-05-05 10:40:17 -060092 controller_index = (unsigned int)(simple_strtoul(
93 usb_controller, NULL, 0));
Mateusz Zalegad862f892013-10-04 19:22:26 +020094 if (board_usb_init(controller_index, USB_INIT_DEVICE)) {
95 error("Couldn't init USB controller.");
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +020096 return CMD_RET_FAILURE;
Mateusz Zalegad862f892013-10-04 19:22:26 +020097 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010098
Stephen Warren5e8c2272014-05-05 10:40:17 -060099 rc = fsg_init(ums);
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100100 if (rc) {
Mateusz Zalegad862f892013-10-04 19:22:26 +0200101 error("fsg_init failed");
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +0200102 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100103 }
104
Stephen Warrenc611a6b2014-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 Majewski44a6ac92013-03-05 12:10:17 +0100110
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100111 /* Timeout unit: seconds */
Stephen Warren5e8c2272014-05-05 10:40:17 -0600112 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100113
Mateusz Zalega21fe3f72014-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 Marczake92cc562014-01-07 15:08:37 +0100119 puts("Please connect USB cable.\n");
120
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200121 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczake92cc562014-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 Majewski44a6ac92013-03-05 12:10:17 +0100139 while (1) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +0530140 usb_gadget_handle_interrupts(controller_index);
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200141
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 Majewski44a6ac92013-03-05 12:10:17 +0100152 goto exit;
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200153 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100154 }
155exit:
156 g_dnl_unregister();
Inha Song3ab2d772015-03-03 17:32:05 +0100157 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Przemyslaw Marczak674b1a62013-10-23 14:30:42 +0200158 return CMD_RET_SUCCESS;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100159}
160
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600161U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevam726c1232015-02-28 15:11:46 -0300162 "Use the UMS [USB Mass Storage]",
Fabio Estevamdec5dd62015-02-25 13:03:19 -0300163 "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n"
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600164 " devtype defaults to mmc"
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100165);