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