Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics |
| 3 | * Lukasz Majewski <l.majewski@samsung.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Przemyslaw Marczak | 351e9b2 | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <g_dnl.h> |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 12 | #include <part.h> |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 13 | #include <usb.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 14 | #include <usb_mass_storage.h> |
| 15 | |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 16 | static 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 | |
| 26 | static 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 | |
| 36 | static struct ums ums_dev = { |
| 37 | .read_sector = ums_read_sector, |
| 38 | .write_sector = ums_write_sector, |
| 39 | .name = "UMS disk", |
| 40 | }; |
| 41 | |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 42 | struct ums *ums_init(const char *devtype, const char *devnum) |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 43 | { |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 44 | block_dev_desc_t *block_dev; |
| 45 | int ret; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 46 | |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 47 | ret = get_device(devtype, devnum, &block_dev); |
| 48 | if (ret < 0) |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 49 | return NULL; |
| 50 | |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 51 | /* 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 Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 56 | ums_dev.start_sector = 0; |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 57 | ums_dev.num_sectors = block_dev->lba; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 58 | |
| 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 Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 65 | int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, |
| 66 | int argc, char * const argv[]) |
| 67 | { |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 68 | 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 Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 76 | if (argc < 3) |
| 77 | return CMD_RET_USAGE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 78 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 79 | usb_controller = argv[1]; |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 80 | if (argc >= 4) { |
| 81 | devtype = argv[2]; |
| 82 | devnum = argv[3]; |
| 83 | } else { |
| 84 | devtype = "mmc"; |
| 85 | devnum = argv[2]; |
| 86 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 87 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 88 | ums = ums_init(devtype, devnum); |
Przemyslaw Marczak | f4dacf7 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 89 | if (!ums) |
| 90 | return CMD_RET_FAILURE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 91 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 92 | controller_index = (unsigned int)(simple_strtoul( |
| 93 | usb_controller, NULL, 0)); |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 94 | if (board_usb_init(controller_index, USB_INIT_DEVICE)) { |
| 95 | error("Couldn't init USB controller."); |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 96 | return CMD_RET_FAILURE; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 97 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 98 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 99 | rc = fsg_init(ums); |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 100 | if (rc) { |
| 101 | error("fsg_init failed"); |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 102 | return CMD_RET_FAILURE; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Stephen Warren | 66b88b0 | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 105 | rc = g_dnl_register("usb_dnl_ums"); |
| 106 | if (rc) { |
| 107 | error("g_dnl_register failed"); |
| 108 | return CMD_RET_FAILURE; |
| 109 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 110 | |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 111 | /* Timeout unit: seconds */ |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 112 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 113 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 114 | 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 Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 119 | puts("Please connect USB cable.\n"); |
| 120 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 121 | while (!g_dnl_board_usb_cable_connected()) { |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 122 | 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 Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 139 | while (1) { |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 140 | usb_gadget_handle_interrupts(controller_index); |
Przemyslaw Marczak | 351e9b2 | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 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 Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 152 | goto exit; |
| 153 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 154 | } |
| 155 | exit: |
| 156 | g_dnl_unregister(); |
Inha Song | 375f2d7 | 2015-03-03 17:32:05 +0100 | [diff] [blame] | 157 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
Przemyslaw Marczak | 93c813b | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 158 | return CMD_RET_SUCCESS; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 161 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
Fabio Estevam | ee02a65 | 2015-02-28 15:11:46 -0300 | [diff] [blame] | 162 | "Use the UMS [USB Mass Storage]", |
Fabio Estevam | e5d3e7f | 2015-02-25 13:03:19 -0300 | [diff] [blame] | 163 | "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n" |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 164 | " devtype defaults to mmc" |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 165 | ); |