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