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:
jamike
Date:
Wed Apr 26 20:08:31 2017 +0000
Revision:
6:d3ac9e1c0035
Parent:
5:fc157e6bd5a5
Child:
8:3e7a33f81048
fix error in read and write

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 #include "dbg.h"
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 21
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 22 #define CBW_SIGNATURE 0x43425355
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 23 #define CSW_SIGNATURE 0x53425355
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 24
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 25 #define DEVICE_TO_HOST 0x80
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 26 #define HOST_TO_DEVICE 0x00
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 27
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 28 #define GET_MAX_LUN (0xFE)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 29 #define BO_MASS_STORAGE_RESET (0xFF)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 30
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 31 USBHostMSD::USBHostMSD()
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 32 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 33 host = USBHost::getHostInst();
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 34 /* register an object in FAT */
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 35
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 36 init_usb();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 37 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 38
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 39 void USBHostMSD::init_usb()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 40 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 41 dev_connected = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 42 dev = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 43 bulk_in = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 44 bulk_out = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 45 dev_connected = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 46 blockSize = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 47 blockCount = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 48 msd_intf = -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 49 msd_device_found = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 50 disk_init = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 51 dev_connected = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 52 nb_ep = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 53 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 54
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 55
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 56 bool USBHostMSD::connected()
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 57 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 58 return dev_connected;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 59 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 60
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 61 bool USBHostMSD::connect()
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 62 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 63
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 64 if (dev_connected) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 65 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 66 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 67
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 68 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 69 if ((dev = host->getDevice(i)) != NULL) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 70
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 71 USB_DBG("Trying to connect MSD device\r\n");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 72
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 73 if(host->enumerate(dev, this))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 74 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 75
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 76 if (msd_device_found) {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 77 /* As this is done in a specific thread
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 78 * this lock is taken to avoid to process a disconnection in
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 79 * usb process during the device registering */
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 80 USBHost::Lock Lock(host);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 81
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 82 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 83 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 84
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 85 if (!bulk_in || !bulk_out)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 86 continue;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 87
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 88 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 89 dev->setName("MSD", msd_intf);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 90 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init_usb);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 91
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 92 dev_connected = true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 93 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 94 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 95 } //if()
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 96 } //for()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 97 init_usb();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 98 return false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 99 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 100
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 101 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 102 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 103 // we don't check VID/PID for MSD driver
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 104 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 105
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 106 /*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 107 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 108 if ((msd_intf == -1) &&
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 109 (intf_class == MSD_CLASS) &&
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 110 (intf_subclass == 0x06) &&
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 111 (intf_protocol == 0x50)) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 112 msd_intf = intf_nb;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 113 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 114 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 115 return false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 116 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 117
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 118 /*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 119 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 120 if (intf_nb == msd_intf) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 121 if (type == BULK_ENDPOINT) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 122 nb_ep++;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 123 if (nb_ep == 2)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 124 msd_device_found = true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 125 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 126 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 127 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 128 return false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 129 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 130
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 131
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 132 int USBHostMSD::testUnitReady()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 133 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 134 USB_DBG("Test unit ready");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 135 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 136 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 137
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 138
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 139 int USBHostMSD::readCapacity()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 140 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 141 USB_DBG("Read capacity");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 142 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 143 uint8_t result[8];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 144 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 145 if (status == 0) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 146 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 147 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7];
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 148 USB_INFO("MSD [dev: %p] - blockCount: %u, blockSize: %d, Capacity: %d\r\n", dev, blockCount, blockSize, blockCount*blockSize);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 149 }
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 5:fc157e6bd5a5 154 int USBHostMSD::SCSIRequestSense()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 155 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 156 USB_DBG("Request sense");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 157 uint8_t cmd[6] = {0x03,0,0,0,18,0};
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 158 uint8_t result[18];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 159 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 160 return status;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 161 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 162
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 163
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 164 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 165 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 166 USB_DBG("Inquiry");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 167 uint8_t evpd = (page_code == 0) ? 0 : 1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 168 uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0};
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 169 uint8_t result[36];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 170 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 171 if (status == 0) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 172 char vid_pid[17];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 173 memcpy(vid_pid, &result[8], 8);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 174 vid_pid[8] = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 175 USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 176
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 177 memcpy(vid_pid, &result[16], 16);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 178 vid_pid[16] = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 179 USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 180
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 181 memcpy(vid_pid, &result[32], 4);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 182 vid_pid[4] = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 183 USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 184 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 185 return status;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 186 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 187
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 188 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 189 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 190 // if ep stalled: send clear feature
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 191 if (res == USB_TYPE_STALL_ERROR) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 192 res = host->controlWrite( dev,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 193 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 194 CLEAR_FEATURE,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 195 0, ep->getAddress(), NULL, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 196 // set state to IDLE if clear feature successful
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 197 if (res == USB_TYPE_OK) {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 198 USBHost::Lock Lock(host);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 199 ep->setState(USB_TYPE_IDLE);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 200 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 201 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 202
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 203 if (res != USB_TYPE_OK)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 204 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 205
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 206 return 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 207 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 208
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 209
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 210 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 211 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 212
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 213 int res = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 214
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 215 cbw.Signature = CBW_SIGNATURE;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 216 cbw.Tag = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 217 cbw.DataLength = transfer_len;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 218 cbw.Flags = flags;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 219 cbw.LUN = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 220 cbw.CBLength = cmd_len;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 221 memset(cbw.CB,0,sizeof(cbw.CB));
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 222 if (cmd) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 223 memcpy(cbw.CB,cmd,cmd_len);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 224 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 225
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 226 // send the cbw
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 227 USB_DBG("Send CBW");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 228 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 229 if (checkResult(res, bulk_out))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 230 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 231
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 232 // data stage if needed
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 233 if (data) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 234 USB_DBG("data stage");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 235 if (flags == HOST_TO_DEVICE) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 236
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 237 res = host->bulkWrite(dev, bulk_out, data, transfer_len);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 238 if (checkResult(res, bulk_out))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 239 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 240
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 241 } else if (flags == DEVICE_TO_HOST) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 242
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 243 res = host->bulkRead(dev, bulk_in, data, transfer_len);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 244 if (checkResult(res, bulk_in))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 245 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 246 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 247 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 248
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 249 // status stage
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 250 csw.Signature = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 251 USB_DBG("Read CSW");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 252 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 253 if (checkResult(res, bulk_in))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 254 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 255
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 256 if (csw.Signature != CSW_SIGNATURE) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 257 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 258 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 259
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 260 USB_DBG("recv csw: status: %d", csw.Status);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 261
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 262 // ModeSense?
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 263 if ((csw.Status == 1) && (cmd[0] != 0x03)) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 264 USB_DBG("request mode sense");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 265 return SCSIRequestSense();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 266 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 267
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 268 // perform reset recovery
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 269 if ((csw.Status == 2) && (cmd[0] != 0x03)) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 270
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 271 // send Bulk-Only Mass Storage Reset request
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 272 res = host->controlWrite( dev,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 273 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 274 BO_MASS_STORAGE_RESET,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 275 0, msd_intf, NULL, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 276
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 277 // unstall both endpoints
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 278 res = host->controlWrite( dev,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 279 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 280 CLEAR_FEATURE,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 281 0, bulk_in->getAddress(), NULL, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 282
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 283 res = host->controlWrite( dev,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 284 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 285 CLEAR_FEATURE,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 286 0, bulk_out->getAddress(), NULL, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 287
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 288 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 289
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 290 return csw.Status;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 291 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 292
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 293
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 294 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 295 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 296 uint8_t cmd[10];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 297 memset(cmd,0,10);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 298 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 299
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 300 cmd[2] = (block >> 24) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 301 cmd[3] = (block >> 16) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 302 cmd[4] = (block >> 8) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 303 cmd[5] = block & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 304
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 305 cmd[7] = (nbBlock >> 8) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 306 cmd[8] = nbBlock & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 307
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 308 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 309 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 310
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 311 int USBHostMSD::getMaxLun()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 312 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 313 uint8_t buf[1], res;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 314 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 315 0xfe, 0, msd_intf, buf, 1);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 316 USB_DBG("max lun: %d", buf[0]);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 317 return res;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 318 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 319
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 320 int USBHostMSD::init()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 321 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 322 USB_DBG("FILESYSTEM: init");
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 323 uint16_t i, timeout = 10, ret;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 324 getMaxLun();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 325 for (i = 0; i < timeout; i++) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 326 Thread::wait(100);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 327 if (!testUnitReady())
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 328 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 329 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 330
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 331 if (i == timeout) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 332 disk_init = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 333 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 334 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 335
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 336 inquiry(0, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 337 disk_init = 1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 338 return readCapacity();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 339 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 340
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 341 int USBHostMSD::program(const void *buffer, bd_addr_t addr, bd_size_t size)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 342 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 343 uint32_t block_number, count;
jamike 6:d3ac9e1c0035 344 uint8_t *buf = (uint8_t *)buffer;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 345 if (!disk_init) {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 346 init();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 347 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 348 if (!disk_init)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 349 return -1;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 350 block_number = addr / blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 351 count = size /blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 352
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 353 for (uint32_t b = block_number; b < block_number + count; b++) {
jamike 6:d3ac9e1c0035 354 if (dataTransfer(buf, b, 1, HOST_TO_DEVICE))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 355 return -1;
jamike 6:d3ac9e1c0035 356 buf += blockSize;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 357 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 358 return 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 359 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 360
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 361 int USBHostMSD::read(void *buffer, bd_addr_t addr, bd_size_t size)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 362 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 363 uint32_t block_number, count;
jamike 6:d3ac9e1c0035 364 uint8_t *buf = (uint8_t *)buffer;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 365 if (!disk_init) {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 366 init();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 367 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 368 if (!disk_init)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 369 return -1;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 370 block_number = addr / blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 371 count = size /blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 372
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 373 for (uint32_t b = block_number; b < block_number + count; b++) {
jamike 6:d3ac9e1c0035 374 if (dataTransfer((uint8_t*)buf, b, 1, DEVICE_TO_HOST))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 375 return -1;
jamike 6:d3ac9e1c0035 376 buf += blockSize;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 377 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 378 return 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 379 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 380
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 381 int USBHostMSD::erase(bd_addr_t addr, bd_size_t size)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 382 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 383 return 0;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 384 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 385
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 386 bd_size_t USBHostMSD::get_read_size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 387 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 388 if (!disk_init)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 389 return -1;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 390 return (bd_size_t)blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 391 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 392
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 393 bd_size_t USBHostMSD::get_program_size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 394 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 395 if (!disk_init)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 396 return -1;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 397 return (bd_size_t)blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 398 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 399 bd_size_t USBHostMSD::get_erase_size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 400 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 401 if (!disk_init)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 402 return -1;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 403 return (bd_size_t)blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 404 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 405
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 406 bd_size_t USBHostMSD::size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 407 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 408 USB_DBG("FILESYSTEM: size ");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 409 if (!disk_init)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 410 return 0;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 411 return (bd_size_t)blockCount*(bd_size_t)blockSize;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 412 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 413 #endif