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:
kenjiArai
Date:
Sat Jan 04 23:30:59 2020 +0000
Revision:
8:3e7a33f81048
Parent:
6:d3ac9e1c0035
updated for STM32F4xx

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 1:ab240722d7ef 198 ep->setState(USB_TYPE_IDLE);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 199 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 200 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 201
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 202 if (res != USB_TYPE_OK)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 203 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 204
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 205 return 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 206 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 207
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 208
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 209 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 210 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 211
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 212 int res = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 213
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 214 cbw.Signature = CBW_SIGNATURE;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 215 cbw.Tag = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 216 cbw.DataLength = transfer_len;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 217 cbw.Flags = flags;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 218 cbw.LUN = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 219 cbw.CBLength = cmd_len;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 220 memset(cbw.CB,0,sizeof(cbw.CB));
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 221 if (cmd) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 222 memcpy(cbw.CB,cmd,cmd_len);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 223 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 224
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 225 // send the cbw
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 226 USB_DBG("Send CBW");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 227 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 228 if (checkResult(res, bulk_out))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 229 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 230
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 231 // data stage if needed
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 232 if (data) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 233 USB_DBG("data stage");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 234 if (flags == HOST_TO_DEVICE) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 235
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 236 res = host->bulkWrite(dev, bulk_out, data, transfer_len);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 237 if (checkResult(res, bulk_out))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 238 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 239
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 240 } else if (flags == DEVICE_TO_HOST) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 241
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 242 res = host->bulkRead(dev, bulk_in, data, transfer_len);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 243 if (checkResult(res, bulk_in))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 244 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 245 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 246 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 247
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 248 // status stage
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 249 csw.Signature = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 250 USB_DBG("Read CSW");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 251 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 252 if (checkResult(res, bulk_in))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 253 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 254
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 255 if (csw.Signature != CSW_SIGNATURE) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 256 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 257 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 258
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 259 USB_DBG("recv csw: status: %d", csw.Status);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 260
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 261 // ModeSense?
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 262 if ((csw.Status == 1) && (cmd[0] != 0x03)) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 263 USB_DBG("request mode sense");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 264 return SCSIRequestSense();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 265 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 266
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 267 // perform reset recovery
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 268 if ((csw.Status == 2) && (cmd[0] != 0x03)) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 269
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 270 // send Bulk-Only Mass Storage Reset request
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 271 res = host->controlWrite( dev,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 272 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 273 BO_MASS_STORAGE_RESET,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 274 0, msd_intf, NULL, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 275
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 276 // unstall both endpoints
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 277 res = host->controlWrite( dev,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 278 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 279 CLEAR_FEATURE,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 280 0, bulk_in->getAddress(), NULL, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 281
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 282 res = host->controlWrite( dev,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 283 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 284 CLEAR_FEATURE,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 285 0, bulk_out->getAddress(), NULL, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 286
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 287 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 288
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 289 return csw.Status;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 290 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 291
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 292
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 293 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 294 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 295 uint8_t cmd[10];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 296 memset(cmd,0,10);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 297 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 298
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 299 cmd[2] = (block >> 24) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 300 cmd[3] = (block >> 16) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 301 cmd[4] = (block >> 8) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 302 cmd[5] = block & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 303
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 304 cmd[7] = (nbBlock >> 8) & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 305 cmd[8] = nbBlock & 0xff;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 306
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 307 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 308 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 309
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 310 int USBHostMSD::getMaxLun()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 311 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 312 uint8_t buf[1], res;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 313 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 314 0xfe, 0, msd_intf, buf, 1);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 315 USB_DBG("max lun: %d", buf[0]);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 316 return res;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 317 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 318
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 319 int USBHostMSD::init()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 320 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 321 USB_DBG("FILESYSTEM: init");
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 322 uint16_t i, timeout = 10, ret;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 323 getMaxLun();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 324 for (i = 0; i < timeout; i++) {
kenjiArai 8:3e7a33f81048 325 ThisThread::sleep_for(100);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 326 if (!testUnitReady())
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 327 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 328 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 329
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 330 if (i == timeout) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 331 disk_init = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 332 return -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 333 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 334
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 335 inquiry(0, 0);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 336 disk_init = 1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 337 return readCapacity();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 338 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 339
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 340 int USBHostMSD::program(const void *buffer, bd_addr_t addr, bd_size_t size)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 341 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 342 uint32_t block_number, count;
jamike 6:d3ac9e1c0035 343 uint8_t *buf = (uint8_t *)buffer;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 344 if (!disk_init) {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 345 init();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 346 }
kenjiArai 8:3e7a33f81048 347 if (!disk_init) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 348 return -1;
kenjiArai 8:3e7a33f81048 349 }
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 }
kenjiArai 8:3e7a33f81048 368 if (!disk_init) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 369 return -1;
kenjiArai 8:3e7a33f81048 370 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 371 block_number = addr / blockSize;
kenjiArai 8:3e7a33f81048 372 count = size / blockSize;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 373
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 374 for (uint32_t b = block_number; b < block_number + count; b++) {
kenjiArai 8:3e7a33f81048 375 if (dataTransfer(buf, b, 1, DEVICE_TO_HOST))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 376 return -1;
jamike 6:d3ac9e1c0035 377 buf += blockSize;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 378 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 379 return 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 380 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 381
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 382 int USBHostMSD::erase(bd_addr_t addr, bd_size_t size)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 383 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 384 return 0;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 385 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 386
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 387 bd_size_t USBHostMSD::get_read_size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 388 {
kenjiArai 8:3e7a33f81048 389 return (disk_init ? (bd_size_t)blockSize : -1);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 390 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 391
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 392 bd_size_t USBHostMSD::get_program_size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 393 {
kenjiArai 8:3e7a33f81048 394 return (disk_init ? (bd_size_t)blockSize : -1);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 395 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 396 bd_size_t USBHostMSD::get_erase_size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 397 {
kenjiArai 8:3e7a33f81048 398 return (disk_init ? (bd_size_t)blockSize : -1);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 399 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 400
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 401 bd_size_t USBHostMSD::size() const
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 402 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 403 USB_DBG("FILESYSTEM: size ");
kenjiArai 8:3e7a33f81048 404 return (disk_init ? (bd_size_t)blockSize : 0);
kenjiArai 8:3e7a33f81048 405 }
kenjiArai 8:3e7a33f81048 406
kenjiArai 8:3e7a33f81048 407 const char *USBHostMSD::get_type() const
kenjiArai 8:3e7a33f81048 408 {
kenjiArai 8:3e7a33f81048 409 return "USBMSD";
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 410 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 411 #endif