USBDevice with compilation error in USBSerial fixed.

Fork of USBDevice by Maxim Integrated

Committer:
bogdanm
Date:
Mon Aug 05 14:13:36 2013 +0300
Revision:
11:eeb3cbbaa996
Parent:
8:335f2506f422
Child:
14:d495202c90f4
Bug fixes, added suppor for LPC1347

Author: Samuel Mokrani

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:80ab0d068708 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 1:80ab0d068708 2 *
samux 1:80ab0d068708 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:80ab0d068708 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 1:80ab0d068708 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 1:80ab0d068708 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 1:80ab0d068708 7 * Software is furnished to do so, subject to the following conditions:
samux 1:80ab0d068708 8 *
samux 1:80ab0d068708 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:80ab0d068708 10 * substantial portions of the Software.
samux 1:80ab0d068708 11 *
samux 1:80ab0d068708 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:80ab0d068708 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:80ab0d068708 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:80ab0d068708 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:80ab0d068708 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:80ab0d068708 17 */
samux 1:80ab0d068708 18
samux 1:80ab0d068708 19 #include "stdint.h"
samux 1:80ab0d068708 20 #include "USBMSD.h"
samux 1:80ab0d068708 21
samux 1:80ab0d068708 22 #define DISK_OK 0x00
samux 1:80ab0d068708 23 #define NO_INIT 0x01
samux 1:80ab0d068708 24 #define NO_DISK 0x02
samux 1:80ab0d068708 25 #define WRITE_PROTECT 0x04
samux 1:80ab0d068708 26
samux 1:80ab0d068708 27 #define CBW_Signature 0x43425355
samux 1:80ab0d068708 28 #define CSW_Signature 0x53425355
samux 1:80ab0d068708 29
samux 1:80ab0d068708 30 // SCSI Commands
samux 1:80ab0d068708 31 #define TEST_UNIT_READY 0x00
samux 1:80ab0d068708 32 #define REQUEST_SENSE 0x03
samux 1:80ab0d068708 33 #define FORMAT_UNIT 0x04
samux 1:80ab0d068708 34 #define INQUIRY 0x12
samux 1:80ab0d068708 35 #define MODE_SELECT6 0x15
samux 1:80ab0d068708 36 #define MODE_SENSE6 0x1A
samux 1:80ab0d068708 37 #define START_STOP_UNIT 0x1B
samux 1:80ab0d068708 38 #define MEDIA_REMOVAL 0x1E
samux 1:80ab0d068708 39 #define READ_FORMAT_CAPACITIES 0x23
samux 1:80ab0d068708 40 #define READ_CAPACITY 0x25
samux 1:80ab0d068708 41 #define READ10 0x28
samux 1:80ab0d068708 42 #define WRITE10 0x2A
samux 1:80ab0d068708 43 #define VERIFY10 0x2F
samux 1:80ab0d068708 44 #define READ12 0xA8
samux 1:80ab0d068708 45 #define WRITE12 0xAA
samux 1:80ab0d068708 46 #define MODE_SELECT10 0x55
samux 1:80ab0d068708 47 #define MODE_SENSE10 0x5A
samux 1:80ab0d068708 48
samux 1:80ab0d068708 49 // MSC class specific requests
samux 1:80ab0d068708 50 #define MSC_REQUEST_RESET 0xFF
samux 1:80ab0d068708 51 #define MSC_REQUEST_GET_MAX_LUN 0xFE
samux 1:80ab0d068708 52
samux 1:80ab0d068708 53 #define DEFAULT_CONFIGURATION (1)
samux 1:80ab0d068708 54
samux 1:80ab0d068708 55 // max packet size
samux 1:80ab0d068708 56 #define MAX_PACKET MAX_PACKET_SIZE_EPBULK
samux 1:80ab0d068708 57
samux 1:80ab0d068708 58 // CSW Status
samux 1:80ab0d068708 59 enum Status {
samux 1:80ab0d068708 60 CSW_PASSED,
samux 1:80ab0d068708 61 CSW_FAILED,
samux 1:80ab0d068708 62 CSW_ERROR,
samux 1:80ab0d068708 63 };
samux 1:80ab0d068708 64
samux 1:80ab0d068708 65
samux 1:80ab0d068708 66 USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
samux 5:d27e4c226965 67 stage = READ_CBW;
samux 5:d27e4c226965 68 memset((void *)&cbw, 0, sizeof(CBW));
samux 5:d27e4c226965 69 memset((void *)&csw, 0, sizeof(CSW));
samux 1:80ab0d068708 70 }
samux 1:80ab0d068708 71
samux 1:80ab0d068708 72
samux 1:80ab0d068708 73
samux 1:80ab0d068708 74 // Called in ISR context to process a class specific request
samux 1:80ab0d068708 75 bool USBMSD::USBCallback_request(void) {
samux 1:80ab0d068708 76
samux 1:80ab0d068708 77 bool success = false;
samux 1:80ab0d068708 78 CONTROL_TRANSFER * transfer = getTransferPtr();
samux 1:80ab0d068708 79 static uint8_t maxLUN[1] = {0};
samux 1:80ab0d068708 80
samux 1:80ab0d068708 81 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
samux 1:80ab0d068708 82 switch (transfer->setup.bRequest) {
samux 1:80ab0d068708 83 case MSC_REQUEST_RESET:
samux 1:80ab0d068708 84 reset();
samux 1:80ab0d068708 85 success = true;
samux 1:80ab0d068708 86 break;
samux 1:80ab0d068708 87 case MSC_REQUEST_GET_MAX_LUN:
samux 1:80ab0d068708 88 transfer->remaining = 1;
samux 1:80ab0d068708 89 transfer->ptr = maxLUN;
samux 1:80ab0d068708 90 transfer->direction = DEVICE_TO_HOST;
samux 1:80ab0d068708 91 success = true;
samux 1:80ab0d068708 92 break;
samux 1:80ab0d068708 93 default:
samux 1:80ab0d068708 94 break;
samux 1:80ab0d068708 95 }
samux 1:80ab0d068708 96 }
samux 1:80ab0d068708 97
samux 1:80ab0d068708 98 return success;
samux 1:80ab0d068708 99 }
samux 1:80ab0d068708 100
samux 1:80ab0d068708 101
samux 1:80ab0d068708 102 bool USBMSD::connect() {
samux 1:80ab0d068708 103
samux 1:80ab0d068708 104 //disk initialization
samux 1:80ab0d068708 105 if (disk_status() & NO_INIT) {
samux 1:80ab0d068708 106 if (disk_initialize()) {
samux 1:80ab0d068708 107 return false;
samux 1:80ab0d068708 108 }
samux 1:80ab0d068708 109 }
samux 1:80ab0d068708 110
samux 1:80ab0d068708 111 // get number of blocks
samux 1:80ab0d068708 112 BlockCount = disk_sectors();
samux 1:80ab0d068708 113
samux 1:80ab0d068708 114 // get memory size
samux 1:80ab0d068708 115 MemorySize = disk_size();
samux 1:80ab0d068708 116
samux 7:f8f057664123 117 if (BlockCount > 0) {
samux 1:80ab0d068708 118 BlockSize = MemorySize / BlockCount;
samux 1:80ab0d068708 119 if (BlockSize != 0) {
samux 1:80ab0d068708 120 page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
samux 1:80ab0d068708 121 if (page == NULL)
samux 1:80ab0d068708 122 return false;
samux 1:80ab0d068708 123 }
samux 1:80ab0d068708 124 } else {
samux 1:80ab0d068708 125 return false;
samux 1:80ab0d068708 126 }
samux 1:80ab0d068708 127
samux 1:80ab0d068708 128 //connect the device
samux 1:80ab0d068708 129 USBDevice::connect();
samux 1:80ab0d068708 130 return true;
samux 1:80ab0d068708 131 }
samux 1:80ab0d068708 132
samux 1:80ab0d068708 133
samux 1:80ab0d068708 134 void USBMSD::reset() {
samux 1:80ab0d068708 135 stage = READ_CBW;
samux 1:80ab0d068708 136 }
samux 1:80ab0d068708 137
samux 1:80ab0d068708 138
samux 1:80ab0d068708 139 // Called in ISR context called when a data is received
samux 1:80ab0d068708 140 bool USBMSD::EP2_OUT_callback() {
samux 1:80ab0d068708 141 uint32_t size = 0;
samux 1:80ab0d068708 142 uint8_t buf[MAX_PACKET_SIZE_EPBULK];
samux 1:80ab0d068708 143 readEP(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK);
samux 1:80ab0d068708 144 switch (stage) {
samux 1:80ab0d068708 145 // the device has to decode the CBW received
samux 1:80ab0d068708 146 case READ_CBW:
samux 1:80ab0d068708 147 CBWDecode(buf, size);
samux 1:80ab0d068708 148 break;
samux 1:80ab0d068708 149
samux 1:80ab0d068708 150 // the device has to receive data from the host
samux 1:80ab0d068708 151 case PROCESS_CBW:
samux 1:80ab0d068708 152 switch (cbw.CB[0]) {
samux 1:80ab0d068708 153 case WRITE10:
samux 1:80ab0d068708 154 case WRITE12:
samux 1:80ab0d068708 155 memoryWrite(buf, size);
samux 1:80ab0d068708 156 break;
samux 1:80ab0d068708 157 case VERIFY10:
samux 1:80ab0d068708 158 memoryVerify(buf, size);
samux 1:80ab0d068708 159 break;
samux 1:80ab0d068708 160 }
samux 1:80ab0d068708 161 break;
samux 1:80ab0d068708 162
samux 1:80ab0d068708 163 // an error has occured: stall endpoint and send CSW
samux 1:80ab0d068708 164 default:
samux 1:80ab0d068708 165 stallEndpoint(EPBULK_OUT);
samux 1:80ab0d068708 166 csw.Status = CSW_ERROR;
samux 1:80ab0d068708 167 sendCSW();
samux 1:80ab0d068708 168 break;
samux 1:80ab0d068708 169 }
samux 1:80ab0d068708 170
samux 1:80ab0d068708 171 //reactivate readings on the OUT bulk endpoint
samux 1:80ab0d068708 172 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 1:80ab0d068708 173 return true;
samux 1:80ab0d068708 174 }
samux 1:80ab0d068708 175
samux 1:80ab0d068708 176 // Called in ISR context when a data has been transferred
samux 1:80ab0d068708 177 bool USBMSD::EP2_IN_callback() {
samux 1:80ab0d068708 178 switch (stage) {
samux 1:80ab0d068708 179
samux 1:80ab0d068708 180 // the device has to send data to the host
samux 1:80ab0d068708 181 case PROCESS_CBW:
samux 1:80ab0d068708 182 switch (cbw.CB[0]) {
samux 1:80ab0d068708 183 case READ10:
samux 1:80ab0d068708 184 case READ12:
samux 1:80ab0d068708 185 memoryRead();
samux 1:80ab0d068708 186 break;
samux 1:80ab0d068708 187 }
samux 1:80ab0d068708 188 break;
samux 1:80ab0d068708 189
samux 1:80ab0d068708 190 //the device has to send a CSW
samux 1:80ab0d068708 191 case SEND_CSW:
samux 1:80ab0d068708 192 sendCSW();
samux 1:80ab0d068708 193 break;
samux 1:80ab0d068708 194
bogdanm 11:eeb3cbbaa996 195 // the host has received the CSW -> we wait a CBW
bogdanm 11:eeb3cbbaa996 196 case WAIT_CSW:
bogdanm 11:eeb3cbbaa996 197 stage = READ_CBW;
bogdanm 11:eeb3cbbaa996 198 break;
bogdanm 11:eeb3cbbaa996 199
bogdanm 11:eeb3cbbaa996 200 // an error has occured
bogdanm 11:eeb3cbbaa996 201 default:
samux 1:80ab0d068708 202 stallEndpoint(EPBULK_IN);
samux 1:80ab0d068708 203 sendCSW();
samux 1:80ab0d068708 204 break;
samux 1:80ab0d068708 205 }
samux 1:80ab0d068708 206 return true;
samux 1:80ab0d068708 207 }
samux 1:80ab0d068708 208
samux 1:80ab0d068708 209
samux 1:80ab0d068708 210 void USBMSD::memoryWrite (uint8_t * buf, uint16_t size) {
samux 1:80ab0d068708 211
samux 1:80ab0d068708 212 if ((addr + size) > MemorySize) {
samux 1:80ab0d068708 213 size = MemorySize - addr;
samux 1:80ab0d068708 214 stage = ERROR;
samux 1:80ab0d068708 215 stallEndpoint(EPBULK_OUT);
samux 1:80ab0d068708 216 }
samux 1:80ab0d068708 217
samux 1:80ab0d068708 218 // we fill an array in RAM of 1 block before writing it in memory
samux 1:80ab0d068708 219 for (int i = 0; i < size; i++)
samux 1:80ab0d068708 220 page[addr%BlockSize + i] = buf[i];
samux 1:80ab0d068708 221
samux 1:80ab0d068708 222 // if the array is filled, write it in memory
samux 1:80ab0d068708 223 if (!((addr + size)%BlockSize)) {
samux 1:80ab0d068708 224 if (!(disk_status() & WRITE_PROTECT)) {
samux 7:f8f057664123 225 disk_write(page, addr/BlockSize);
samux 1:80ab0d068708 226 }
samux 1:80ab0d068708 227 }
samux 1:80ab0d068708 228
samux 1:80ab0d068708 229 addr += size;
samux 1:80ab0d068708 230 length -= size;
samux 1:80ab0d068708 231 csw.DataResidue -= size;
samux 1:80ab0d068708 232
samux 1:80ab0d068708 233 if ((!length) || (stage != PROCESS_CBW)) {
samux 1:80ab0d068708 234 csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED;
samux 1:80ab0d068708 235 sendCSW();
samux 1:80ab0d068708 236 }
samux 1:80ab0d068708 237 }
samux 1:80ab0d068708 238
samux 1:80ab0d068708 239 void USBMSD::memoryVerify (uint8_t * buf, uint16_t size) {
samux 1:80ab0d068708 240 uint32_t n;
samux 1:80ab0d068708 241
samux 1:80ab0d068708 242 if ((addr + size) > MemorySize) {
samux 1:80ab0d068708 243 size = MemorySize - addr;
samux 1:80ab0d068708 244 stage = ERROR;
samux 1:80ab0d068708 245 stallEndpoint(EPBULK_OUT);
samux 1:80ab0d068708 246 }
samux 1:80ab0d068708 247
samux 1:80ab0d068708 248 // beginning of a new block -> load a whole block in RAM
samux 1:80ab0d068708 249 if (!(addr%BlockSize))
samux 7:f8f057664123 250 disk_read(page, addr/BlockSize);
samux 1:80ab0d068708 251
samux 1:80ab0d068708 252 // info are in RAM -> no need to re-read memory
samux 1:80ab0d068708 253 for (n = 0; n < size; n++) {
samux 1:80ab0d068708 254 if (page[addr%BlockSize + n] != buf[n]) {
samux 1:80ab0d068708 255 memOK = false;
samux 1:80ab0d068708 256 break;
samux 1:80ab0d068708 257 }
samux 1:80ab0d068708 258 }
samux 1:80ab0d068708 259
samux 1:80ab0d068708 260 addr += size;
samux 1:80ab0d068708 261 length -= size;
samux 1:80ab0d068708 262 csw.DataResidue -= size;
samux 1:80ab0d068708 263
samux 1:80ab0d068708 264 if ( !length || (stage != PROCESS_CBW)) {
samux 1:80ab0d068708 265 csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED;
samux 1:80ab0d068708 266 sendCSW();
samux 1:80ab0d068708 267 }
samux 1:80ab0d068708 268 }
samux 1:80ab0d068708 269
samux 1:80ab0d068708 270
samux 1:80ab0d068708 271 bool USBMSD::inquiryRequest (void) {
samux 1:80ab0d068708 272 uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01,
samux 1:80ab0d068708 273 36 - 4, 0x80, 0x00, 0x00,
samux 1:80ab0d068708 274 'M', 'B', 'E', 'D', '.', 'O', 'R', 'G',
samux 1:80ab0d068708 275 'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ',
samux 1:80ab0d068708 276 '1', '.', '0', ' ',
samux 1:80ab0d068708 277 };
samux 1:80ab0d068708 278 if (!write(inquiry, sizeof(inquiry))) {
samux 1:80ab0d068708 279 return false;
samux 1:80ab0d068708 280 }
samux 1:80ab0d068708 281 return true;
samux 1:80ab0d068708 282 }
samux 1:80ab0d068708 283
samux 1:80ab0d068708 284
samux 1:80ab0d068708 285 bool USBMSD::readFormatCapacity() {
samux 1:80ab0d068708 286 uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,
bogdanm 11:eeb3cbbaa996 287 (uint8_t)((BlockCount >> 24) & 0xff),
bogdanm 11:eeb3cbbaa996 288 (uint8_t)((BlockCount >> 16) & 0xff),
bogdanm 11:eeb3cbbaa996 289 (uint8_t)((BlockCount >> 8) & 0xff),
bogdanm 11:eeb3cbbaa996 290 (uint8_t)((BlockCount >> 0) & 0xff),
samux 1:80ab0d068708 291
samux 1:80ab0d068708 292 0x02,
bogdanm 11:eeb3cbbaa996 293 (uint8_t)((BlockSize >> 16) & 0xff),
bogdanm 11:eeb3cbbaa996 294 (uint8_t)((BlockSize >> 8) & 0xff),
bogdanm 11:eeb3cbbaa996 295 (uint8_t)((BlockSize >> 0) & 0xff),
samux 1:80ab0d068708 296 };
samux 1:80ab0d068708 297 if (!write(capacity, sizeof(capacity))) {
samux 1:80ab0d068708 298 return false;
samux 1:80ab0d068708 299 }
samux 1:80ab0d068708 300 return true;
samux 1:80ab0d068708 301 }
samux 1:80ab0d068708 302
samux 1:80ab0d068708 303
samux 1:80ab0d068708 304 bool USBMSD::readCapacity (void) {
samux 1:80ab0d068708 305 uint8_t capacity[] = {
bogdanm 11:eeb3cbbaa996 306 (uint8_t)(((BlockCount - 1) >> 24) & 0xff),
bogdanm 11:eeb3cbbaa996 307 (uint8_t)(((BlockCount - 1) >> 16) & 0xff),
bogdanm 11:eeb3cbbaa996 308 (uint8_t)(((BlockCount - 1) >> 8) & 0xff),
bogdanm 11:eeb3cbbaa996 309 (uint8_t)(((BlockCount - 1) >> 0) & 0xff),
samux 1:80ab0d068708 310
bogdanm 11:eeb3cbbaa996 311 (uint8_t)((BlockSize >> 24) & 0xff),
bogdanm 11:eeb3cbbaa996 312 (uint8_t)((BlockSize >> 16) & 0xff),
bogdanm 11:eeb3cbbaa996 313 (uint8_t)((BlockSize >> 8) & 0xff),
bogdanm 11:eeb3cbbaa996 314 (uint8_t)((BlockSize >> 0) & 0xff),
samux 1:80ab0d068708 315 };
samux 1:80ab0d068708 316 if (!write(capacity, sizeof(capacity))) {
samux 1:80ab0d068708 317 return false;
samux 1:80ab0d068708 318 }
samux 1:80ab0d068708 319 return true;
samux 1:80ab0d068708 320 }
samux 1:80ab0d068708 321
samux 1:80ab0d068708 322 bool USBMSD::write (uint8_t * buf, uint16_t size) {
samux 1:80ab0d068708 323
samux 1:80ab0d068708 324 if (size >= cbw.DataLength) {
samux 1:80ab0d068708 325 size = cbw.DataLength;
samux 1:80ab0d068708 326 }
samux 1:80ab0d068708 327 stage = SEND_CSW;
samux 1:80ab0d068708 328
samux 1:80ab0d068708 329 if (!writeNB(EPBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) {
samux 1:80ab0d068708 330 return false;
samux 1:80ab0d068708 331 }
samux 1:80ab0d068708 332
samux 1:80ab0d068708 333 csw.DataResidue -= size;
samux 1:80ab0d068708 334 csw.Status = CSW_PASSED;
samux 1:80ab0d068708 335 return true;
samux 1:80ab0d068708 336 }
samux 1:80ab0d068708 337
samux 1:80ab0d068708 338
samux 1:80ab0d068708 339 bool USBMSD::modeSense6 (void) {
samux 1:80ab0d068708 340 uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 };
samux 1:80ab0d068708 341 if (!write(sense6, sizeof(sense6))) {
samux 1:80ab0d068708 342 return false;
samux 1:80ab0d068708 343 }
samux 1:80ab0d068708 344 return true;
samux 1:80ab0d068708 345 }
samux 1:80ab0d068708 346
samux 1:80ab0d068708 347 void USBMSD::sendCSW() {
samux 1:80ab0d068708 348 csw.Signature = CSW_Signature;
samux 1:80ab0d068708 349 writeNB(EPBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK);
samux 1:80ab0d068708 350 stage = WAIT_CSW;
samux 1:80ab0d068708 351 }
samux 1:80ab0d068708 352
samux 1:80ab0d068708 353 bool USBMSD::requestSense (void) {
samux 1:80ab0d068708 354 uint8_t request_sense[] = {
samux 1:80ab0d068708 355 0x70,
samux 1:80ab0d068708 356 0x00,
samux 1:80ab0d068708 357 0x05, // Sense Key: illegal request
samux 1:80ab0d068708 358 0x00,
samux 1:80ab0d068708 359 0x00,
samux 1:80ab0d068708 360 0x00,
samux 1:80ab0d068708 361 0x00,
samux 1:80ab0d068708 362 0x0A,
samux 1:80ab0d068708 363 0x00,
samux 1:80ab0d068708 364 0x00,
samux 1:80ab0d068708 365 0x00,
samux 1:80ab0d068708 366 0x00,
samux 1:80ab0d068708 367 0x30,
samux 1:80ab0d068708 368 0x01,
samux 1:80ab0d068708 369 0x00,
samux 1:80ab0d068708 370 0x00,
samux 1:80ab0d068708 371 0x00,
samux 1:80ab0d068708 372 0x00,
samux 1:80ab0d068708 373 };
samux 1:80ab0d068708 374
samux 1:80ab0d068708 375 if (!write(request_sense, sizeof(request_sense))) {
samux 1:80ab0d068708 376 return false;
samux 1:80ab0d068708 377 }
samux 1:80ab0d068708 378
samux 1:80ab0d068708 379 return true;
samux 1:80ab0d068708 380 }
samux 1:80ab0d068708 381
samux 1:80ab0d068708 382 void USBMSD::fail() {
samux 1:80ab0d068708 383 csw.Status = CSW_FAILED;
samux 1:80ab0d068708 384 sendCSW();
samux 1:80ab0d068708 385 }
samux 1:80ab0d068708 386
samux 1:80ab0d068708 387
samux 1:80ab0d068708 388 void USBMSD::CBWDecode(uint8_t * buf, uint16_t size) {
samux 1:80ab0d068708 389 if (size == sizeof(cbw)) {
samux 1:80ab0d068708 390 memcpy((uint8_t *)&cbw, buf, size);
samux 1:80ab0d068708 391 if (cbw.Signature == CBW_Signature) {
samux 1:80ab0d068708 392 csw.Tag = cbw.Tag;
samux 1:80ab0d068708 393 csw.DataResidue = cbw.DataLength;
samux 1:80ab0d068708 394 if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) {
samux 1:80ab0d068708 395 fail();
samux 1:80ab0d068708 396 } else {
samux 1:80ab0d068708 397 switch (cbw.CB[0]) {
samux 1:80ab0d068708 398 case TEST_UNIT_READY:
samux 1:80ab0d068708 399 testUnitReady();
samux 1:80ab0d068708 400 break;
samux 1:80ab0d068708 401 case REQUEST_SENSE:
samux 1:80ab0d068708 402 requestSense();
samux 1:80ab0d068708 403 break;
samux 1:80ab0d068708 404 case INQUIRY:
samux 1:80ab0d068708 405 inquiryRequest();
samux 1:80ab0d068708 406 break;
samux 1:80ab0d068708 407 case MODE_SENSE6:
samux 1:80ab0d068708 408 modeSense6();
samux 1:80ab0d068708 409 break;
samux 1:80ab0d068708 410 case READ_FORMAT_CAPACITIES:
samux 1:80ab0d068708 411 readFormatCapacity();
samux 1:80ab0d068708 412 break;
samux 1:80ab0d068708 413 case READ_CAPACITY:
samux 1:80ab0d068708 414 readCapacity();
samux 1:80ab0d068708 415 break;
samux 1:80ab0d068708 416 case READ10:
samux 1:80ab0d068708 417 case READ12:
samux 1:80ab0d068708 418 if (infoTransfer()) {
samux 1:80ab0d068708 419 if ((cbw.Flags & 0x80)) {
samux 1:80ab0d068708 420 stage = PROCESS_CBW;
samux 1:80ab0d068708 421 memoryRead();
samux 1:80ab0d068708 422 } else {
samux 1:80ab0d068708 423 stallEndpoint(EPBULK_OUT);
samux 1:80ab0d068708 424 csw.Status = CSW_ERROR;
samux 1:80ab0d068708 425 sendCSW();
samux 1:80ab0d068708 426 }
samux 1:80ab0d068708 427 }
samux 1:80ab0d068708 428 break;
samux 1:80ab0d068708 429 case WRITE10:
samux 1:80ab0d068708 430 case WRITE12:
samux 1:80ab0d068708 431 if (infoTransfer()) {
samux 1:80ab0d068708 432 if (!(cbw.Flags & 0x80)) {
samux 1:80ab0d068708 433 stage = PROCESS_CBW;
samux 1:80ab0d068708 434 } else {
samux 1:80ab0d068708 435 stallEndpoint(EPBULK_IN);
samux 1:80ab0d068708 436 csw.Status = CSW_ERROR;
samux 1:80ab0d068708 437 sendCSW();
samux 1:80ab0d068708 438 }
samux 1:80ab0d068708 439 }
samux 1:80ab0d068708 440 break;
samux 1:80ab0d068708 441 case VERIFY10:
samux 1:80ab0d068708 442 if (!(cbw.CB[1] & 0x02)) {
samux 1:80ab0d068708 443 csw.Status = CSW_PASSED;
samux 1:80ab0d068708 444 sendCSW();
samux 1:80ab0d068708 445 break;
samux 1:80ab0d068708 446 }
samux 1:80ab0d068708 447 if (infoTransfer()) {
samux 1:80ab0d068708 448 if (!(cbw.Flags & 0x80)) {
samux 1:80ab0d068708 449 stage = PROCESS_CBW;
samux 1:80ab0d068708 450 memOK = true;
samux 1:80ab0d068708 451 } else {
samux 1:80ab0d068708 452 stallEndpoint(EPBULK_IN);
samux 1:80ab0d068708 453 csw.Status = CSW_ERROR;
samux 1:80ab0d068708 454 sendCSW();
samux 1:80ab0d068708 455 }
samux 1:80ab0d068708 456 }
samux 1:80ab0d068708 457 break;
samux 3:6d85e04fb59f 458 case MEDIA_REMOVAL:
samux 3:6d85e04fb59f 459 csw.Status = CSW_PASSED;
samux 3:6d85e04fb59f 460 sendCSW();
samux 3:6d85e04fb59f 461 break;
samux 1:80ab0d068708 462 default:
samux 1:80ab0d068708 463 fail();
samux 1:80ab0d068708 464 break;
samux 1:80ab0d068708 465 }
samux 1:80ab0d068708 466 }
samux 1:80ab0d068708 467 }
samux 1:80ab0d068708 468 }
samux 1:80ab0d068708 469 }
samux 1:80ab0d068708 470
samux 1:80ab0d068708 471 void USBMSD::testUnitReady (void) {
samux 1:80ab0d068708 472
samux 1:80ab0d068708 473 if (cbw.DataLength != 0) {
samux 1:80ab0d068708 474 if ((cbw.Flags & 0x80) != 0) {
samux 1:80ab0d068708 475 stallEndpoint(EPBULK_IN);
samux 1:80ab0d068708 476 } else {
samux 1:80ab0d068708 477 stallEndpoint(EPBULK_OUT);
samux 1:80ab0d068708 478 }
samux 1:80ab0d068708 479 }
samux 1:80ab0d068708 480
samux 1:80ab0d068708 481 csw.Status = CSW_PASSED;
samux 1:80ab0d068708 482 sendCSW();
samux 1:80ab0d068708 483 }
samux 1:80ab0d068708 484
samux 1:80ab0d068708 485
samux 1:80ab0d068708 486 void USBMSD::memoryRead (void) {
samux 1:80ab0d068708 487 uint32_t n;
samux 1:80ab0d068708 488
samux 1:80ab0d068708 489 n = (length > MAX_PACKET) ? MAX_PACKET : length;
samux 1:80ab0d068708 490
samux 1:80ab0d068708 491 if ((addr + n) > MemorySize) {
samux 1:80ab0d068708 492 n = MemorySize - addr;
samux 1:80ab0d068708 493 stage = ERROR;
samux 1:80ab0d068708 494 }
samux 1:80ab0d068708 495
samux 1:80ab0d068708 496 // we read an entire block
samux 1:80ab0d068708 497 if (!(addr%BlockSize))
samux 7:f8f057664123 498 disk_read(page, addr/BlockSize);
samux 1:80ab0d068708 499
samux 1:80ab0d068708 500 // write data which are in RAM
samux 1:80ab0d068708 501 writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);
samux 1:80ab0d068708 502
samux 1:80ab0d068708 503 addr += n;
samux 1:80ab0d068708 504 length -= n;
samux 1:80ab0d068708 505
samux 1:80ab0d068708 506 csw.DataResidue -= n;
samux 1:80ab0d068708 507
samux 1:80ab0d068708 508 if ( !length || (stage != PROCESS_CBW)) {
samux 1:80ab0d068708 509 csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;
samux 1:80ab0d068708 510 stage = (stage == PROCESS_CBW) ? SEND_CSW : stage;
samux 1:80ab0d068708 511 }
samux 1:80ab0d068708 512 }
samux 1:80ab0d068708 513
samux 1:80ab0d068708 514
samux 1:80ab0d068708 515 bool USBMSD::infoTransfer (void) {
samux 1:80ab0d068708 516 uint32_t n;
samux 1:80ab0d068708 517
samux 1:80ab0d068708 518 // Logical Block Address of First Block
samux 1:80ab0d068708 519 n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0);
samux 1:80ab0d068708 520
samux 1:80ab0d068708 521 addr = n * BlockSize;
samux 1:80ab0d068708 522
samux 1:80ab0d068708 523 // Number of Blocks to transfer
samux 1:80ab0d068708 524 switch (cbw.CB[0]) {
samux 1:80ab0d068708 525 case READ10:
samux 1:80ab0d068708 526 case WRITE10:
samux 1:80ab0d068708 527 case VERIFY10:
samux 1:80ab0d068708 528 n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0);
samux 1:80ab0d068708 529 break;
samux 1:80ab0d068708 530
samux 1:80ab0d068708 531 case READ12:
samux 1:80ab0d068708 532 case WRITE12:
samux 1:80ab0d068708 533 n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0);
samux 1:80ab0d068708 534 break;
samux 1:80ab0d068708 535 }
samux 1:80ab0d068708 536
samux 1:80ab0d068708 537 length = n * BlockSize;
samux 1:80ab0d068708 538
samux 1:80ab0d068708 539 if (!cbw.DataLength) { // host requests no data
samux 1:80ab0d068708 540 csw.Status = CSW_FAILED;
samux 1:80ab0d068708 541 sendCSW();
samux 1:80ab0d068708 542 return false;
samux 1:80ab0d068708 543 }
samux 1:80ab0d068708 544
samux 1:80ab0d068708 545 if (cbw.DataLength != length) {
samux 1:80ab0d068708 546 if ((cbw.Flags & 0x80) != 0) {
samux 1:80ab0d068708 547 stallEndpoint(EPBULK_IN);
samux 1:80ab0d068708 548 } else {
samux 1:80ab0d068708 549 stallEndpoint(EPBULK_OUT);
samux 1:80ab0d068708 550 }
samux 1:80ab0d068708 551
samux 1:80ab0d068708 552 csw.Status = CSW_FAILED;
samux 1:80ab0d068708 553 sendCSW();
samux 1:80ab0d068708 554 return false;
samux 1:80ab0d068708 555 }
samux 1:80ab0d068708 556
samux 1:80ab0d068708 557 return true;
samux 1:80ab0d068708 558 }
samux 1:80ab0d068708 559
samux 1:80ab0d068708 560
samux 1:80ab0d068708 561
samux 1:80ab0d068708 562
samux 1:80ab0d068708 563
samux 1:80ab0d068708 564 // Called in ISR context
samux 1:80ab0d068708 565 // Set configuration. Return false if the
samux 1:80ab0d068708 566 // configuration is not supported.
samux 1:80ab0d068708 567 bool USBMSD::USBCallback_setConfiguration(uint8_t configuration) {
samux 1:80ab0d068708 568 if (configuration != DEFAULT_CONFIGURATION) {
samux 1:80ab0d068708 569 return false;
samux 1:80ab0d068708 570 }
samux 1:80ab0d068708 571
samux 1:80ab0d068708 572 // Configure endpoints > 0
samux 1:80ab0d068708 573 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
samux 1:80ab0d068708 574 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 1:80ab0d068708 575
samux 1:80ab0d068708 576 //activate readings
samux 1:80ab0d068708 577 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 1:80ab0d068708 578 return true;
samux 1:80ab0d068708 579 }
samux 1:80ab0d068708 580
samux 1:80ab0d068708 581
samux 1:80ab0d068708 582 uint8_t * USBMSD::stringIinterfaceDesc() {
samux 1:80ab0d068708 583 static uint8_t stringIinterfaceDescriptor[] = {
samux 1:80ab0d068708 584 0x08, //bLength
samux 1:80ab0d068708 585 STRING_DESCRIPTOR, //bDescriptorType 0x03
samux 1:80ab0d068708 586 'M',0,'S',0,'D',0 //bString iInterface - MSD
samux 1:80ab0d068708 587 };
samux 1:80ab0d068708 588 return stringIinterfaceDescriptor;
samux 1:80ab0d068708 589 }
samux 1:80ab0d068708 590
samux 1:80ab0d068708 591 uint8_t * USBMSD::stringIproductDesc() {
samux 1:80ab0d068708 592 static uint8_t stringIproductDescriptor[] = {
samux 1:80ab0d068708 593 0x12, //bLength
samux 1:80ab0d068708 594 STRING_DESCRIPTOR, //bDescriptorType 0x03
samux 1:80ab0d068708 595 'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio
samux 1:80ab0d068708 596 };
samux 1:80ab0d068708 597 return stringIproductDescriptor;
samux 1:80ab0d068708 598 }
samux 1:80ab0d068708 599
samux 1:80ab0d068708 600
samux 1:80ab0d068708 601 uint8_t * USBMSD::configurationDesc() {
samux 1:80ab0d068708 602 static uint8_t configDescriptor[] = {
samux 1:80ab0d068708 603
samux 1:80ab0d068708 604 // Configuration 1
samux 1:80ab0d068708 605 9, // bLength
samux 1:80ab0d068708 606 2, // bDescriptorType
samux 1:80ab0d068708 607 LSB(9 + 9 + 7 + 7), // wTotalLength
samux 1:80ab0d068708 608 MSB(9 + 9 + 7 + 7),
samux 1:80ab0d068708 609 0x01, // bNumInterfaces
samux 1:80ab0d068708 610 0x01, // bConfigurationValue: 0x01 is used to select this configuration
samux 1:80ab0d068708 611 0x00, // iConfiguration: no string to describe this configuration
samux 1:80ab0d068708 612 0xC0, // bmAttributes
samux 1:80ab0d068708 613 100, // bMaxPower, device power consumption is 100 mA
samux 1:80ab0d068708 614
samux 1:80ab0d068708 615 // Interface 0, Alternate Setting 0, MSC Class
samux 1:80ab0d068708 616 9, // bLength
samux 1:80ab0d068708 617 4, // bDescriptorType
samux 1:80ab0d068708 618 0x00, // bInterfaceNumber
samux 1:80ab0d068708 619 0x00, // bAlternateSetting
samux 1:80ab0d068708 620 0x02, // bNumEndpoints
samux 1:80ab0d068708 621 0x08, // bInterfaceClass
samux 1:80ab0d068708 622 0x06, // bInterfaceSubClass
samux 1:80ab0d068708 623 0x50, // bInterfaceProtocol
samux 1:80ab0d068708 624 0x04, // iInterface
samux 1:80ab0d068708 625
samux 1:80ab0d068708 626 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
samux 1:80ab0d068708 627 7, // bLength
samux 1:80ab0d068708 628 5, // bDescriptorType
samux 1:80ab0d068708 629 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
samux 1:80ab0d068708 630 0x02, // bmAttributes (0x02=bulk)
samux 1:80ab0d068708 631 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
samux 1:80ab0d068708 632 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
samux 1:80ab0d068708 633 0, // bInterval
samux 1:80ab0d068708 634
samux 1:80ab0d068708 635 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
samux 1:80ab0d068708 636 7, // bLength
samux 1:80ab0d068708 637 5, // bDescriptorType
samux 1:80ab0d068708 638 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
samux 1:80ab0d068708 639 0x02, // bmAttributes (0x02=bulk)
samux 1:80ab0d068708 640 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
samux 1:80ab0d068708 641 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
samux 1:80ab0d068708 642 0 // bInterval
samux 1:80ab0d068708 643 };
samux 1:80ab0d068708 644 return configDescriptor;
samux 8:335f2506f422 645 }