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