]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/USBHost/USBHostMSD/USBHostMSD.cpp
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / USBHost / USBHostMSD / USBHostMSD.cpp
1 /* mbed USBHost Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "USBHostMSD.h"
18
19 #if USBHOST_MSD
20
21 #include "dbg.h"
22
23 #define CBW_SIGNATURE 0x43425355
24 #define CSW_SIGNATURE 0x53425355
25
26 #define DEVICE_TO_HOST 0x80
27 #define HOST_TO_DEVICE 0x00
28
29 #define GET_MAX_LUN (0xFE)
30 #define BO_MASS_STORAGE_RESET (0xFF)
31
32 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
33 {
34 host = USBHost::getHostInst();
35 init();
36 }
37
38 void USBHostMSD::init() {
39 dev_connected = false;
40 dev = NULL;
41 bulk_in = NULL;
42 bulk_out = NULL;
43 dev_connected = false;
44 blockSize = 0;
45 blockCount = 0;
46 msd_intf = -1;
47 msd_device_found = false;
48 disk_init = false;
49 dev_connected = false;
50 nb_ep = 0;
51 }
52
53
54 bool USBHostMSD::connected()
55 {
56 return dev_connected;
57 }
58
59 bool USBHostMSD::connect()
60 {
61
62 if (dev_connected) {
63 return true;
64 }
65
66 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
67 if ((dev = host->getDevice(i)) != NULL) {
68
69 USB_DBG("Trying to connect MSD device\r\n");
70
71 if(host->enumerate(dev, this))
72 break;
73
74 if (msd_device_found) {
75 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN);
76 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT);
77
78 if (!bulk_in || !bulk_out)
79 continue;
80
81 USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf);
82 dev->setName("MSD", msd_intf);
83 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);
84
85 dev_connected = true;
86 return true;
87 }
88 } //if()
89 } //for()
90 init();
91 return false;
92 }
93
94 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid)
95 {
96 // we don't check VID/PID for MSD driver
97 }
98
99 /*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
100 {
101 if ((msd_intf == -1) &&
102 (intf_class == MSD_CLASS) &&
103 (intf_subclass == 0x06) &&
104 (intf_protocol == 0x50)) {
105 msd_intf = intf_nb;
106 return true;
107 }
108 return false;
109 }
110
111 /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
112 {
113 if (intf_nb == msd_intf) {
114 if (type == BULK_ENDPOINT) {
115 nb_ep++;
116 if (nb_ep == 2)
117 msd_device_found = true;
118 return true;
119 }
120 }
121 return false;
122 }
123
124
125 int USBHostMSD::testUnitReady() {
126 USB_DBG("Test unit ready");
127 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
128 }
129
130
131 int USBHostMSD::readCapacity() {
132 USB_DBG("Read capacity");
133 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
134 uint8_t result[8];
135 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8);
136 if (status == 0) {
137 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3];
138 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7];
139 USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize);
140 }
141 return status;
142 }
143
144
145 int USBHostMSD::SCSIRequestSense() {
146 USB_DBG("Request sense");
147 uint8_t cmd[6] = {0x03,0,0,0,18,0};
148 uint8_t result[18];
149 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18);
150 return status;
151 }
152
153
154 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
155 USB_DBG("Inquiry");
156 uint8_t evpd = (page_code == 0) ? 0 : 1;
157 uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0};
158 uint8_t result[36];
159 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36);
160 if (status == 0) {
161 char vid_pid[17];
162 memcpy(vid_pid, &result[8], 8);
163 vid_pid[8] = 0;
164 USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid);
165
166 memcpy(vid_pid, &result[16], 16);
167 vid_pid[16] = 0;
168 USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid);
169
170 memcpy(vid_pid, &result[32], 4);
171 vid_pid[4] = 0;
172 USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid);
173 }
174 return status;
175 }
176
177 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
178 // if ep stalled: send clear feature
179 if (res == USB_TYPE_STALL_ERROR) {
180 res = host->controlWrite( dev,
181 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
182 CLEAR_FEATURE,
183 0, ep->getAddress(), NULL, 0);
184 // set state to IDLE if clear feature successful
185 if (res == USB_TYPE_OK) {
186 ep->setState(USB_TYPE_IDLE);
187 }
188 }
189
190 if (res != USB_TYPE_OK)
191 return -1;
192
193 return 0;
194 }
195
196
197 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
198
199 int res = 0;
200
201 cbw.Signature = CBW_SIGNATURE;
202 cbw.Tag = 0;
203 cbw.DataLength = transfer_len;
204 cbw.Flags = flags;
205 cbw.LUN = 0;
206 cbw.CBLength = cmd_len;
207 memset(cbw.CB,0,sizeof(cbw.CB));
208 if (cmd) {
209 memcpy(cbw.CB,cmd,cmd_len);
210 }
211
212 // send the cbw
213 USB_DBG("Send CBW");
214 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
215 if (checkResult(res, bulk_out))
216 return -1;
217
218 // data stage if needed
219 if (data) {
220 USB_DBG("data stage");
221 if (flags == HOST_TO_DEVICE) {
222
223 res = host->bulkWrite(dev, bulk_out, data, transfer_len);
224 if (checkResult(res, bulk_out))
225 return -1;
226
227 } else if (flags == DEVICE_TO_HOST) {
228
229 res = host->bulkRead(dev, bulk_in, data, transfer_len);
230 if (checkResult(res, bulk_in))
231 return -1;
232 }
233 }
234
235 // status stage
236 csw.Signature = 0;
237 USB_DBG("Read CSW");
238 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
239 if (checkResult(res, bulk_in))
240 return -1;
241
242 if (csw.Signature != CSW_SIGNATURE) {
243 return -1;
244 }
245
246 USB_DBG("recv csw: status: %d", csw.Status);
247
248 // ModeSense?
249 if ((csw.Status == 1) && (cmd[0] != 0x03)) {
250 USB_DBG("request mode sense");
251 return SCSIRequestSense();
252 }
253
254 // perform reset recovery
255 if ((csw.Status == 2) && (cmd[0] != 0x03)) {
256
257 // send Bulk-Only Mass Storage Reset request
258 res = host->controlWrite( dev,
259 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
260 BO_MASS_STORAGE_RESET,
261 0, msd_intf, NULL, 0);
262
263 // unstall both endpoints
264 res = host->controlWrite( dev,
265 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
266 CLEAR_FEATURE,
267 0, bulk_in->getAddress(), NULL, 0);
268
269 res = host->controlWrite( dev,
270 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
271 CLEAR_FEATURE,
272 0, bulk_out->getAddress(), NULL, 0);
273
274 }
275
276 return csw.Status;
277 }
278
279
280 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
281 uint8_t cmd[10];
282 memset(cmd,0,10);
283 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
284
285 cmd[2] = (block >> 24) & 0xff;
286 cmd[3] = (block >> 16) & 0xff;
287 cmd[4] = (block >> 8) & 0xff;
288 cmd[5] = block & 0xff;
289
290 cmd[7] = (nbBlock >> 8) & 0xff;
291 cmd[8] = nbBlock & 0xff;
292
293 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
294 }
295
296 int USBHostMSD::getMaxLun() {
297 uint8_t buf[1], res;
298 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
299 0xfe, 0, msd_intf, buf, 1);
300 USB_DBG("max lun: %d", buf[0]);
301 return res;
302 }
303
304 int USBHostMSD::disk_initialize() {
305 USB_DBG("FILESYSTEM: init");
306 uint16_t i, timeout = 10;
307
308 getMaxLun();
309
310 for (i = 0; i < timeout; i++) {
311 Thread::wait(100);
312 if (!testUnitReady())
313 break;
314 }
315
316 if (i == timeout) {
317 disk_init = false;
318 return -1;
319 }
320
321 inquiry(0, 0);
322 disk_init = 1;
323 return readCapacity();
324 }
325
326 int USBHostMSD::disk_write(const uint8_t* buffer, uint64_t block_number, uint8_t count) {
327 USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count);
328 if (!disk_init) {
329 disk_initialize();
330 }
331 if (!disk_init)
332 return -1;
333 for (uint64_t b = block_number; b < block_number + count; b++) {
334 if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE))
335 return -1;
336 buffer += 512;
337 }
338 return 0;
339 }
340
341 int USBHostMSD::disk_read(uint8_t* buffer, uint64_t block_number, uint8_t count) {
342 USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count);
343 if (!disk_init) {
344 disk_initialize();
345 }
346 if (!disk_init)
347 return -1;
348 for (uint64_t b = block_number; b < block_number + count; b++) {
349 if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST))
350 return -1;
351 buffer += 512;
352 }
353 return 0;
354 }
355
356 uint64_t USBHostMSD::disk_sectors() {
357 USB_DBG("FILESYSTEM: sectors");
358 if (!disk_init) {
359 disk_initialize();
360 }
361 if (!disk_init)
362 return 0;
363 return blockCount;
364 }
365
366 #endif
Imprint / Impressum