Lukasz Majewski | 44a6ac9 | 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 | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 12 | #include <g_dnl.h> |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 13 | #include <part.h> |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 14 | #include <usb.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 15 | #include <usb_mass_storage.h> |
| 16 | |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 17 | static 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 | |
| 27 | static 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 | |
| 37 | static struct ums ums_dev = { |
| 38 | .read_sector = ums_read_sector, |
| 39 | .write_sector = ums_write_sector, |
| 40 | .name = "UMS disk", |
| 41 | }; |
| 42 | |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 43 | struct ums *ums_init(const char *devtype, const char *devnum) |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 44 | { |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 45 | block_dev_desc_t *block_dev; |
| 46 | int ret; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 47 | |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 48 | ret = get_device(devtype, devnum, &block_dev); |
| 49 | if (ret < 0) |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 50 | return NULL; |
| 51 | |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 52 | /* 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 Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 57 | ums_dev.start_sector = 0; |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 58 | ums_dev.num_sectors = block_dev->lba; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 59 | |
| 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 Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 66 | int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, |
| 67 | int argc, char * const argv[]) |
| 68 | { |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 69 | 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 Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 77 | if (argc < 3) |
| 78 | return CMD_RET_USAGE; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 79 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 80 | usb_controller = argv[1]; |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 81 | if (argc >= 4) { |
| 82 | devtype = argv[2]; |
| 83 | devnum = argv[3]; |
| 84 | } else { |
| 85 | devtype = "mmc"; |
| 86 | devnum = argv[2]; |
| 87 | } |
Przemyslaw Marczak | 0e1e709 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 88 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 89 | ums = ums_init(devtype, devnum); |
Przemyslaw Marczak | 0e1e709 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 90 | if (!ums) |
| 91 | return CMD_RET_FAILURE; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 92 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 93 | controller_index = (unsigned int)(simple_strtoul( |
| 94 | usb_controller, NULL, 0)); |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 95 | if (board_usb_init(controller_index, USB_INIT_DEVICE)) { |
| 96 | error("Couldn't init USB controller."); |
Przemyslaw Marczak | 674b1a6 | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 97 | return CMD_RET_FAILURE; |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 98 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 99 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 100 | rc = fsg_init(ums); |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 101 | if (rc) { |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 102 | error("fsg_init failed"); |
Przemyslaw Marczak | 674b1a6 | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 103 | return CMD_RET_FAILURE; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Stephen Warren | c611a6b | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 106 | rc = g_dnl_register("usb_dnl_ums"); |
| 107 | if (rc) { |
| 108 | error("g_dnl_register failed"); |
| 109 | return CMD_RET_FAILURE; |
| 110 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 111 | |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 112 | /* Timeout unit: seconds */ |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 113 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 114 | |
Mateusz Zalega | 21fe3f7 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 115 | 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 Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 120 | puts("Please connect USB cable.\n"); |
| 121 | |
Mateusz Zalega | 21fe3f7 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 122 | while (!g_dnl_board_usb_cable_connected()) { |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 123 | 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 Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 140 | while (1) { |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 141 | usb_gadget_handle_interrupts(controller_index); |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 142 | |
| 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 Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 153 | goto exit; |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 154 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 155 | } |
| 156 | exit: |
| 157 | g_dnl_unregister(); |
Inha Song | 3ab2d77 | 2015-03-03 17:32:05 +0100 | [diff] [blame] | 158 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
Przemyslaw Marczak | 674b1a6 | 2013-10-23 14:30:42 +0200 | [diff] [blame] | 159 | return CMD_RET_SUCCESS; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 162 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
Fabio Estevam | 726c123 | 2015-02-28 15:11:46 -0300 | [diff] [blame] | 163 | "Use the UMS [USB Mass Storage]", |
Fabio Estevam | dec5dd6 | 2015-02-25 13:03:19 -0300 | [diff] [blame] | 164 | "<USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n" |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 165 | " devtype defaults to mmc" |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 166 | ); |