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