USBHost custom library.
Dependents: GR-PEACH_HVC-P2_sample_client mbed-os-storage-access GR-PEACH_Digital_Signage GR-PEACH_Audio_Playback_Sample ... more
Fork of USBHost by
USBHostMSD/USBHostMSD.cpp@39:4a7c5479dc23, 2017-03-16 (annotated)
- Committer:
- dkato
- Date:
- Thu Mar 16 04:38:43 2017 +0000
- Revision:
- 39:4a7c5479dc23
- Parent:
- 33:6c4cab4d356f
Supports mbed os 5.4.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 8:93da8ea2708b | 1 | /* mbed USBHost Library |
samux | 8:93da8ea2708b | 2 | * Copyright (c) 2006-2013 ARM Limited |
samux | 8:93da8ea2708b | 3 | * |
samux | 8:93da8ea2708b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
samux | 8:93da8ea2708b | 5 | * you may not use this file except in compliance with the License. |
samux | 8:93da8ea2708b | 6 | * You may obtain a copy of the License at |
samux | 8:93da8ea2708b | 7 | * |
samux | 8:93da8ea2708b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
samux | 8:93da8ea2708b | 9 | * |
samux | 8:93da8ea2708b | 10 | * Unless required by applicable law or agreed to in writing, software |
samux | 8:93da8ea2708b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
samux | 8:93da8ea2708b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
samux | 8:93da8ea2708b | 13 | * See the License for the specific language governing permissions and |
samux | 8:93da8ea2708b | 14 | * limitations under the License. |
samux | 8:93da8ea2708b | 15 | */ |
mbed_official | 0:a554658735bf | 16 | |
mbed_official | 0:a554658735bf | 17 | #include "USBHostMSD.h" |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #if USBHOST_MSD |
mbed_official | 0:a554658735bf | 20 | |
mbed_official | 0:a554658735bf | 21 | #include "dbg.h" |
mbed_official | 0:a554658735bf | 22 | |
mbed_official | 0:a554658735bf | 23 | #define CBW_SIGNATURE 0x43425355 |
mbed_official | 0:a554658735bf | 24 | #define CSW_SIGNATURE 0x53425355 |
mbed_official | 0:a554658735bf | 25 | |
mbed_official | 0:a554658735bf | 26 | #define DEVICE_TO_HOST 0x80 |
mbed_official | 0:a554658735bf | 27 | #define HOST_TO_DEVICE 0x00 |
mbed_official | 0:a554658735bf | 28 | |
samux | 4:b320d68e98e7 | 29 | #define GET_MAX_LUN (0xFE) |
samux | 4:b320d68e98e7 | 30 | #define BO_MASS_STORAGE_RESET (0xFF) |
mbed_official | 0:a554658735bf | 31 | |
dkato | 39:4a7c5479dc23 | 32 | USBHostMSD::USBHostMSD() |
mbed_official | 0:a554658735bf | 33 | { |
mbed_official | 0:a554658735bf | 34 | host = USBHost::getHostInst(); |
dkato | 39:4a7c5479dc23 | 35 | msd_init(); |
mbed_official | 0:a554658735bf | 36 | } |
mbed_official | 0:a554658735bf | 37 | |
dkato | 39:4a7c5479dc23 | 38 | USBHostMSD::~USBHostMSD() |
dkato | 39:4a7c5479dc23 | 39 | { |
dkato | 39:4a7c5479dc23 | 40 | if (_is_initialized) { |
dkato | 39:4a7c5479dc23 | 41 | deinit(); |
dkato | 39:4a7c5479dc23 | 42 | } |
dkato | 39:4a7c5479dc23 | 43 | } |
dkato | 39:4a7c5479dc23 | 44 | |
dkato | 39:4a7c5479dc23 | 45 | void USBHostMSD::msd_init() { |
mbed_official | 0:a554658735bf | 46 | dev_connected = false; |
mbed_official | 0:a554658735bf | 47 | dev = NULL; |
mbed_official | 0:a554658735bf | 48 | bulk_in = NULL; |
mbed_official | 0:a554658735bf | 49 | bulk_out = NULL; |
mbed_official | 0:a554658735bf | 50 | dev_connected = false; |
mbed_official | 0:a554658735bf | 51 | blockSize = 0; |
mbed_official | 0:a554658735bf | 52 | blockCount = 0; |
mbed_official | 0:a554658735bf | 53 | msd_intf = -1; |
mbed_official | 0:a554658735bf | 54 | msd_device_found = false; |
dkato | 39:4a7c5479dc23 | 55 | _is_initialized = false; |
mbed_official | 0:a554658735bf | 56 | dev_connected = false; |
samux | 4:b320d68e98e7 | 57 | nb_ep = 0; |
mbed_official | 0:a554658735bf | 58 | } |
mbed_official | 0:a554658735bf | 59 | |
mbed_official | 0:a554658735bf | 60 | |
mbed_official | 0:a554658735bf | 61 | bool USBHostMSD::connected() |
mbed_official | 0:a554658735bf | 62 | { |
mbed_official | 0:a554658735bf | 63 | return dev_connected; |
mbed_official | 0:a554658735bf | 64 | } |
mbed_official | 0:a554658735bf | 65 | |
mbed_official | 0:a554658735bf | 66 | bool USBHostMSD::connect() |
mbed_official | 0:a554658735bf | 67 | { |
mbed_official | 0:a554658735bf | 68 | |
mbed_official | 0:a554658735bf | 69 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 70 | return true; |
mbed_official | 0:a554658735bf | 71 | } |
mbed_official | 0:a554658735bf | 72 | |
samux | 5:e48791a1ef18 | 73 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 74 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 24:868cbfe611a7 | 75 | |
samux | 4:b320d68e98e7 | 76 | USB_DBG("Trying to connect MSD device\r\n"); |
mbed_official | 24:868cbfe611a7 | 77 | |
mbed_official | 0:a554658735bf | 78 | if(host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 79 | break; |
mbed_official | 0:a554658735bf | 80 | |
mbed_official | 0:a554658735bf | 81 | if (msd_device_found) { |
mbed_official | 0:a554658735bf | 82 | bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 83 | bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT); |
mbed_official | 24:868cbfe611a7 | 84 | |
mbed_official | 0:a554658735bf | 85 | if (!bulk_in || !bulk_out) |
samux | 4:b320d68e98e7 | 86 | continue; |
mbed_official | 24:868cbfe611a7 | 87 | |
samux | 4:b320d68e98e7 | 88 | USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf); |
samux | 4:b320d68e98e7 | 89 | dev->setName("MSD", msd_intf); |
dkato | 39:4a7c5479dc23 | 90 | host->registerDriver(dev, msd_intf, this, &USBHostMSD::msd_init); |
mbed_official | 0:a554658735bf | 91 | |
mbed_official | 0:a554658735bf | 92 | dev_connected = true; |
mbed_official | 0:a554658735bf | 93 | return true; |
mbed_official | 0:a554658735bf | 94 | } |
mbed_official | 0:a554658735bf | 95 | } //if() |
mbed_official | 0:a554658735bf | 96 | } //for() |
dkato | 39:4a7c5479dc23 | 97 | msd_init(); |
mbed_official | 0:a554658735bf | 98 | return false; |
mbed_official | 0:a554658735bf | 99 | } |
mbed_official | 0:a554658735bf | 100 | |
mbed_official | 0:a554658735bf | 101 | /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 102 | { |
mbed_official | 0:a554658735bf | 103 | // we don't check VID/PID for MSD driver |
mbed_official | 0:a554658735bf | 104 | } |
mbed_official | 0:a554658735bf | 105 | |
mbed_official | 0:a554658735bf | 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 |
mbed_official | 0:a554658735bf | 107 | { |
mbed_official | 0:a554658735bf | 108 | if ((msd_intf == -1) && |
mbed_official | 0:a554658735bf | 109 | (intf_class == MSD_CLASS) && |
mbed_official | 0:a554658735bf | 110 | (intf_subclass == 0x06) && |
mbed_official | 0:a554658735bf | 111 | (intf_protocol == 0x50)) { |
mbed_official | 0:a554658735bf | 112 | msd_intf = intf_nb; |
mbed_official | 0:a554658735bf | 113 | return true; |
mbed_official | 0:a554658735bf | 114 | } |
mbed_official | 0:a554658735bf | 115 | return false; |
mbed_official | 0:a554658735bf | 116 | } |
mbed_official | 0:a554658735bf | 117 | |
mbed_official | 0:a554658735bf | 118 | /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
mbed_official | 0:a554658735bf | 119 | { |
mbed_official | 0:a554658735bf | 120 | if (intf_nb == msd_intf) { |
mbed_official | 0:a554658735bf | 121 | if (type == BULK_ENDPOINT) { |
samux | 4:b320d68e98e7 | 122 | nb_ep++; |
samux | 4:b320d68e98e7 | 123 | if (nb_ep == 2) |
samux | 4:b320d68e98e7 | 124 | msd_device_found = true; |
mbed_official | 0:a554658735bf | 125 | return true; |
mbed_official | 0:a554658735bf | 126 | } |
mbed_official | 0:a554658735bf | 127 | } |
mbed_official | 0:a554658735bf | 128 | return false; |
mbed_official | 0:a554658735bf | 129 | } |
mbed_official | 0:a554658735bf | 130 | |
mbed_official | 0:a554658735bf | 131 | |
mbed_official | 0:a554658735bf | 132 | int USBHostMSD::testUnitReady() { |
samux | 4:b320d68e98e7 | 133 | USB_DBG("Test unit ready"); |
mbed_official | 0:a554658735bf | 134 | return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0); |
mbed_official | 0:a554658735bf | 135 | } |
mbed_official | 0:a554658735bf | 136 | |
samux | 4:b320d68e98e7 | 137 | |
mbed_official | 0:a554658735bf | 138 | int USBHostMSD::readCapacity() { |
samux | 4:b320d68e98e7 | 139 | USB_DBG("Read capacity"); |
mbed_official | 0:a554658735bf | 140 | uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0}; |
mbed_official | 0:a554658735bf | 141 | uint8_t result[8]; |
mbed_official | 0:a554658735bf | 142 | int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8); |
mbed_official | 0:a554658735bf | 143 | if (status == 0) { |
mbed_official | 0:a554658735bf | 144 | blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]; |
mbed_official | 0:a554658735bf | 145 | blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7]; |
samux | 4:b320d68e98e7 | 146 | USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize); |
mbed_official | 0:a554658735bf | 147 | } |
mbed_official | 0:a554658735bf | 148 | return status; |
mbed_official | 0:a554658735bf | 149 | } |
mbed_official | 0:a554658735bf | 150 | |
mbed_official | 0:a554658735bf | 151 | |
mbed_official | 0:a554658735bf | 152 | int USBHostMSD::SCSIRequestSense() { |
samux | 4:b320d68e98e7 | 153 | USB_DBG("Request sense"); |
samux | 4:b320d68e98e7 | 154 | uint8_t cmd[6] = {0x03,0,0,0,18,0}; |
samux | 4:b320d68e98e7 | 155 | uint8_t result[18]; |
samux | 4:b320d68e98e7 | 156 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18); |
mbed_official | 0:a554658735bf | 157 | return status; |
mbed_official | 0:a554658735bf | 158 | } |
mbed_official | 0:a554658735bf | 159 | |
mbed_official | 0:a554658735bf | 160 | |
mbed_official | 0:a554658735bf | 161 | int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) { |
samux | 4:b320d68e98e7 | 162 | USB_DBG("Inquiry"); |
mbed_official | 0:a554658735bf | 163 | uint8_t evpd = (page_code == 0) ? 0 : 1; |
mbed_official | 13:b58a2204422f | 164 | uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0}; |
mbed_official | 0:a554658735bf | 165 | uint8_t result[36]; |
mbed_official | 0:a554658735bf | 166 | int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36); |
mbed_official | 0:a554658735bf | 167 | if (status == 0) { |
samux | 4:b320d68e98e7 | 168 | char vid_pid[17]; |
mbed_official | 0:a554658735bf | 169 | memcpy(vid_pid, &result[8], 8); |
mbed_official | 0:a554658735bf | 170 | vid_pid[8] = 0; |
mbed_official | 0:a554658735bf | 171 | USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 172 | |
samux | 4:b320d68e98e7 | 173 | memcpy(vid_pid, &result[16], 16); |
samux | 4:b320d68e98e7 | 174 | vid_pid[16] = 0; |
samux | 4:b320d68e98e7 | 175 | USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 176 | |
mbed_official | 0:a554658735bf | 177 | memcpy(vid_pid, &result[32], 4); |
mbed_official | 0:a554658735bf | 178 | vid_pid[4] = 0; |
mbed_official | 0:a554658735bf | 179 | USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid); |
mbed_official | 0:a554658735bf | 180 | } |
mbed_official | 0:a554658735bf | 181 | return status; |
mbed_official | 0:a554658735bf | 182 | } |
mbed_official | 0:a554658735bf | 183 | |
mbed_official | 0:a554658735bf | 184 | int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) { |
mbed_official | 0:a554658735bf | 185 | // if ep stalled: send clear feature |
mbed_official | 0:a554658735bf | 186 | if (res == USB_TYPE_STALL_ERROR) { |
mbed_official | 0:a554658735bf | 187 | res = host->controlWrite( dev, |
mbed_official | 0:a554658735bf | 188 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
mbed_official | 0:a554658735bf | 189 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 190 | 0, ep->getAddress(), NULL, 0); |
mbed_official | 0:a554658735bf | 191 | // set state to IDLE if clear feature successful |
mbed_official | 0:a554658735bf | 192 | if (res == USB_TYPE_OK) { |
mbed_official | 0:a554658735bf | 193 | ep->setState(USB_TYPE_IDLE); |
mbed_official | 0:a554658735bf | 194 | } |
mbed_official | 0:a554658735bf | 195 | } |
mbed_official | 0:a554658735bf | 196 | |
mbed_official | 0:a554658735bf | 197 | if (res != USB_TYPE_OK) |
mbed_official | 0:a554658735bf | 198 | return -1; |
mbed_official | 0:a554658735bf | 199 | |
mbed_official | 0:a554658735bf | 200 | return 0; |
mbed_official | 0:a554658735bf | 201 | } |
mbed_official | 0:a554658735bf | 202 | |
mbed_official | 0:a554658735bf | 203 | |
mbed_official | 0:a554658735bf | 204 | int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) { |
mbed_official | 0:a554658735bf | 205 | |
mbed_official | 0:a554658735bf | 206 | int res = 0; |
mbed_official | 0:a554658735bf | 207 | |
mbed_official | 0:a554658735bf | 208 | cbw.Signature = CBW_SIGNATURE; |
mbed_official | 0:a554658735bf | 209 | cbw.Tag = 0; |
mbed_official | 0:a554658735bf | 210 | cbw.DataLength = transfer_len; |
mbed_official | 0:a554658735bf | 211 | cbw.Flags = flags; |
mbed_official | 0:a554658735bf | 212 | cbw.LUN = 0; |
mbed_official | 0:a554658735bf | 213 | cbw.CBLength = cmd_len; |
mbed_official | 0:a554658735bf | 214 | memset(cbw.CB,0,sizeof(cbw.CB)); |
mbed_official | 0:a554658735bf | 215 | if (cmd) { |
mbed_official | 0:a554658735bf | 216 | memcpy(cbw.CB,cmd,cmd_len); |
mbed_official | 0:a554658735bf | 217 | } |
mbed_official | 0:a554658735bf | 218 | |
mbed_official | 0:a554658735bf | 219 | // send the cbw |
samux | 4:b320d68e98e7 | 220 | USB_DBG("Send CBW"); |
mbed_official | 0:a554658735bf | 221 | res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31); |
mbed_official | 0:a554658735bf | 222 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 223 | return -1; |
mbed_official | 0:a554658735bf | 224 | |
mbed_official | 0:a554658735bf | 225 | // data stage if needed |
mbed_official | 0:a554658735bf | 226 | if (data) { |
samux | 4:b320d68e98e7 | 227 | USB_DBG("data stage"); |
mbed_official | 0:a554658735bf | 228 | if (flags == HOST_TO_DEVICE) { |
mbed_official | 24:868cbfe611a7 | 229 | |
mbed_official | 0:a554658735bf | 230 | res = host->bulkWrite(dev, bulk_out, data, transfer_len); |
mbed_official | 0:a554658735bf | 231 | if (checkResult(res, bulk_out)) |
mbed_official | 0:a554658735bf | 232 | return -1; |
mbed_official | 0:a554658735bf | 233 | |
mbed_official | 0:a554658735bf | 234 | } else if (flags == DEVICE_TO_HOST) { |
mbed_official | 0:a554658735bf | 235 | |
mbed_official | 0:a554658735bf | 236 | res = host->bulkRead(dev, bulk_in, data, transfer_len); |
mbed_official | 0:a554658735bf | 237 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 238 | return -1; |
mbed_official | 0:a554658735bf | 239 | } |
mbed_official | 0:a554658735bf | 240 | } |
mbed_official | 0:a554658735bf | 241 | |
mbed_official | 0:a554658735bf | 242 | // status stage |
mbed_official | 0:a554658735bf | 243 | csw.Signature = 0; |
samux | 4:b320d68e98e7 | 244 | USB_DBG("Read CSW"); |
mbed_official | 0:a554658735bf | 245 | res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13); |
mbed_official | 0:a554658735bf | 246 | if (checkResult(res, bulk_in)) |
mbed_official | 0:a554658735bf | 247 | return -1; |
mbed_official | 0:a554658735bf | 248 | |
mbed_official | 0:a554658735bf | 249 | if (csw.Signature != CSW_SIGNATURE) { |
mbed_official | 0:a554658735bf | 250 | return -1; |
mbed_official | 0:a554658735bf | 251 | } |
mbed_official | 24:868cbfe611a7 | 252 | |
samux | 4:b320d68e98e7 | 253 | USB_DBG("recv csw: status: %d", csw.Status); |
mbed_official | 0:a554658735bf | 254 | |
mbed_official | 0:a554658735bf | 255 | // ModeSense? |
samux | 4:b320d68e98e7 | 256 | if ((csw.Status == 1) && (cmd[0] != 0x03)) { |
samux | 4:b320d68e98e7 | 257 | USB_DBG("request mode sense"); |
mbed_official | 0:a554658735bf | 258 | return SCSIRequestSense(); |
samux | 4:b320d68e98e7 | 259 | } |
mbed_official | 24:868cbfe611a7 | 260 | |
samux | 4:b320d68e98e7 | 261 | // perform reset recovery |
samux | 4:b320d68e98e7 | 262 | if ((csw.Status == 2) && (cmd[0] != 0x03)) { |
mbed_official | 24:868cbfe611a7 | 263 | |
samux | 4:b320d68e98e7 | 264 | // send Bulk-Only Mass Storage Reset request |
samux | 4:b320d68e98e7 | 265 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 266 | USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 267 | BO_MASS_STORAGE_RESET, |
samux | 4:b320d68e98e7 | 268 | 0, msd_intf, NULL, 0); |
mbed_official | 24:868cbfe611a7 | 269 | |
samux | 4:b320d68e98e7 | 270 | // unstall both endpoints |
samux | 4:b320d68e98e7 | 271 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 272 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 273 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 274 | 0, bulk_in->getAddress(), NULL, 0); |
mbed_official | 24:868cbfe611a7 | 275 | |
samux | 4:b320d68e98e7 | 276 | res = host->controlWrite( dev, |
samux | 4:b320d68e98e7 | 277 | USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, |
samux | 4:b320d68e98e7 | 278 | CLEAR_FEATURE, |
samux | 4:b320d68e98e7 | 279 | 0, bulk_out->getAddress(), NULL, 0); |
mbed_official | 24:868cbfe611a7 | 280 | |
samux | 4:b320d68e98e7 | 281 | } |
mbed_official | 0:a554658735bf | 282 | |
mbed_official | 0:a554658735bf | 283 | return csw.Status; |
mbed_official | 0:a554658735bf | 284 | } |
mbed_official | 0:a554658735bf | 285 | |
mbed_official | 0:a554658735bf | 286 | |
mbed_official | 0:a554658735bf | 287 | int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) { |
mbed_official | 0:a554658735bf | 288 | uint8_t cmd[10]; |
mbed_official | 0:a554658735bf | 289 | memset(cmd,0,10); |
mbed_official | 0:a554658735bf | 290 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
mbed_official | 0:a554658735bf | 291 | |
mbed_official | 0:a554658735bf | 292 | cmd[2] = (block >> 24) & 0xff; |
mbed_official | 0:a554658735bf | 293 | cmd[3] = (block >> 16) & 0xff; |
mbed_official | 0:a554658735bf | 294 | cmd[4] = (block >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 295 | cmd[5] = block & 0xff; |
mbed_official | 0:a554658735bf | 296 | |
mbed_official | 0:a554658735bf | 297 | cmd[7] = (nbBlock >> 8) & 0xff; |
mbed_official | 0:a554658735bf | 298 | cmd[8] = nbBlock & 0xff; |
mbed_official | 0:a554658735bf | 299 | |
mbed_official | 0:a554658735bf | 300 | return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock); |
mbed_official | 0:a554658735bf | 301 | } |
mbed_official | 0:a554658735bf | 302 | |
samux | 4:b320d68e98e7 | 303 | int USBHostMSD::getMaxLun() { |
samux | 4:b320d68e98e7 | 304 | uint8_t buf[1], res; |
samux | 4:b320d68e98e7 | 305 | res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
samux | 4:b320d68e98e7 | 306 | 0xfe, 0, msd_intf, buf, 1); |
samux | 4:b320d68e98e7 | 307 | USB_DBG("max lun: %d", buf[0]); |
samux | 4:b320d68e98e7 | 308 | return res; |
samux | 4:b320d68e98e7 | 309 | } |
mbed_official | 0:a554658735bf | 310 | |
dkato | 39:4a7c5479dc23 | 311 | #define USB_BLOCK_DEVICE_ERROR_WOULD_BLOCK -5001 /*!< operation would block */ |
dkato | 39:4a7c5479dc23 | 312 | #define USB_BLOCK_DEVICE_ERROR_UNSUPPORTED -5002 /*!< unsupported operation */ |
dkato | 39:4a7c5479dc23 | 313 | #define USB_BLOCK_DEVICE_ERROR_PARAMETER -5003 /*!< invalid parameter */ |
dkato | 39:4a7c5479dc23 | 314 | #define USB_BLOCK_DEVICE_ERROR_NO_INIT -5004 /*!< uninitialized */ |
dkato | 39:4a7c5479dc23 | 315 | #define USB_BLOCK_DEVICE_ERROR_NO_DEVICE -5005 /*!< device is missing or not connected */ |
dkato | 39:4a7c5479dc23 | 316 | #define USB_BLOCK_DEVICE_ERROR_WRITE_PROTECTED -5006 /*!< write protected */ |
dkato | 39:4a7c5479dc23 | 317 | |
dkato | 39:4a7c5479dc23 | 318 | int USBHostMSD::init() { |
mbed_official | 27:4206883f4cb7 | 319 | uint16_t i, timeout = 10; |
mbed_official | 24:868cbfe611a7 | 320 | |
dkato | 39:4a7c5479dc23 | 321 | _lock.lock(); |
samux | 4:b320d68e98e7 | 322 | getMaxLun(); |
mbed_official | 24:868cbfe611a7 | 323 | |
mbed_official | 0:a554658735bf | 324 | for (i = 0; i < timeout; i++) { |
samux | 4:b320d68e98e7 | 325 | Thread::wait(100); |
mbed_official | 0:a554658735bf | 326 | if (!testUnitReady()) |
mbed_official | 0:a554658735bf | 327 | break; |
mbed_official | 0:a554658735bf | 328 | } |
mbed_official | 24:868cbfe611a7 | 329 | |
mbed_official | 0:a554658735bf | 330 | if (i == timeout) { |
dkato | 39:4a7c5479dc23 | 331 | return BD_ERROR_DEVICE_ERROR; |
mbed_official | 0:a554658735bf | 332 | } |
mbed_official | 24:868cbfe611a7 | 333 | |
mbed_official | 0:a554658735bf | 334 | inquiry(0, 0); |
dkato | 39:4a7c5479dc23 | 335 | if (readCapacity() != 0) { |
dkato | 39:4a7c5479dc23 | 336 | _lock.unlock(); |
dkato | 39:4a7c5479dc23 | 337 | return BD_ERROR_DEVICE_ERROR; |
dkato | 39:4a7c5479dc23 | 338 | } |
dkato | 39:4a7c5479dc23 | 339 | _is_initialized = true; |
dkato | 39:4a7c5479dc23 | 340 | _lock.unlock(); |
dkato | 39:4a7c5479dc23 | 341 | |
dkato | 39:4a7c5479dc23 | 342 | return BD_ERROR_OK; |
dkato | 39:4a7c5479dc23 | 343 | } |
dkato | 39:4a7c5479dc23 | 344 | |
dkato | 39:4a7c5479dc23 | 345 | int USBHostMSD::deinit() { |
dkato | 39:4a7c5479dc23 | 346 | return 0; |
mbed_official | 0:a554658735bf | 347 | } |
mbed_official | 0:a554658735bf | 348 | |
dkato | 39:4a7c5479dc23 | 349 | int USBHostMSD::program(const void *b, bd_addr_t addr, bd_size_t size) |
dkato | 39:4a7c5479dc23 | 350 | { |
dkato | 39:4a7c5479dc23 | 351 | if (!is_valid_program(addr, size)) { |
dkato | 39:4a7c5479dc23 | 352 | return USB_BLOCK_DEVICE_ERROR_PARAMETER; |
dkato | 39:4a7c5479dc23 | 353 | } |
dkato | 39:4a7c5479dc23 | 354 | |
dkato | 39:4a7c5479dc23 | 355 | _lock.lock(); |
dkato | 39:4a7c5479dc23 | 356 | if (!_is_initialized) { |
dkato | 39:4a7c5479dc23 | 357 | _lock.unlock(); |
dkato | 39:4a7c5479dc23 | 358 | return USB_BLOCK_DEVICE_ERROR_NO_INIT; |
mbed_official | 0:a554658735bf | 359 | } |
dkato | 39:4a7c5479dc23 | 360 | |
dkato | 39:4a7c5479dc23 | 361 | const uint8_t *buffer = static_cast<const uint8_t*>(b); |
dkato | 39:4a7c5479dc23 | 362 | while (size > 0) { |
dkato | 39:4a7c5479dc23 | 363 | bd_addr_t block = addr / 512; |
dkato | 39:4a7c5479dc23 | 364 | |
dkato | 39:4a7c5479dc23 | 365 | // send the data block |
dkato | 39:4a7c5479dc23 | 366 | if (dataTransfer((uint8_t*)buffer, block, 1, HOST_TO_DEVICE)) { |
dkato | 39:4a7c5479dc23 | 367 | _lock.unlock(); |
dkato | 39:4a7c5479dc23 | 368 | return BD_ERROR_DEVICE_ERROR; |
dkato | 39:4a7c5479dc23 | 369 | } |
dkato | 39:4a7c5479dc23 | 370 | |
mbed_official | 26:607951c26872 | 371 | buffer += 512; |
dkato | 39:4a7c5479dc23 | 372 | addr += 512; |
dkato | 39:4a7c5479dc23 | 373 | size -= 512; |
mbed_official | 26:607951c26872 | 374 | } |
dkato | 39:4a7c5479dc23 | 375 | _lock.unlock(); |
mbed_official | 26:607951c26872 | 376 | return 0; |
mbed_official | 0:a554658735bf | 377 | } |
mbed_official | 0:a554658735bf | 378 | |
dkato | 39:4a7c5479dc23 | 379 | int USBHostMSD::read(void *b, bd_addr_t addr, bd_size_t size) |
dkato | 39:4a7c5479dc23 | 380 | { |
dkato | 39:4a7c5479dc23 | 381 | if (!is_valid_read(addr, size)) { |
dkato | 39:4a7c5479dc23 | 382 | return USB_BLOCK_DEVICE_ERROR_PARAMETER; |
dkato | 39:4a7c5479dc23 | 383 | } |
dkato | 39:4a7c5479dc23 | 384 | |
dkato | 39:4a7c5479dc23 | 385 | _lock.lock(); |
dkato | 39:4a7c5479dc23 | 386 | if (!_is_initialized) { |
dkato | 39:4a7c5479dc23 | 387 | _lock.unlock(); |
dkato | 39:4a7c5479dc23 | 388 | return USB_BLOCK_DEVICE_ERROR_PARAMETER; |
mbed_official | 0:a554658735bf | 389 | } |
dkato | 39:4a7c5479dc23 | 390 | |
dkato | 39:4a7c5479dc23 | 391 | uint8_t *buffer = static_cast<uint8_t *>(b); |
dkato | 39:4a7c5479dc23 | 392 | while (size > 0) { |
dkato | 39:4a7c5479dc23 | 393 | bd_addr_t block = addr / 512; |
dkato | 39:4a7c5479dc23 | 394 | |
dkato | 39:4a7c5479dc23 | 395 | // receive the data |
dkato | 39:4a7c5479dc23 | 396 | if (dataTransfer((uint8_t*)buffer, block, 1, DEVICE_TO_HOST)) { |
dkato | 39:4a7c5479dc23 | 397 | _lock.unlock(); |
dkato | 39:4a7c5479dc23 | 398 | return BD_ERROR_DEVICE_ERROR; |
dkato | 39:4a7c5479dc23 | 399 | } |
mbed_official | 26:607951c26872 | 400 | buffer += 512; |
dkato | 39:4a7c5479dc23 | 401 | addr += 512; |
dkato | 39:4a7c5479dc23 | 402 | size -= 512; |
mbed_official | 26:607951c26872 | 403 | } |
dkato | 39:4a7c5479dc23 | 404 | _lock.unlock(); |
mbed_official | 26:607951c26872 | 405 | return 0; |
mbed_official | 0:a554658735bf | 406 | } |
mbed_official | 0:a554658735bf | 407 | |
dkato | 39:4a7c5479dc23 | 408 | int USBHostMSD::erase(bd_addr_t addr, bd_size_t size) |
dkato | 39:4a7c5479dc23 | 409 | { |
dkato | 39:4a7c5479dc23 | 410 | return 0; |
dkato | 39:4a7c5479dc23 | 411 | } |
dkato | 39:4a7c5479dc23 | 412 | |
dkato | 39:4a7c5479dc23 | 413 | bd_size_t USBHostMSD::get_read_size() const |
dkato | 39:4a7c5479dc23 | 414 | { |
dkato | 39:4a7c5479dc23 | 415 | return 512; |
dkato | 39:4a7c5479dc23 | 416 | } |
dkato | 39:4a7c5479dc23 | 417 | |
dkato | 39:4a7c5479dc23 | 418 | bd_size_t USBHostMSD::get_program_size() const |
dkato | 39:4a7c5479dc23 | 419 | { |
dkato | 39:4a7c5479dc23 | 420 | return 512; |
dkato | 39:4a7c5479dc23 | 421 | } |
dkato | 39:4a7c5479dc23 | 422 | |
dkato | 39:4a7c5479dc23 | 423 | bd_size_t USBHostMSD::get_erase_size() const |
dkato | 39:4a7c5479dc23 | 424 | { |
dkato | 39:4a7c5479dc23 | 425 | return 512; |
dkato | 39:4a7c5479dc23 | 426 | } |
dkato | 39:4a7c5479dc23 | 427 | |
dkato | 39:4a7c5479dc23 | 428 | bd_size_t USBHostMSD::size() const |
dkato | 39:4a7c5479dc23 | 429 | { |
dkato | 39:4a7c5479dc23 | 430 | bd_size_t sectors = 0; |
dkato | 39:4a7c5479dc23 | 431 | if(_is_initialized) { |
dkato | 39:4a7c5479dc23 | 432 | sectors = blockCount; |
mbed_official | 0:a554658735bf | 433 | } |
dkato | 39:4a7c5479dc23 | 434 | return 512*sectors; |
dkato | 39:4a7c5479dc23 | 435 | } |
dkato | 39:4a7c5479dc23 | 436 | |
dkato | 39:4a7c5479dc23 | 437 | void USBHostMSD::debug(bool dbg) |
dkato | 39:4a7c5479dc23 | 438 | { |
dkato | 39:4a7c5479dc23 | 439 | // _dbg = dbg; |
mbed_official | 0:a554658735bf | 440 | } |
mbed_official | 0:a554658735bf | 441 | |
mbed_official | 0:a554658735bf | 442 | #endif |
dkato | 39:4a7c5479dc23 | 443 |