Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FATFileSystem mbed-rtos
Fork of USBHost by
USBHostMSD.cpp
00001 /* mbed USBHost Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "USBHostMSD.h" 00018 00019 #if USBHOST_MSD 00020 00021 #include "dbg.h" 00022 00023 #define CBW_SIGNATURE 0x43425355 00024 #define CSW_SIGNATURE 0x53425355 00025 00026 #define DEVICE_TO_HOST 0x80 00027 #define HOST_TO_DEVICE 0x00 00028 00029 #define GET_MAX_LUN (0xFE) 00030 #define BO_MASS_STORAGE_RESET (0xFF) 00031 00032 extern DigitalOut myled; 00033 00034 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) 00035 { 00036 host = USBHost::getHostInst(); 00037 init(); 00038 } 00039 00040 void USBHostMSD::init() { 00041 dev_connected = false; 00042 dev = NULL; 00043 bulk_in = NULL; 00044 bulk_out = NULL; 00045 dev_connected = false; 00046 blockSize = 0; 00047 blockCount = 0; 00048 msd_intf = -1; 00049 msd_device_found = false; 00050 disk_init = false; 00051 dev_connected = false; 00052 nb_ep = 0; 00053 } 00054 00055 00056 bool USBHostMSD::connected() 00057 { 00058 return dev_connected; 00059 } 00060 00061 bool USBHostMSD::connect() 00062 { 00063 00064 if (dev_connected) { 00065 return true; 00066 } 00067 00068 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00069 if ((dev = host->getDevice(i)) != NULL) { 00070 00071 USB_DBG("Trying to connect MSD device\r\n"); 00072 00073 if(host->enumerate(dev, this)) 00074 break; 00075 00076 if (msd_device_found) { 00077 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); 00078 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); 00079 00080 if (!bulk_in || !bulk_out) 00081 continue; 00082 00083 USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); 00084 dev->setName("MSD", msd_intf); 00085 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init); 00086 00087 dev_connected = true; 00088 return true; 00089 } 00090 } //if() 00091 } //for() 00092 init(); 00093 return false; 00094 } 00095 00096 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) 00097 { 00098 // we don't check VID/PID for MSD driver 00099 } 00100 00101 /*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 00102 { 00103 if ((msd_intf == -1) && 00104 (intf_class == MSD_CLASS) && 00105 (intf_subclass == 0x06) && 00106 (intf_protocol == 0x50)) { 00107 msd_intf = intf_nb; 00108 return true; 00109 } 00110 return false; 00111 } 00112 00113 /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00114 { 00115 if (intf_nb == msd_intf) { 00116 if (type == BULK_ENDPOINT) { 00117 nb_ep++; 00118 if (nb_ep == 2) 00119 msd_device_found = true; 00120 return true; 00121 } 00122 } 00123 return false; 00124 } 00125 00126 00127 int USBHostMSD::testUnitReady() { 00128 USB_DBG("Test unit ready"); 00129 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); 00130 } 00131 00132 00133 int USBHostMSD::readCapacity() { 00134 USB_DBG("Read capacity"); 00135 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; 00136 uint8_t result[8]; 00137 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); 00138 if (status == 0) { 00139 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; 00140 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; 00141 USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); 00142 } 00143 return status; 00144 } 00145 00146 00147 int USBHostMSD::SCSIRequestSense() { 00148 USB_DBG("Request sense"); 00149 uint8_t cmd[6] = {0x03,0,0,0,18,0}; 00150 uint8_t result[18]; 00151 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); 00152 return status; 00153 } 00154 00155 00156 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) { 00157 USB_DBG("Inquiry"); 00158 uint8_t evpd = (page_code == 0) ? 0 : 1; 00159 uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; 00160 uint8_t result[36]; 00161 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); 00162 if (status == 0) { 00163 char vid_pid[17]; 00164 memcpy(vid_pid, &result[8], 8); 00165 vid_pid[8] = 0; 00166 USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); 00167 00168 memcpy(vid_pid, &result[16], 16); 00169 vid_pid[16] = 0; 00170 USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); 00171 00172 memcpy(vid_pid, &result[32], 4); 00173 vid_pid[4] = 0; 00174 USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); 00175 } 00176 return status; 00177 } 00178 00179 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) { 00180 // if ep stalled: send clear feature 00181 if (res == USB_TYPE_STALL_ERROR) { 00182 res = host->controlWrite( dev, 00183 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00184 CLEAR_FEATURE, 00185 0, ep->getAddress(), NULL, 0); 00186 // set state to IDLE if clear feature successful 00187 if (res == USB_TYPE_OK) { 00188 ep->setState(USB_TYPE_IDLE); 00189 } 00190 } 00191 00192 if (res != USB_TYPE_OK) 00193 return -1; 00194 00195 return 0; 00196 } 00197 00198 00199 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) { 00200 00201 int res = 0; 00202 00203 cbw.Signature = CBW_SIGNATURE; 00204 cbw.Tag = 0; 00205 cbw.DataLength = transfer_len; 00206 cbw.Flags = flags; 00207 cbw.LUN = 0; 00208 cbw.CBLength = cmd_len; 00209 memset(cbw.CB,0,sizeof(cbw.CB)); 00210 if (cmd) { 00211 memcpy(cbw.CB,cmd,cmd_len); 00212 } 00213 00214 // send the cbw 00215 USB_DBG("Send CBW"); 00216 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); 00217 if (checkResult(res, bulk_out)) 00218 return -1; 00219 00220 // data stage if needed 00221 if (data) { 00222 USB_DBG("data stage"); 00223 if (flags == HOST_TO_DEVICE) { 00224 00225 res = host->bulkWrite(dev, bulk_out, data, transfer_len); 00226 if (checkResult(res, bulk_out)) 00227 return -1; 00228 00229 } else if (flags == DEVICE_TO_HOST) { 00230 00231 res = host->bulkRead(dev, bulk_in, data, transfer_len); 00232 if (checkResult(res, bulk_in)) 00233 return -1; 00234 } 00235 } 00236 00237 // status stage 00238 csw.Signature = 0; 00239 USB_DBG("Read CSW"); 00240 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); 00241 if (checkResult(res, bulk_in)) 00242 return -1; 00243 00244 if (csw.Signature != CSW_SIGNATURE) { 00245 return -1; 00246 } 00247 00248 USB_DBG("recv csw: status: %d", csw.Status); 00249 00250 // ModeSense? 00251 if ((csw.Status == 1) && (cmd[0] != 0x03)) { 00252 USB_DBG("request mode sense"); 00253 return SCSIRequestSense(); 00254 } 00255 00256 // perform reset recovery 00257 if ((csw.Status == 2) && (cmd[0] != 0x03)) { 00258 00259 // send Bulk-Only Mass Storage Reset request 00260 res = host->controlWrite( dev, 00261 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, 00262 BO_MASS_STORAGE_RESET, 00263 0, msd_intf, NULL, 0); 00264 00265 // unstall both endpoints 00266 res = host->controlWrite( dev, 00267 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00268 CLEAR_FEATURE, 00269 0, bulk_in->getAddress(), NULL, 0); 00270 00271 res = host->controlWrite( dev, 00272 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00273 CLEAR_FEATURE, 00274 0, bulk_out->getAddress(), NULL, 0); 00275 00276 } 00277 00278 return csw.Status; 00279 } 00280 00281 00282 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { 00283 uint8_t cmd[10]; 00284 memset(cmd,0,10); 00285 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; 00286 00287 cmd[2] = (block >> 24) & 0xff; 00288 cmd[3] = (block >> 16) & 0xff; 00289 cmd[4] = (block >> 8) & 0xff; 00290 cmd[5] = block & 0xff; 00291 00292 cmd[7] = (nbBlock >> 8) & 0xff; 00293 cmd[8] = nbBlock & 0xff; 00294 00295 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); 00296 } 00297 00298 int USBHostMSD::getMaxLun() { 00299 uint8_t buf[1], res; 00300 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, 00301 0xfe, 0, msd_intf, buf, 1); 00302 USB_DBG("max lun: %d", buf[0]); 00303 return res; 00304 } 00305 00306 int USBHostMSD::disk_initialize() { 00307 USB_DBG("FILESYSTEM: init"); 00308 uint16_t i, timeout = 10; 00309 00310 getMaxLun(); 00311 00312 for (i = 0; i < timeout; i++) { 00313 Thread::wait(100); 00314 if (!testUnitReady()) 00315 break; 00316 } 00317 00318 if (i == timeout) { 00319 disk_init = false; 00320 return -1; 00321 } 00322 00323 inquiry(0, 0); 00324 disk_init = 1; 00325 return readCapacity(); 00326 } 00327 00328 int USBHostMSD::disk_write(const uint8_t* buffer, uint64_t block_number, uint8_t count) { 00329 USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count); 00330 if (!disk_init) { 00331 disk_initialize(); 00332 } 00333 if (!disk_init) 00334 return -1; 00335 for (uint64_t b = block_number; b < block_number + count; b++) { 00336 if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE)) 00337 return -1; 00338 buffer += 512; 00339 } 00340 return 0; 00341 } 00342 00343 int USBHostMSD::disk_read(uint8_t* buffer, uint64_t block_number, uint8_t count) { 00344 USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count); 00345 if (!disk_init) { 00346 disk_initialize(); 00347 } 00348 if (!disk_init) 00349 return -1; 00350 for (uint64_t b = block_number; b < block_number + count; b++) { 00351 if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST)) 00352 return -1; 00353 buffer += 512; 00354 } 00355 return 0; 00356 } 00357 00358 uint64_t USBHostMSD::disk_sectors() { 00359 USB_DBG("FILESYSTEM: sectors"); 00360 if (!disk_init) { 00361 disk_initialize(); 00362 } 00363 if (!disk_init) 00364 return 0; 00365 return blockCount; 00366 } 00367 00368 #endif
Generated on Wed Jul 20 2022 23:25:25 by
1.7.2
