Added TARGET_DISCO_F469NI in USBHOST\USBHost\TARGET_STM\USBHALHost_STM_TARGET.h

Dependents:   DISCO-F469NI_USB_Disk STM32F4xx_USB_Memory

Fork of USBHOST by ST

Committer:
frq08711@LMECWL0871.LME.ST.COM
Date:
Fri Feb 17 12:40:57 2017 +0100
Revision:
3:1c76b46ad779
Parent:
1:ab240722d7ef
Child:
5:fc157e6bd5a5
fixes for USBMSD

Who changed what in which revision?

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