Dieter Graef / USBHost_DISCO-F746NG

Dependents:   DISCO-F746NG_USB_Host

Fork of KL46Z-USBHost by Norimasa Okamoto

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