USBHost library. NOTE: This library is only officially supported on the LPC1768 platform. For more information, please see the handbook page.
Dependencies: FATFileSystem mbed-rtos
Dependents: BTstack WallbotWii SD to Flash Data Transfer USBHost-MSD_HelloWorld ... more
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 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) 00033 { 00034 host = USBHost::getHostInst(); 00035 init(); 00036 } 00037 00038 void USBHostMSD::init() { 00039 dev_connected = false; 00040 dev = NULL; 00041 bulk_in = NULL; 00042 bulk_out = NULL; 00043 dev_connected = false; 00044 blockSize = 0; 00045 blockCount = 0; 00046 msd_intf = -1; 00047 msd_device_found = false; 00048 disk_init = false; 00049 dev_connected = false; 00050 nb_ep = 0; 00051 } 00052 00053 00054 bool USBHostMSD::connected() 00055 { 00056 return dev_connected; 00057 } 00058 00059 bool USBHostMSD::connect() 00060 { 00061 00062 if (dev_connected) { 00063 return true; 00064 } 00065 00066 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00067 if ((dev = host->getDevice(i)) != NULL) { 00068 00069 USB_DBG("Trying to connect MSD device\r\n"); 00070 00071 if(host->enumerate(dev, this)) 00072 break; 00073 00074 if (msd_device_found) { 00075 /* As this is done in a specific thread 00076 * this lock is taken to avoid to process a disconnection in 00077 * usb process during the device registering */ 00078 USBHost::Lock Lock(host); 00079 00080 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); 00081 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); 00082 00083 if (!bulk_in || !bulk_out) 00084 continue; 00085 00086 USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); 00087 dev->setName("MSD", msd_intf); 00088 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init); 00089 00090 dev_connected = true; 00091 return true; 00092 } 00093 } //if() 00094 } //for() 00095 init(); 00096 return false; 00097 } 00098 00099 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) 00100 { 00101 // we don't check VID/PID for MSD driver 00102 } 00103 00104 /*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 00105 { 00106 if ((msd_intf == -1) && 00107 (intf_class == MSD_CLASS) && 00108 (intf_subclass == 0x06) && 00109 (intf_protocol == 0x50)) { 00110 msd_intf = intf_nb; 00111 return true; 00112 } 00113 return false; 00114 } 00115 00116 /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00117 { 00118 if (intf_nb == msd_intf) { 00119 if (type == BULK_ENDPOINT) { 00120 nb_ep++; 00121 if (nb_ep == 2) 00122 msd_device_found = true; 00123 return true; 00124 } 00125 } 00126 return false; 00127 } 00128 00129 00130 int USBHostMSD::testUnitReady() 00131 { 00132 USB_DBG("Test unit ready"); 00133 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); 00134 } 00135 00136 00137 int USBHostMSD::readCapacity() 00138 { 00139 USB_DBG("Read capacity"); 00140 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; 00141 uint8_t result[8]; 00142 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); 00143 if (status == 0) { 00144 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; 00145 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; 00146 USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); 00147 } 00148 return status; 00149 } 00150 00151 00152 int USBHostMSD::SCSIRequestSense() 00153 { 00154 USB_DBG("Request sense"); 00155 uint8_t cmd[6] = {0x03,0,0,0,18,0}; 00156 uint8_t result[18]; 00157 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); 00158 return status; 00159 } 00160 00161 00162 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) 00163 { 00164 USB_DBG("Inquiry"); 00165 uint8_t evpd = (page_code == 0) ? 0 : 1; 00166 uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; 00167 uint8_t result[36]; 00168 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); 00169 if (status == 0) { 00170 char vid_pid[17]; 00171 memcpy(vid_pid, &result[8], 8); 00172 vid_pid[8] = 0; 00173 USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); 00174 00175 memcpy(vid_pid, &result[16], 16); 00176 vid_pid[16] = 0; 00177 USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); 00178 00179 memcpy(vid_pid, &result[32], 4); 00180 vid_pid[4] = 0; 00181 USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); 00182 } 00183 return status; 00184 } 00185 00186 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) 00187 { 00188 // if ep stalled: send clear feature 00189 if (res == USB_TYPE_STALL_ERROR) { 00190 res = host->controlWrite( dev, 00191 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00192 CLEAR_FEATURE, 00193 0, ep->getAddress(), NULL, 0); 00194 // set state to IDLE if clear feature successful 00195 if (res == USB_TYPE_OK) { 00196 ep->setState(USB_TYPE_IDLE); 00197 } 00198 } 00199 00200 if (res != USB_TYPE_OK) 00201 return -1; 00202 00203 return 0; 00204 } 00205 00206 00207 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) 00208 { 00209 00210 int res = 0; 00211 00212 cbw.Signature = CBW_SIGNATURE; 00213 cbw.Tag = 0; 00214 cbw.DataLength = transfer_len; 00215 cbw.Flags = flags; 00216 cbw.LUN = 0; 00217 cbw.CBLength = cmd_len; 00218 memset(cbw.CB,0,sizeof(cbw.CB)); 00219 if (cmd) { 00220 memcpy(cbw.CB,cmd,cmd_len); 00221 } 00222 00223 // send the cbw 00224 USB_DBG("Send CBW"); 00225 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); 00226 if (checkResult(res, bulk_out)) 00227 return -1; 00228 00229 // data stage if needed 00230 if (data) { 00231 USB_DBG("data stage"); 00232 if (flags == HOST_TO_DEVICE) { 00233 00234 res = host->bulkWrite(dev, bulk_out, data, transfer_len); 00235 if (checkResult(res, bulk_out)) 00236 return -1; 00237 00238 } else if (flags == DEVICE_TO_HOST) { 00239 00240 res = host->bulkRead(dev, bulk_in, data, transfer_len); 00241 if (checkResult(res, bulk_in)) 00242 return -1; 00243 } 00244 } 00245 00246 // status stage 00247 csw.Signature = 0; 00248 USB_DBG("Read CSW"); 00249 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); 00250 if (checkResult(res, bulk_in)) 00251 return -1; 00252 00253 if (csw.Signature != CSW_SIGNATURE) { 00254 return -1; 00255 } 00256 00257 USB_DBG("recv csw: status: %d", csw.Status); 00258 00259 // ModeSense? 00260 if ((csw.Status == 1) && (cmd[0] != 0x03)) { 00261 USB_DBG("request mode sense"); 00262 return SCSIRequestSense(); 00263 } 00264 00265 // perform reset recovery 00266 if ((csw.Status == 2) && (cmd[0] != 0x03)) { 00267 00268 // send Bulk-Only Mass Storage Reset request 00269 res = host->controlWrite( dev, 00270 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, 00271 BO_MASS_STORAGE_RESET, 00272 0, msd_intf, NULL, 0); 00273 00274 // unstall both endpoints 00275 res = host->controlWrite( dev, 00276 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00277 CLEAR_FEATURE, 00278 0, bulk_in->getAddress(), NULL, 0); 00279 00280 res = host->controlWrite( dev, 00281 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, 00282 CLEAR_FEATURE, 00283 0, bulk_out->getAddress(), NULL, 0); 00284 00285 } 00286 00287 return csw.Status; 00288 } 00289 00290 00291 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) 00292 { 00293 uint8_t cmd[10]; 00294 memset(cmd,0,10); 00295 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; 00296 00297 cmd[2] = (block >> 24) & 0xff; 00298 cmd[3] = (block >> 16) & 0xff; 00299 cmd[4] = (block >> 8) & 0xff; 00300 cmd[5] = block & 0xff; 00301 00302 cmd[7] = (nbBlock >> 8) & 0xff; 00303 cmd[8] = nbBlock & 0xff; 00304 00305 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); 00306 } 00307 00308 int USBHostMSD::getMaxLun() 00309 { 00310 uint8_t buf[1], res; 00311 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, 00312 0xfe, 0, msd_intf, buf, 1); 00313 USB_DBG("max lun: %d", buf[0]); 00314 return res; 00315 } 00316 00317 int USBHostMSD::disk_initialize() { 00318 USB_DBG("FILESYSTEM: init"); 00319 uint16_t i, timeout = 10; 00320 00321 getMaxLun(); 00322 00323 for (i = 0; i < timeout; i++) { 00324 Thread::wait(100); 00325 if (!testUnitReady()) 00326 break; 00327 } 00328 00329 if (i == timeout) { 00330 disk_init = false; 00331 return -1; 00332 } 00333 00334 inquiry(0, 0); 00335 disk_init = 1; 00336 return readCapacity(); 00337 } 00338 00339 int USBHostMSD::disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count) { 00340 USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count); 00341 if (!disk_init) { 00342 disk_initialize(); 00343 } 00344 if (!disk_init) 00345 return -1; 00346 for (uint32_t b = block_number; b < block_number + count; b++) { 00347 if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE)) 00348 return -1; 00349 buffer += 512; 00350 } 00351 return 0; 00352 } 00353 00354 int USBHostMSD::disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count) { 00355 USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count); 00356 if (!disk_init) { 00357 disk_initialize(); 00358 } 00359 if (!disk_init) 00360 return -1; 00361 for (uint32_t b = block_number; b < block_number + count; b++) { 00362 if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST)) 00363 return -1; 00364 buffer += 512; 00365 } 00366 return 0; 00367 } 00368 00369 uint32_t USBHostMSD::disk_sectors() { 00370 USB_DBG("FILESYSTEM: sectors"); 00371 if (!disk_init) { 00372 disk_initialize(); 00373 } 00374 if (!disk_init) 00375 return 0; 00376 return blockCount; 00377 } 00378 00379 #endif
Generated on Tue Jul 12 2022 13:32:26 by 1.7.2