Modified for PS3 Joystick

Dependents:   NiseKabuto

Fork of USBDevice by Samuel Mokrani

Committer:
sankichi
Date:
Sat Jul 27 14:05:26 2013 +0000
Revision:
1:ac5cca60029a
Parent:
0:140cdf8e2d60
First Release

Who changed what in which revision?

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