]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2011 Samsung Electronics | |
3 | * Lukasz Majewski <[email protected]> | |
4 | * | |
5 | * SPDX-License-Identifier: GPL-2.0+ | |
6 | */ | |
7 | ||
8 | #include <errno.h> | |
9 | #include <common.h> | |
10 | #include <command.h> | |
11 | #include <g_dnl.h> | |
12 | #include <part.h> | |
13 | #include <usb.h> | |
14 | #include <usb_mass_storage.h> | |
15 | ||
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 | ||
42 | struct ums *ums_init(const char *devtype, const char *devnum) | |
43 | { | |
44 | block_dev_desc_t *block_dev; | |
45 | int ret; | |
46 | ||
47 | ret = get_device(devtype, devnum, &block_dev); | |
48 | if (ret < 0) | |
49 | return NULL; | |
50 | ||
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; | |
56 | ums_dev.start_sector = 0; | |
57 | ums_dev.num_sectors = block_dev->lba; | |
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 | ||
65 | int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, | |
66 | int argc, char * const argv[]) | |
67 | { | |
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 | ||
76 | if (argc < 3) | |
77 | return CMD_RET_USAGE; | |
78 | ||
79 | usb_controller = argv[1]; | |
80 | if (argc >= 4) { | |
81 | devtype = argv[2]; | |
82 | devnum = argv[3]; | |
83 | } else { | |
84 | devtype = "mmc"; | |
85 | devnum = argv[2]; | |
86 | } | |
87 | ||
88 | ums = ums_init(devtype, devnum); | |
89 | if (!ums) | |
90 | return CMD_RET_FAILURE; | |
91 | ||
92 | controller_index = (unsigned int)(simple_strtoul( | |
93 | usb_controller, NULL, 0)); | |
94 | if (board_usb_init(controller_index, USB_INIT_DEVICE)) { | |
95 | error("Couldn't init USB controller."); | |
96 | return CMD_RET_FAILURE; | |
97 | } | |
98 | ||
99 | rc = fsg_init(ums); | |
100 | if (rc) { | |
101 | error("fsg_init failed"); | |
102 | return CMD_RET_FAILURE; | |
103 | } | |
104 | ||
105 | rc = g_dnl_register("usb_dnl_ums"); | |
106 | if (rc) { | |
107 | error("g_dnl_register failed"); | |
108 | return CMD_RET_FAILURE; | |
109 | } | |
110 | ||
111 | /* Timeout unit: seconds */ | |
112 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; | |
113 | ||
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 | */ | |
119 | puts("Please connect USB cable.\n"); | |
120 | ||
121 | while (!g_dnl_board_usb_cable_connected()) { | |
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 | ||
139 | while (1) { | |
140 | 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 | ||
152 | goto exit; | |
153 | } | |
154 | } | |
155 | exit: | |
156 | g_dnl_unregister(); | |
157 | return CMD_RET_SUCCESS; | |
158 | } | |
159 | ||
160 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, | |
161 | "Use the UMS [User Mass Storage]", | |
162 | "ums <USB_controller> [<devtype>] <devnum> e.g. ums 0 mmc 0\n" | |
163 | " devtype defaults to mmc" | |
164 | ); |