Adaptation of the official mbed USBHost repository to work with the LPC4088 Display Module

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_USBHost by EmbeddedArtists AB

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostMSD.cpp Source File

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