Samuel Mokrani
/
USBMSD_SD_HID_HelloWorld
USB MSD HID composite device hello world
USBMSD_HID.cpp@0:61e5ecd27a36, 2013-01-21 (annotated)
- Committer:
- samux
- Date:
- Mon Jan 21 12:03:05 2013 +0000
- Revision:
- 0:61e5ecd27a36
USB MSD HID composite device hello world
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:61e5ecd27a36 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
samux | 0:61e5ecd27a36 | 2 | * |
samux | 0:61e5ecd27a36 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 0:61e5ecd27a36 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
samux | 0:61e5ecd27a36 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
samux | 0:61e5ecd27a36 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
samux | 0:61e5ecd27a36 | 7 | * Software is furnished to do so, subject to the following conditions: |
samux | 0:61e5ecd27a36 | 8 | * |
samux | 0:61e5ecd27a36 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 0:61e5ecd27a36 | 10 | * substantial portions of the Software. |
samux | 0:61e5ecd27a36 | 11 | * |
samux | 0:61e5ecd27a36 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 0:61e5ecd27a36 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 0:61e5ecd27a36 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 0:61e5ecd27a36 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 0:61e5ecd27a36 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 0:61e5ecd27a36 | 17 | */ |
samux | 0:61e5ecd27a36 | 18 | |
samux | 0:61e5ecd27a36 | 19 | #include "stdint.h" |
samux | 0:61e5ecd27a36 | 20 | #include "USBMSD_HID.h" |
samux | 0:61e5ecd27a36 | 21 | |
samux | 0:61e5ecd27a36 | 22 | #define DISK_OK 0x00 |
samux | 0:61e5ecd27a36 | 23 | #define NO_INIT 0x01 |
samux | 0:61e5ecd27a36 | 24 | #define NO_DISK 0x02 |
samux | 0:61e5ecd27a36 | 25 | #define WRITE_PROTECT 0x04 |
samux | 0:61e5ecd27a36 | 26 | |
samux | 0:61e5ecd27a36 | 27 | #define CBW_Signature 0x43425355 |
samux | 0:61e5ecd27a36 | 28 | #define CSW_Signature 0x53425355 |
samux | 0:61e5ecd27a36 | 29 | |
samux | 0:61e5ecd27a36 | 30 | // SCSI Commands |
samux | 0:61e5ecd27a36 | 31 | #define TEST_UNIT_READY 0x00 |
samux | 0:61e5ecd27a36 | 32 | #define REQUEST_SENSE 0x03 |
samux | 0:61e5ecd27a36 | 33 | #define FORMAT_UNIT 0x04 |
samux | 0:61e5ecd27a36 | 34 | #define INQUIRY 0x12 |
samux | 0:61e5ecd27a36 | 35 | #define MODE_SELECT6 0x15 |
samux | 0:61e5ecd27a36 | 36 | #define MODE_SENSE6 0x1A |
samux | 0:61e5ecd27a36 | 37 | #define START_STOP_UNIT 0x1B |
samux | 0:61e5ecd27a36 | 38 | #define MEDIA_REMOVAL 0x1E |
samux | 0:61e5ecd27a36 | 39 | #define READ_FORMAT_CAPACITIES 0x23 |
samux | 0:61e5ecd27a36 | 40 | #define READ_CAPACITY 0x25 |
samux | 0:61e5ecd27a36 | 41 | #define READ10 0x28 |
samux | 0:61e5ecd27a36 | 42 | #define WRITE10 0x2A |
samux | 0:61e5ecd27a36 | 43 | #define VERIFY10 0x2F |
samux | 0:61e5ecd27a36 | 44 | #define READ12 0xA8 |
samux | 0:61e5ecd27a36 | 45 | #define WRITE12 0xAA |
samux | 0:61e5ecd27a36 | 46 | #define MODE_SELECT10 0x55 |
samux | 0:61e5ecd27a36 | 47 | #define MODE_SENSE10 0x5A |
samux | 0:61e5ecd27a36 | 48 | |
samux | 0:61e5ecd27a36 | 49 | // MSC class specific requests |
samux | 0:61e5ecd27a36 | 50 | #define MSC_REQUEST_RESET 0xFF |
samux | 0:61e5ecd27a36 | 51 | #define MSC_REQUEST_GET_MAX_LUN 0xFE |
samux | 0:61e5ecd27a36 | 52 | |
samux | 0:61e5ecd27a36 | 53 | #define DEFAULT_CONFIGURATION (1) |
samux | 0:61e5ecd27a36 | 54 | |
samux | 0:61e5ecd27a36 | 55 | // max packet size |
samux | 0:61e5ecd27a36 | 56 | #define MAX_PACKET MAX_PACKET_SIZE_EPBULK |
samux | 0:61e5ecd27a36 | 57 | |
samux | 0:61e5ecd27a36 | 58 | // CSW Status |
samux | 0:61e5ecd27a36 | 59 | enum Status { |
samux | 0:61e5ecd27a36 | 60 | CSW_PASSED, |
samux | 0:61e5ecd27a36 | 61 | CSW_FAILED, |
samux | 0:61e5ecd27a36 | 62 | CSW_ERROR, |
samux | 0:61e5ecd27a36 | 63 | }; |
samux | 0:61e5ecd27a36 | 64 | |
samux | 0:61e5ecd27a36 | 65 | |
samux | 0:61e5ecd27a36 | 66 | USBMSD_HID::USBMSD_HID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) |
samux | 0:61e5ecd27a36 | 67 | { |
samux | 0:61e5ecd27a36 | 68 | stage = READ_CBW; |
samux | 0:61e5ecd27a36 | 69 | memset((void *)&cbw, 0, sizeof(CBW)); |
samux | 0:61e5ecd27a36 | 70 | memset((void *)&csw, 0, sizeof(CSW)); |
samux | 0:61e5ecd27a36 | 71 | |
samux | 0:61e5ecd27a36 | 72 | output_length = output_report_length; |
samux | 0:61e5ecd27a36 | 73 | input_length = input_report_length; |
samux | 0:61e5ecd27a36 | 74 | } |
samux | 0:61e5ecd27a36 | 75 | |
samux | 0:61e5ecd27a36 | 76 | |
samux | 0:61e5ecd27a36 | 77 | bool USBMSD_HID::send(HID_REPORT *report) |
samux | 0:61e5ecd27a36 | 78 | { |
samux | 0:61e5ecd27a36 | 79 | return USBDevice::write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE); |
samux | 0:61e5ecd27a36 | 80 | } |
samux | 0:61e5ecd27a36 | 81 | |
samux | 0:61e5ecd27a36 | 82 | bool USBMSD_HID::sendNB(HID_REPORT *report) |
samux | 0:61e5ecd27a36 | 83 | { |
samux | 0:61e5ecd27a36 | 84 | return USBDevice::writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE); |
samux | 0:61e5ecd27a36 | 85 | } |
samux | 0:61e5ecd27a36 | 86 | |
samux | 0:61e5ecd27a36 | 87 | |
samux | 0:61e5ecd27a36 | 88 | bool USBMSD_HID::read(HID_REPORT *report) |
samux | 0:61e5ecd27a36 | 89 | { |
samux | 0:61e5ecd27a36 | 90 | uint32_t bytesRead = 0; |
samux | 0:61e5ecd27a36 | 91 | bool result; |
samux | 0:61e5ecd27a36 | 92 | result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE); |
samux | 0:61e5ecd27a36 | 93 | if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE)) |
samux | 0:61e5ecd27a36 | 94 | return false; |
samux | 0:61e5ecd27a36 | 95 | report->length = bytesRead; |
samux | 0:61e5ecd27a36 | 96 | return result; |
samux | 0:61e5ecd27a36 | 97 | } |
samux | 0:61e5ecd27a36 | 98 | |
samux | 0:61e5ecd27a36 | 99 | |
samux | 0:61e5ecd27a36 | 100 | bool USBMSD_HID::readNB(HID_REPORT *report) |
samux | 0:61e5ecd27a36 | 101 | { |
samux | 0:61e5ecd27a36 | 102 | uint32_t bytesRead = 0; |
samux | 0:61e5ecd27a36 | 103 | bool result; |
samux | 0:61e5ecd27a36 | 104 | result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE); |
samux | 0:61e5ecd27a36 | 105 | report->length = bytesRead; |
samux | 0:61e5ecd27a36 | 106 | if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE)) |
samux | 0:61e5ecd27a36 | 107 | return false; |
samux | 0:61e5ecd27a36 | 108 | return result; |
samux | 0:61e5ecd27a36 | 109 | } |
samux | 0:61e5ecd27a36 | 110 | |
samux | 0:61e5ecd27a36 | 111 | |
samux | 0:61e5ecd27a36 | 112 | uint16_t USBMSD_HID::reportDescLength() |
samux | 0:61e5ecd27a36 | 113 | { |
samux | 0:61e5ecd27a36 | 114 | reportDesc(); |
samux | 0:61e5ecd27a36 | 115 | return reportLength; |
samux | 0:61e5ecd27a36 | 116 | } |
samux | 0:61e5ecd27a36 | 117 | |
samux | 0:61e5ecd27a36 | 118 | |
samux | 0:61e5ecd27a36 | 119 | // Called in ISR context to process a class specific request |
samux | 0:61e5ecd27a36 | 120 | bool USBMSD_HID::USBCallback_request(void) |
samux | 0:61e5ecd27a36 | 121 | { |
samux | 0:61e5ecd27a36 | 122 | |
samux | 0:61e5ecd27a36 | 123 | bool success = false; |
samux | 0:61e5ecd27a36 | 124 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
samux | 0:61e5ecd27a36 | 125 | static uint8_t maxLUN[1] = {0}; |
samux | 0:61e5ecd27a36 | 126 | uint8_t *hidDescriptor; |
samux | 0:61e5ecd27a36 | 127 | |
samux | 0:61e5ecd27a36 | 128 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { |
samux | 0:61e5ecd27a36 | 129 | switch (transfer->setup.bRequest) { |
samux | 0:61e5ecd27a36 | 130 | case MSC_REQUEST_RESET: |
samux | 0:61e5ecd27a36 | 131 | reset(); |
samux | 0:61e5ecd27a36 | 132 | success = true; |
samux | 0:61e5ecd27a36 | 133 | break; |
samux | 0:61e5ecd27a36 | 134 | case MSC_REQUEST_GET_MAX_LUN: |
samux | 0:61e5ecd27a36 | 135 | transfer->remaining = 1; |
samux | 0:61e5ecd27a36 | 136 | transfer->ptr = maxLUN; |
samux | 0:61e5ecd27a36 | 137 | transfer->direction = DEVICE_TO_HOST; |
samux | 0:61e5ecd27a36 | 138 | success = true; |
samux | 0:61e5ecd27a36 | 139 | break; |
samux | 0:61e5ecd27a36 | 140 | |
samux | 0:61e5ecd27a36 | 141 | case SET_REPORT: |
samux | 0:61e5ecd27a36 | 142 | // First byte will be used for report ID |
samux | 0:61e5ecd27a36 | 143 | outputReport.data[0] = transfer->setup.wValue & 0xff; |
samux | 0:61e5ecd27a36 | 144 | outputReport.length = transfer->setup.wLength + 1; |
samux | 0:61e5ecd27a36 | 145 | |
samux | 0:61e5ecd27a36 | 146 | transfer->remaining = sizeof(outputReport.data) - 1; |
samux | 0:61e5ecd27a36 | 147 | transfer->ptr = &outputReport.data[1]; |
samux | 0:61e5ecd27a36 | 148 | transfer->direction = HOST_TO_DEVICE; |
samux | 0:61e5ecd27a36 | 149 | transfer->notify = true; |
samux | 0:61e5ecd27a36 | 150 | success = true; |
samux | 0:61e5ecd27a36 | 151 | |
samux | 0:61e5ecd27a36 | 152 | break; |
samux | 0:61e5ecd27a36 | 153 | default: |
samux | 0:61e5ecd27a36 | 154 | break; |
samux | 0:61e5ecd27a36 | 155 | } |
samux | 0:61e5ecd27a36 | 156 | } |
samux | 0:61e5ecd27a36 | 157 | |
samux | 0:61e5ecd27a36 | 158 | if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE)) |
samux | 0:61e5ecd27a36 | 159 | { |
samux | 0:61e5ecd27a36 | 160 | switch (transfer->setup.bRequest) |
samux | 0:61e5ecd27a36 | 161 | { |
samux | 0:61e5ecd27a36 | 162 | case GET_DESCRIPTOR: |
samux | 0:61e5ecd27a36 | 163 | switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) |
samux | 0:61e5ecd27a36 | 164 | { |
samux | 0:61e5ecd27a36 | 165 | case REPORT_DESCRIPTOR: |
samux | 0:61e5ecd27a36 | 166 | if ((reportDesc() != NULL) \ |
samux | 0:61e5ecd27a36 | 167 | && (reportDescLength() != 0)) |
samux | 0:61e5ecd27a36 | 168 | { |
samux | 0:61e5ecd27a36 | 169 | transfer->remaining = reportDescLength(); |
samux | 0:61e5ecd27a36 | 170 | transfer->ptr = reportDesc(); |
samux | 0:61e5ecd27a36 | 171 | transfer->direction = DEVICE_TO_HOST; |
samux | 0:61e5ecd27a36 | 172 | success = true; |
samux | 0:61e5ecd27a36 | 173 | } |
samux | 0:61e5ecd27a36 | 174 | break; |
samux | 0:61e5ecd27a36 | 175 | case HID_DESCRIPTOR: |
samux | 0:61e5ecd27a36 | 176 | // Find the HID descriptor, after the configuration descriptor |
samux | 0:61e5ecd27a36 | 177 | hidDescriptor = findDescriptor(HID_DESCRIPTOR); |
samux | 0:61e5ecd27a36 | 178 | if (hidDescriptor != NULL) |
samux | 0:61e5ecd27a36 | 179 | { |
samux | 0:61e5ecd27a36 | 180 | transfer->remaining = HID_DESCRIPTOR_LENGTH; |
samux | 0:61e5ecd27a36 | 181 | transfer->ptr = hidDescriptor; |
samux | 0:61e5ecd27a36 | 182 | transfer->direction = DEVICE_TO_HOST; |
samux | 0:61e5ecd27a36 | 183 | success = true; |
samux | 0:61e5ecd27a36 | 184 | } |
samux | 0:61e5ecd27a36 | 185 | break; |
samux | 0:61e5ecd27a36 | 186 | |
samux | 0:61e5ecd27a36 | 187 | default: |
samux | 0:61e5ecd27a36 | 188 | break; |
samux | 0:61e5ecd27a36 | 189 | } |
samux | 0:61e5ecd27a36 | 190 | break; |
samux | 0:61e5ecd27a36 | 191 | default: |
samux | 0:61e5ecd27a36 | 192 | break; |
samux | 0:61e5ecd27a36 | 193 | } |
samux | 0:61e5ecd27a36 | 194 | } |
samux | 0:61e5ecd27a36 | 195 | |
samux | 0:61e5ecd27a36 | 196 | return success; |
samux | 0:61e5ecd27a36 | 197 | } |
samux | 0:61e5ecd27a36 | 198 | |
samux | 0:61e5ecd27a36 | 199 | |
samux | 0:61e5ecd27a36 | 200 | bool USBMSD_HID::connect() |
samux | 0:61e5ecd27a36 | 201 | { |
samux | 0:61e5ecd27a36 | 202 | |
samux | 0:61e5ecd27a36 | 203 | //disk initialization |
samux | 0:61e5ecd27a36 | 204 | if (disk_status() & NO_INIT) { |
samux | 0:61e5ecd27a36 | 205 | if (disk_initialize()) { |
samux | 0:61e5ecd27a36 | 206 | return false; |
samux | 0:61e5ecd27a36 | 207 | } |
samux | 0:61e5ecd27a36 | 208 | } |
samux | 0:61e5ecd27a36 | 209 | |
samux | 0:61e5ecd27a36 | 210 | // get number of blocks |
samux | 0:61e5ecd27a36 | 211 | BlockCount = disk_sectors(); |
samux | 0:61e5ecd27a36 | 212 | |
samux | 0:61e5ecd27a36 | 213 | // get memory size |
samux | 0:61e5ecd27a36 | 214 | MemorySize = disk_size(); |
samux | 0:61e5ecd27a36 | 215 | |
samux | 0:61e5ecd27a36 | 216 | if (BlockCount > 0) { |
samux | 0:61e5ecd27a36 | 217 | BlockSize = MemorySize / BlockCount; |
samux | 0:61e5ecd27a36 | 218 | if (BlockSize != 0) { |
samux | 0:61e5ecd27a36 | 219 | page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t)); |
samux | 0:61e5ecd27a36 | 220 | if (page == NULL) |
samux | 0:61e5ecd27a36 | 221 | return false; |
samux | 0:61e5ecd27a36 | 222 | } |
samux | 0:61e5ecd27a36 | 223 | } else { |
samux | 0:61e5ecd27a36 | 224 | return false; |
samux | 0:61e5ecd27a36 | 225 | } |
samux | 0:61e5ecd27a36 | 226 | |
samux | 0:61e5ecd27a36 | 227 | //connect the device |
samux | 0:61e5ecd27a36 | 228 | USBDevice::connect(); |
samux | 0:61e5ecd27a36 | 229 | return true; |
samux | 0:61e5ecd27a36 | 230 | } |
samux | 0:61e5ecd27a36 | 231 | |
samux | 0:61e5ecd27a36 | 232 | |
samux | 0:61e5ecd27a36 | 233 | void USBMSD_HID::reset() |
samux | 0:61e5ecd27a36 | 234 | { |
samux | 0:61e5ecd27a36 | 235 | stage = READ_CBW; |
samux | 0:61e5ecd27a36 | 236 | } |
samux | 0:61e5ecd27a36 | 237 | |
samux | 0:61e5ecd27a36 | 238 | |
samux | 0:61e5ecd27a36 | 239 | // Called in ISR context called when a data is received |
samux | 0:61e5ecd27a36 | 240 | bool USBMSD_HID::EP2_OUT_callback() |
samux | 0:61e5ecd27a36 | 241 | { |
samux | 0:61e5ecd27a36 | 242 | uint32_t size = 0; |
samux | 0:61e5ecd27a36 | 243 | uint8_t buf[MAX_PACKET_SIZE_EPBULK]; |
samux | 0:61e5ecd27a36 | 244 | readEP(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK); |
samux | 0:61e5ecd27a36 | 245 | switch (stage) { |
samux | 0:61e5ecd27a36 | 246 | // the device has to decode the CBW received |
samux | 0:61e5ecd27a36 | 247 | case READ_CBW: |
samux | 0:61e5ecd27a36 | 248 | CBWDecode(buf, size); |
samux | 0:61e5ecd27a36 | 249 | break; |
samux | 0:61e5ecd27a36 | 250 | |
samux | 0:61e5ecd27a36 | 251 | // the device has to receive data from the host |
samux | 0:61e5ecd27a36 | 252 | case PROCESS_CBW: |
samux | 0:61e5ecd27a36 | 253 | switch (cbw.CB[0]) { |
samux | 0:61e5ecd27a36 | 254 | case WRITE10: |
samux | 0:61e5ecd27a36 | 255 | case WRITE12: |
samux | 0:61e5ecd27a36 | 256 | memoryWrite(buf, size); |
samux | 0:61e5ecd27a36 | 257 | break; |
samux | 0:61e5ecd27a36 | 258 | case VERIFY10: |
samux | 0:61e5ecd27a36 | 259 | memoryVerify(buf, size); |
samux | 0:61e5ecd27a36 | 260 | break; |
samux | 0:61e5ecd27a36 | 261 | } |
samux | 0:61e5ecd27a36 | 262 | break; |
samux | 0:61e5ecd27a36 | 263 | |
samux | 0:61e5ecd27a36 | 264 | // an error has occured: stall endpoint and send CSW |
samux | 0:61e5ecd27a36 | 265 | default: |
samux | 0:61e5ecd27a36 | 266 | stallEndpoint(EPBULK_OUT); |
samux | 0:61e5ecd27a36 | 267 | csw.Status = CSW_ERROR; |
samux | 0:61e5ecd27a36 | 268 | sendCSW(); |
samux | 0:61e5ecd27a36 | 269 | break; |
samux | 0:61e5ecd27a36 | 270 | } |
samux | 0:61e5ecd27a36 | 271 | |
samux | 0:61e5ecd27a36 | 272 | //reactivate readings on the OUT bulk endpoint |
samux | 0:61e5ecd27a36 | 273 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
samux | 0:61e5ecd27a36 | 274 | return true; |
samux | 0:61e5ecd27a36 | 275 | } |
samux | 0:61e5ecd27a36 | 276 | |
samux | 0:61e5ecd27a36 | 277 | // Called in ISR context when a data has been transferred |
samux | 0:61e5ecd27a36 | 278 | bool USBMSD_HID::EP2_IN_callback() |
samux | 0:61e5ecd27a36 | 279 | { |
samux | 0:61e5ecd27a36 | 280 | switch (stage) { |
samux | 0:61e5ecd27a36 | 281 | |
samux | 0:61e5ecd27a36 | 282 | // the device has to send data to the host |
samux | 0:61e5ecd27a36 | 283 | case PROCESS_CBW: |
samux | 0:61e5ecd27a36 | 284 | switch (cbw.CB[0]) { |
samux | 0:61e5ecd27a36 | 285 | case READ10: |
samux | 0:61e5ecd27a36 | 286 | case READ12: |
samux | 0:61e5ecd27a36 | 287 | memoryRead(); |
samux | 0:61e5ecd27a36 | 288 | break; |
samux | 0:61e5ecd27a36 | 289 | } |
samux | 0:61e5ecd27a36 | 290 | break; |
samux | 0:61e5ecd27a36 | 291 | |
samux | 0:61e5ecd27a36 | 292 | //the device has to send a CSW |
samux | 0:61e5ecd27a36 | 293 | case SEND_CSW: |
samux | 0:61e5ecd27a36 | 294 | sendCSW(); |
samux | 0:61e5ecd27a36 | 295 | break; |
samux | 0:61e5ecd27a36 | 296 | |
samux | 0:61e5ecd27a36 | 297 | // an error has occured |
samux | 0:61e5ecd27a36 | 298 | case ERROR: |
samux | 0:61e5ecd27a36 | 299 | stallEndpoint(EPBULK_IN); |
samux | 0:61e5ecd27a36 | 300 | sendCSW(); |
samux | 0:61e5ecd27a36 | 301 | break; |
samux | 0:61e5ecd27a36 | 302 | |
samux | 0:61e5ecd27a36 | 303 | // the host has received the CSW -> we wait a CBW |
samux | 0:61e5ecd27a36 | 304 | case WAIT_CSW: |
samux | 0:61e5ecd27a36 | 305 | stage = READ_CBW; |
samux | 0:61e5ecd27a36 | 306 | break; |
samux | 0:61e5ecd27a36 | 307 | } |
samux | 0:61e5ecd27a36 | 308 | return true; |
samux | 0:61e5ecd27a36 | 309 | } |
samux | 0:61e5ecd27a36 | 310 | |
samux | 0:61e5ecd27a36 | 311 | |
samux | 0:61e5ecd27a36 | 312 | void USBMSD_HID::memoryWrite (uint8_t * buf, uint16_t size) |
samux | 0:61e5ecd27a36 | 313 | { |
samux | 0:61e5ecd27a36 | 314 | |
samux | 0:61e5ecd27a36 | 315 | if ((addr + size) > MemorySize) { |
samux | 0:61e5ecd27a36 | 316 | size = MemorySize - addr; |
samux | 0:61e5ecd27a36 | 317 | stage = ERROR; |
samux | 0:61e5ecd27a36 | 318 | stallEndpoint(EPBULK_OUT); |
samux | 0:61e5ecd27a36 | 319 | } |
samux | 0:61e5ecd27a36 | 320 | |
samux | 0:61e5ecd27a36 | 321 | // we fill an array in RAM of 1 block before writing it in memory |
samux | 0:61e5ecd27a36 | 322 | for (int i = 0; i < size; i++) |
samux | 0:61e5ecd27a36 | 323 | page[addr%BlockSize + i] = buf[i]; |
samux | 0:61e5ecd27a36 | 324 | |
samux | 0:61e5ecd27a36 | 325 | // if the array is filled, write it in memory |
samux | 0:61e5ecd27a36 | 326 | if (!((addr + size)%BlockSize)) { |
samux | 0:61e5ecd27a36 | 327 | if (!(disk_status() & WRITE_PROTECT)) { |
samux | 0:61e5ecd27a36 | 328 | disk_write(page, addr/BlockSize); |
samux | 0:61e5ecd27a36 | 329 | } |
samux | 0:61e5ecd27a36 | 330 | } |
samux | 0:61e5ecd27a36 | 331 | |
samux | 0:61e5ecd27a36 | 332 | addr += size; |
samux | 0:61e5ecd27a36 | 333 | length -= size; |
samux | 0:61e5ecd27a36 | 334 | csw.DataResidue -= size; |
samux | 0:61e5ecd27a36 | 335 | |
samux | 0:61e5ecd27a36 | 336 | if ((!length) || (stage != PROCESS_CBW)) { |
samux | 0:61e5ecd27a36 | 337 | csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED; |
samux | 0:61e5ecd27a36 | 338 | sendCSW(); |
samux | 0:61e5ecd27a36 | 339 | } |
samux | 0:61e5ecd27a36 | 340 | } |
samux | 0:61e5ecd27a36 | 341 | |
samux | 0:61e5ecd27a36 | 342 | void USBMSD_HID::memoryVerify (uint8_t * buf, uint16_t size) |
samux | 0:61e5ecd27a36 | 343 | { |
samux | 0:61e5ecd27a36 | 344 | uint32_t n; |
samux | 0:61e5ecd27a36 | 345 | |
samux | 0:61e5ecd27a36 | 346 | if ((addr + size) > MemorySize) { |
samux | 0:61e5ecd27a36 | 347 | size = MemorySize - addr; |
samux | 0:61e5ecd27a36 | 348 | stage = ERROR; |
samux | 0:61e5ecd27a36 | 349 | stallEndpoint(EPBULK_OUT); |
samux | 0:61e5ecd27a36 | 350 | } |
samux | 0:61e5ecd27a36 | 351 | |
samux | 0:61e5ecd27a36 | 352 | // beginning of a new block -> load a whole block in RAM |
samux | 0:61e5ecd27a36 | 353 | if (!(addr%BlockSize)) |
samux | 0:61e5ecd27a36 | 354 | disk_read(page, addr/BlockSize); |
samux | 0:61e5ecd27a36 | 355 | |
samux | 0:61e5ecd27a36 | 356 | // info are in RAM -> no need to re-read memory |
samux | 0:61e5ecd27a36 | 357 | for (n = 0; n < size; n++) { |
samux | 0:61e5ecd27a36 | 358 | if (page[addr%BlockSize + n] != buf[n]) { |
samux | 0:61e5ecd27a36 | 359 | memOK = false; |
samux | 0:61e5ecd27a36 | 360 | break; |
samux | 0:61e5ecd27a36 | 361 | } |
samux | 0:61e5ecd27a36 | 362 | } |
samux | 0:61e5ecd27a36 | 363 | |
samux | 0:61e5ecd27a36 | 364 | addr += size; |
samux | 0:61e5ecd27a36 | 365 | length -= size; |
samux | 0:61e5ecd27a36 | 366 | csw.DataResidue -= size; |
samux | 0:61e5ecd27a36 | 367 | |
samux | 0:61e5ecd27a36 | 368 | if ( !length || (stage != PROCESS_CBW)) { |
samux | 0:61e5ecd27a36 | 369 | csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED; |
samux | 0:61e5ecd27a36 | 370 | sendCSW(); |
samux | 0:61e5ecd27a36 | 371 | } |
samux | 0:61e5ecd27a36 | 372 | } |
samux | 0:61e5ecd27a36 | 373 | |
samux | 0:61e5ecd27a36 | 374 | |
samux | 0:61e5ecd27a36 | 375 | bool USBMSD_HID::inquiryRequest (void) |
samux | 0:61e5ecd27a36 | 376 | { |
samux | 0:61e5ecd27a36 | 377 | uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01, |
samux | 0:61e5ecd27a36 | 378 | 36 - 4, 0x80, 0x00, 0x00, |
samux | 0:61e5ecd27a36 | 379 | 'M', 'B', 'E', 'D', '.', 'O', 'R', 'G', |
samux | 0:61e5ecd27a36 | 380 | 'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ', |
samux | 0:61e5ecd27a36 | 381 | '1', '.', '0', ' ', |
samux | 0:61e5ecd27a36 | 382 | }; |
samux | 0:61e5ecd27a36 | 383 | if (!write(inquiry, sizeof(inquiry))) { |
samux | 0:61e5ecd27a36 | 384 | return false; |
samux | 0:61e5ecd27a36 | 385 | } |
samux | 0:61e5ecd27a36 | 386 | return true; |
samux | 0:61e5ecd27a36 | 387 | } |
samux | 0:61e5ecd27a36 | 388 | |
samux | 0:61e5ecd27a36 | 389 | |
samux | 0:61e5ecd27a36 | 390 | bool USBMSD_HID::readFormatCapacity() |
samux | 0:61e5ecd27a36 | 391 | { |
samux | 0:61e5ecd27a36 | 392 | uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08, |
samux | 0:61e5ecd27a36 | 393 | (BlockCount >> 24) & 0xff, |
samux | 0:61e5ecd27a36 | 394 | (BlockCount >> 16) & 0xff, |
samux | 0:61e5ecd27a36 | 395 | (BlockCount >> 8) & 0xff, |
samux | 0:61e5ecd27a36 | 396 | (BlockCount >> 0) & 0xff, |
samux | 0:61e5ecd27a36 | 397 | |
samux | 0:61e5ecd27a36 | 398 | 0x02, |
samux | 0:61e5ecd27a36 | 399 | (BlockSize >> 16) & 0xff, |
samux | 0:61e5ecd27a36 | 400 | (BlockSize >> 8) & 0xff, |
samux | 0:61e5ecd27a36 | 401 | (BlockSize >> 0) & 0xff, |
samux | 0:61e5ecd27a36 | 402 | }; |
samux | 0:61e5ecd27a36 | 403 | if (!write(capacity, sizeof(capacity))) { |
samux | 0:61e5ecd27a36 | 404 | return false; |
samux | 0:61e5ecd27a36 | 405 | } |
samux | 0:61e5ecd27a36 | 406 | return true; |
samux | 0:61e5ecd27a36 | 407 | } |
samux | 0:61e5ecd27a36 | 408 | |
samux | 0:61e5ecd27a36 | 409 | |
samux | 0:61e5ecd27a36 | 410 | bool USBMSD_HID::readCapacity (void) |
samux | 0:61e5ecd27a36 | 411 | { |
samux | 0:61e5ecd27a36 | 412 | uint8_t capacity[] = { |
samux | 0:61e5ecd27a36 | 413 | ((BlockCount - 1) >> 24) & 0xff, |
samux | 0:61e5ecd27a36 | 414 | ((BlockCount - 1) >> 16) & 0xff, |
samux | 0:61e5ecd27a36 | 415 | ((BlockCount - 1) >> 8) & 0xff, |
samux | 0:61e5ecd27a36 | 416 | ((BlockCount - 1) >> 0) & 0xff, |
samux | 0:61e5ecd27a36 | 417 | |
samux | 0:61e5ecd27a36 | 418 | (BlockSize >> 24) & 0xff, |
samux | 0:61e5ecd27a36 | 419 | (BlockSize >> 16) & 0xff, |
samux | 0:61e5ecd27a36 | 420 | (BlockSize >> 8) & 0xff, |
samux | 0:61e5ecd27a36 | 421 | (BlockSize >> 0) & 0xff, |
samux | 0:61e5ecd27a36 | 422 | }; |
samux | 0:61e5ecd27a36 | 423 | if (!write(capacity, sizeof(capacity))) { |
samux | 0:61e5ecd27a36 | 424 | return false; |
samux | 0:61e5ecd27a36 | 425 | } |
samux | 0:61e5ecd27a36 | 426 | return true; |
samux | 0:61e5ecd27a36 | 427 | } |
samux | 0:61e5ecd27a36 | 428 | |
samux | 0:61e5ecd27a36 | 429 | bool USBMSD_HID::write (uint8_t * buf, uint16_t size) |
samux | 0:61e5ecd27a36 | 430 | { |
samux | 0:61e5ecd27a36 | 431 | |
samux | 0:61e5ecd27a36 | 432 | if (size >= cbw.DataLength) { |
samux | 0:61e5ecd27a36 | 433 | size = cbw.DataLength; |
samux | 0:61e5ecd27a36 | 434 | } |
samux | 0:61e5ecd27a36 | 435 | stage = SEND_CSW; |
samux | 0:61e5ecd27a36 | 436 | |
samux | 0:61e5ecd27a36 | 437 | if (!writeNB(EPBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) { |
samux | 0:61e5ecd27a36 | 438 | return false; |
samux | 0:61e5ecd27a36 | 439 | } |
samux | 0:61e5ecd27a36 | 440 | |
samux | 0:61e5ecd27a36 | 441 | csw.DataResidue -= size; |
samux | 0:61e5ecd27a36 | 442 | csw.Status = CSW_PASSED; |
samux | 0:61e5ecd27a36 | 443 | return true; |
samux | 0:61e5ecd27a36 | 444 | } |
samux | 0:61e5ecd27a36 | 445 | |
samux | 0:61e5ecd27a36 | 446 | |
samux | 0:61e5ecd27a36 | 447 | bool USBMSD_HID::modeSense6 (void) |
samux | 0:61e5ecd27a36 | 448 | { |
samux | 0:61e5ecd27a36 | 449 | uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 }; |
samux | 0:61e5ecd27a36 | 450 | if (!write(sense6, sizeof(sense6))) { |
samux | 0:61e5ecd27a36 | 451 | return false; |
samux | 0:61e5ecd27a36 | 452 | } |
samux | 0:61e5ecd27a36 | 453 | return true; |
samux | 0:61e5ecd27a36 | 454 | } |
samux | 0:61e5ecd27a36 | 455 | |
samux | 0:61e5ecd27a36 | 456 | void USBMSD_HID::sendCSW() |
samux | 0:61e5ecd27a36 | 457 | { |
samux | 0:61e5ecd27a36 | 458 | csw.Signature = CSW_Signature; |
samux | 0:61e5ecd27a36 | 459 | writeNB(EPBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK); |
samux | 0:61e5ecd27a36 | 460 | stage = WAIT_CSW; |
samux | 0:61e5ecd27a36 | 461 | } |
samux | 0:61e5ecd27a36 | 462 | |
samux | 0:61e5ecd27a36 | 463 | bool USBMSD_HID::requestSense (void) |
samux | 0:61e5ecd27a36 | 464 | { |
samux | 0:61e5ecd27a36 | 465 | uint8_t request_sense[] = { |
samux | 0:61e5ecd27a36 | 466 | 0x70, |
samux | 0:61e5ecd27a36 | 467 | 0x00, |
samux | 0:61e5ecd27a36 | 468 | 0x05, // Sense Key: illegal request |
samux | 0:61e5ecd27a36 | 469 | 0x00, |
samux | 0:61e5ecd27a36 | 470 | 0x00, |
samux | 0:61e5ecd27a36 | 471 | 0x00, |
samux | 0:61e5ecd27a36 | 472 | 0x00, |
samux | 0:61e5ecd27a36 | 473 | 0x0A, |
samux | 0:61e5ecd27a36 | 474 | 0x00, |
samux | 0:61e5ecd27a36 | 475 | 0x00, |
samux | 0:61e5ecd27a36 | 476 | 0x00, |
samux | 0:61e5ecd27a36 | 477 | 0x00, |
samux | 0:61e5ecd27a36 | 478 | 0x30, |
samux | 0:61e5ecd27a36 | 479 | 0x01, |
samux | 0:61e5ecd27a36 | 480 | 0x00, |
samux | 0:61e5ecd27a36 | 481 | 0x00, |
samux | 0:61e5ecd27a36 | 482 | 0x00, |
samux | 0:61e5ecd27a36 | 483 | 0x00, |
samux | 0:61e5ecd27a36 | 484 | }; |
samux | 0:61e5ecd27a36 | 485 | |
samux | 0:61e5ecd27a36 | 486 | if (!write(request_sense, sizeof(request_sense))) { |
samux | 0:61e5ecd27a36 | 487 | return false; |
samux | 0:61e5ecd27a36 | 488 | } |
samux | 0:61e5ecd27a36 | 489 | |
samux | 0:61e5ecd27a36 | 490 | return true; |
samux | 0:61e5ecd27a36 | 491 | } |
samux | 0:61e5ecd27a36 | 492 | |
samux | 0:61e5ecd27a36 | 493 | void USBMSD_HID::fail() |
samux | 0:61e5ecd27a36 | 494 | { |
samux | 0:61e5ecd27a36 | 495 | csw.Status = CSW_FAILED; |
samux | 0:61e5ecd27a36 | 496 | sendCSW(); |
samux | 0:61e5ecd27a36 | 497 | } |
samux | 0:61e5ecd27a36 | 498 | |
samux | 0:61e5ecd27a36 | 499 | |
samux | 0:61e5ecd27a36 | 500 | void USBMSD_HID::CBWDecode(uint8_t * buf, uint16_t size) |
samux | 0:61e5ecd27a36 | 501 | { |
samux | 0:61e5ecd27a36 | 502 | if (size == sizeof(cbw)) { |
samux | 0:61e5ecd27a36 | 503 | memcpy((uint8_t *)&cbw, buf, size); |
samux | 0:61e5ecd27a36 | 504 | if (cbw.Signature == CBW_Signature) { |
samux | 0:61e5ecd27a36 | 505 | csw.Tag = cbw.Tag; |
samux | 0:61e5ecd27a36 | 506 | csw.DataResidue = cbw.DataLength; |
samux | 0:61e5ecd27a36 | 507 | if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) { |
samux | 0:61e5ecd27a36 | 508 | fail(); |
samux | 0:61e5ecd27a36 | 509 | } else { |
samux | 0:61e5ecd27a36 | 510 | switch (cbw.CB[0]) { |
samux | 0:61e5ecd27a36 | 511 | case TEST_UNIT_READY: |
samux | 0:61e5ecd27a36 | 512 | testUnitReady(); |
samux | 0:61e5ecd27a36 | 513 | break; |
samux | 0:61e5ecd27a36 | 514 | case REQUEST_SENSE: |
samux | 0:61e5ecd27a36 | 515 | requestSense(); |
samux | 0:61e5ecd27a36 | 516 | break; |
samux | 0:61e5ecd27a36 | 517 | case INQUIRY: |
samux | 0:61e5ecd27a36 | 518 | inquiryRequest(); |
samux | 0:61e5ecd27a36 | 519 | break; |
samux | 0:61e5ecd27a36 | 520 | case MODE_SENSE6: |
samux | 0:61e5ecd27a36 | 521 | modeSense6(); |
samux | 0:61e5ecd27a36 | 522 | break; |
samux | 0:61e5ecd27a36 | 523 | case READ_FORMAT_CAPACITIES: |
samux | 0:61e5ecd27a36 | 524 | readFormatCapacity(); |
samux | 0:61e5ecd27a36 | 525 | break; |
samux | 0:61e5ecd27a36 | 526 | case READ_CAPACITY: |
samux | 0:61e5ecd27a36 | 527 | readCapacity(); |
samux | 0:61e5ecd27a36 | 528 | break; |
samux | 0:61e5ecd27a36 | 529 | case READ10: |
samux | 0:61e5ecd27a36 | 530 | case READ12: |
samux | 0:61e5ecd27a36 | 531 | if (infoTransfer()) { |
samux | 0:61e5ecd27a36 | 532 | if ((cbw.Flags & 0x80)) { |
samux | 0:61e5ecd27a36 | 533 | stage = PROCESS_CBW; |
samux | 0:61e5ecd27a36 | 534 | memoryRead(); |
samux | 0:61e5ecd27a36 | 535 | } else { |
samux | 0:61e5ecd27a36 | 536 | stallEndpoint(EPBULK_OUT); |
samux | 0:61e5ecd27a36 | 537 | csw.Status = CSW_ERROR; |
samux | 0:61e5ecd27a36 | 538 | sendCSW(); |
samux | 0:61e5ecd27a36 | 539 | } |
samux | 0:61e5ecd27a36 | 540 | } |
samux | 0:61e5ecd27a36 | 541 | break; |
samux | 0:61e5ecd27a36 | 542 | case WRITE10: |
samux | 0:61e5ecd27a36 | 543 | case WRITE12: |
samux | 0:61e5ecd27a36 | 544 | if (infoTransfer()) { |
samux | 0:61e5ecd27a36 | 545 | if (!(cbw.Flags & 0x80)) { |
samux | 0:61e5ecd27a36 | 546 | stage = PROCESS_CBW; |
samux | 0:61e5ecd27a36 | 547 | } else { |
samux | 0:61e5ecd27a36 | 548 | stallEndpoint(EPBULK_IN); |
samux | 0:61e5ecd27a36 | 549 | csw.Status = CSW_ERROR; |
samux | 0:61e5ecd27a36 | 550 | sendCSW(); |
samux | 0:61e5ecd27a36 | 551 | } |
samux | 0:61e5ecd27a36 | 552 | } |
samux | 0:61e5ecd27a36 | 553 | break; |
samux | 0:61e5ecd27a36 | 554 | case VERIFY10: |
samux | 0:61e5ecd27a36 | 555 | if (!(cbw.CB[1] & 0x02)) { |
samux | 0:61e5ecd27a36 | 556 | csw.Status = CSW_PASSED; |
samux | 0:61e5ecd27a36 | 557 | sendCSW(); |
samux | 0:61e5ecd27a36 | 558 | break; |
samux | 0:61e5ecd27a36 | 559 | } |
samux | 0:61e5ecd27a36 | 560 | if (infoTransfer()) { |
samux | 0:61e5ecd27a36 | 561 | if (!(cbw.Flags & 0x80)) { |
samux | 0:61e5ecd27a36 | 562 | stage = PROCESS_CBW; |
samux | 0:61e5ecd27a36 | 563 | memOK = true; |
samux | 0:61e5ecd27a36 | 564 | } else { |
samux | 0:61e5ecd27a36 | 565 | stallEndpoint(EPBULK_IN); |
samux | 0:61e5ecd27a36 | 566 | csw.Status = CSW_ERROR; |
samux | 0:61e5ecd27a36 | 567 | sendCSW(); |
samux | 0:61e5ecd27a36 | 568 | } |
samux | 0:61e5ecd27a36 | 569 | } |
samux | 0:61e5ecd27a36 | 570 | break; |
samux | 0:61e5ecd27a36 | 571 | case MEDIA_REMOVAL: |
samux | 0:61e5ecd27a36 | 572 | csw.Status = CSW_PASSED; |
samux | 0:61e5ecd27a36 | 573 | sendCSW(); |
samux | 0:61e5ecd27a36 | 574 | break; |
samux | 0:61e5ecd27a36 | 575 | default: |
samux | 0:61e5ecd27a36 | 576 | fail(); |
samux | 0:61e5ecd27a36 | 577 | break; |
samux | 0:61e5ecd27a36 | 578 | } |
samux | 0:61e5ecd27a36 | 579 | } |
samux | 0:61e5ecd27a36 | 580 | } |
samux | 0:61e5ecd27a36 | 581 | } |
samux | 0:61e5ecd27a36 | 582 | } |
samux | 0:61e5ecd27a36 | 583 | |
samux | 0:61e5ecd27a36 | 584 | void USBMSD_HID::testUnitReady (void) |
samux | 0:61e5ecd27a36 | 585 | { |
samux | 0:61e5ecd27a36 | 586 | |
samux | 0:61e5ecd27a36 | 587 | if (cbw.DataLength != 0) { |
samux | 0:61e5ecd27a36 | 588 | if ((cbw.Flags & 0x80) != 0) { |
samux | 0:61e5ecd27a36 | 589 | stallEndpoint(EPBULK_IN); |
samux | 0:61e5ecd27a36 | 590 | } else { |
samux | 0:61e5ecd27a36 | 591 | stallEndpoint(EPBULK_OUT); |
samux | 0:61e5ecd27a36 | 592 | } |
samux | 0:61e5ecd27a36 | 593 | } |
samux | 0:61e5ecd27a36 | 594 | |
samux | 0:61e5ecd27a36 | 595 | csw.Status = CSW_PASSED; |
samux | 0:61e5ecd27a36 | 596 | sendCSW(); |
samux | 0:61e5ecd27a36 | 597 | } |
samux | 0:61e5ecd27a36 | 598 | |
samux | 0:61e5ecd27a36 | 599 | |
samux | 0:61e5ecd27a36 | 600 | void USBMSD_HID::memoryRead (void) |
samux | 0:61e5ecd27a36 | 601 | { |
samux | 0:61e5ecd27a36 | 602 | uint32_t n; |
samux | 0:61e5ecd27a36 | 603 | |
samux | 0:61e5ecd27a36 | 604 | n = (length > MAX_PACKET) ? MAX_PACKET : length; |
samux | 0:61e5ecd27a36 | 605 | |
samux | 0:61e5ecd27a36 | 606 | if ((addr + n) > MemorySize) { |
samux | 0:61e5ecd27a36 | 607 | n = MemorySize - addr; |
samux | 0:61e5ecd27a36 | 608 | stage = ERROR; |
samux | 0:61e5ecd27a36 | 609 | } |
samux | 0:61e5ecd27a36 | 610 | |
samux | 0:61e5ecd27a36 | 611 | // we read an entire block |
samux | 0:61e5ecd27a36 | 612 | if (!(addr%BlockSize)) |
samux | 0:61e5ecd27a36 | 613 | disk_read(page, addr/BlockSize); |
samux | 0:61e5ecd27a36 | 614 | |
samux | 0:61e5ecd27a36 | 615 | // write data which are in RAM |
samux | 0:61e5ecd27a36 | 616 | writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK); |
samux | 0:61e5ecd27a36 | 617 | |
samux | 0:61e5ecd27a36 | 618 | addr += n; |
samux | 0:61e5ecd27a36 | 619 | length -= n; |
samux | 0:61e5ecd27a36 | 620 | |
samux | 0:61e5ecd27a36 | 621 | csw.DataResidue -= n; |
samux | 0:61e5ecd27a36 | 622 | |
samux | 0:61e5ecd27a36 | 623 | if ( !length || (stage != PROCESS_CBW)) { |
samux | 0:61e5ecd27a36 | 624 | csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED; |
samux | 0:61e5ecd27a36 | 625 | stage = (stage == PROCESS_CBW) ? SEND_CSW : stage; |
samux | 0:61e5ecd27a36 | 626 | } |
samux | 0:61e5ecd27a36 | 627 | } |
samux | 0:61e5ecd27a36 | 628 | |
samux | 0:61e5ecd27a36 | 629 | |
samux | 0:61e5ecd27a36 | 630 | bool USBMSD_HID::infoTransfer (void) |
samux | 0:61e5ecd27a36 | 631 | { |
samux | 0:61e5ecd27a36 | 632 | uint32_t n; |
samux | 0:61e5ecd27a36 | 633 | |
samux | 0:61e5ecd27a36 | 634 | // Logical Block Address of First Block |
samux | 0:61e5ecd27a36 | 635 | n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0); |
samux | 0:61e5ecd27a36 | 636 | |
samux | 0:61e5ecd27a36 | 637 | addr = n * BlockSize; |
samux | 0:61e5ecd27a36 | 638 | |
samux | 0:61e5ecd27a36 | 639 | // Number of Blocks to transfer |
samux | 0:61e5ecd27a36 | 640 | switch (cbw.CB[0]) { |
samux | 0:61e5ecd27a36 | 641 | case READ10: |
samux | 0:61e5ecd27a36 | 642 | case WRITE10: |
samux | 0:61e5ecd27a36 | 643 | case VERIFY10: |
samux | 0:61e5ecd27a36 | 644 | n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0); |
samux | 0:61e5ecd27a36 | 645 | break; |
samux | 0:61e5ecd27a36 | 646 | |
samux | 0:61e5ecd27a36 | 647 | case READ12: |
samux | 0:61e5ecd27a36 | 648 | case WRITE12: |
samux | 0:61e5ecd27a36 | 649 | n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0); |
samux | 0:61e5ecd27a36 | 650 | break; |
samux | 0:61e5ecd27a36 | 651 | } |
samux | 0:61e5ecd27a36 | 652 | |
samux | 0:61e5ecd27a36 | 653 | length = n * BlockSize; |
samux | 0:61e5ecd27a36 | 654 | |
samux | 0:61e5ecd27a36 | 655 | if (!cbw.DataLength) { // host requests no data |
samux | 0:61e5ecd27a36 | 656 | csw.Status = CSW_FAILED; |
samux | 0:61e5ecd27a36 | 657 | sendCSW(); |
samux | 0:61e5ecd27a36 | 658 | return false; |
samux | 0:61e5ecd27a36 | 659 | } |
samux | 0:61e5ecd27a36 | 660 | |
samux | 0:61e5ecd27a36 | 661 | if (cbw.DataLength != length) { |
samux | 0:61e5ecd27a36 | 662 | if ((cbw.Flags & 0x80) != 0) { |
samux | 0:61e5ecd27a36 | 663 | stallEndpoint(EPBULK_IN); |
samux | 0:61e5ecd27a36 | 664 | } else { |
samux | 0:61e5ecd27a36 | 665 | stallEndpoint(EPBULK_OUT); |
samux | 0:61e5ecd27a36 | 666 | } |
samux | 0:61e5ecd27a36 | 667 | |
samux | 0:61e5ecd27a36 | 668 | csw.Status = CSW_FAILED; |
samux | 0:61e5ecd27a36 | 669 | sendCSW(); |
samux | 0:61e5ecd27a36 | 670 | return false; |
samux | 0:61e5ecd27a36 | 671 | } |
samux | 0:61e5ecd27a36 | 672 | |
samux | 0:61e5ecd27a36 | 673 | return true; |
samux | 0:61e5ecd27a36 | 674 | } |
samux | 0:61e5ecd27a36 | 675 | |
samux | 0:61e5ecd27a36 | 676 | |
samux | 0:61e5ecd27a36 | 677 | |
samux | 0:61e5ecd27a36 | 678 | |
samux | 0:61e5ecd27a36 | 679 | |
samux | 0:61e5ecd27a36 | 680 | // Called in ISR context |
samux | 0:61e5ecd27a36 | 681 | // Set configuration. Return false if the |
samux | 0:61e5ecd27a36 | 682 | // configuration is not supported. |
samux | 0:61e5ecd27a36 | 683 | bool USBMSD_HID::USBCallback_setConfiguration(uint8_t configuration) |
samux | 0:61e5ecd27a36 | 684 | { |
samux | 0:61e5ecd27a36 | 685 | if (configuration != DEFAULT_CONFIGURATION) { |
samux | 0:61e5ecd27a36 | 686 | return false; |
samux | 0:61e5ecd27a36 | 687 | } |
samux | 0:61e5ecd27a36 | 688 | |
samux | 0:61e5ecd27a36 | 689 | // Configure endpoints > 0 |
samux | 0:61e5ecd27a36 | 690 | addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK); |
samux | 0:61e5ecd27a36 | 691 | addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
samux | 0:61e5ecd27a36 | 692 | |
samux | 0:61e5ecd27a36 | 693 | //activate readings |
samux | 0:61e5ecd27a36 | 694 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
samux | 0:61e5ecd27a36 | 695 | |
samux | 0:61e5ecd27a36 | 696 | // Configure endpoints > 0 |
samux | 0:61e5ecd27a36 | 697 | addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT); |
samux | 0:61e5ecd27a36 | 698 | addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT); |
samux | 0:61e5ecd27a36 | 699 | |
samux | 0:61e5ecd27a36 | 700 | // We activate the endpoint to be able to recceive data |
samux | 0:61e5ecd27a36 | 701 | readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT); |
samux | 0:61e5ecd27a36 | 702 | return true; |
samux | 0:61e5ecd27a36 | 703 | } |
samux | 0:61e5ecd27a36 | 704 | |
samux | 0:61e5ecd27a36 | 705 | |
samux | 0:61e5ecd27a36 | 706 | uint8_t * USBMSD_HID::stringIinterfaceDesc() |
samux | 0:61e5ecd27a36 | 707 | { |
samux | 0:61e5ecd27a36 | 708 | static uint8_t stringIinterfaceDescriptor[] = { |
samux | 0:61e5ecd27a36 | 709 | 0x08, //bLength |
samux | 0:61e5ecd27a36 | 710 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
samux | 0:61e5ecd27a36 | 711 | 'M',0,'S',0,'D',0 //bString iInterface - MSD |
samux | 0:61e5ecd27a36 | 712 | }; |
samux | 0:61e5ecd27a36 | 713 | return stringIinterfaceDescriptor; |
samux | 0:61e5ecd27a36 | 714 | } |
samux | 0:61e5ecd27a36 | 715 | |
samux | 0:61e5ecd27a36 | 716 | uint8_t * USBMSD_HID::stringIproductDesc() |
samux | 0:61e5ecd27a36 | 717 | { |
samux | 0:61e5ecd27a36 | 718 | static uint8_t stringIproductDescriptor[] = { |
samux | 0:61e5ecd27a36 | 719 | 0x12, //bLength |
samux | 0:61e5ecd27a36 | 720 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
samux | 0:61e5ecd27a36 | 721 | 'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio |
samux | 0:61e5ecd27a36 | 722 | }; |
samux | 0:61e5ecd27a36 | 723 | return stringIproductDescriptor; |
samux | 0:61e5ecd27a36 | 724 | } |
samux | 0:61e5ecd27a36 | 725 | |
samux | 0:61e5ecd27a36 | 726 | |
samux | 0:61e5ecd27a36 | 727 | uint8_t * USBMSD_HID::reportDesc() { |
samux | 0:61e5ecd27a36 | 728 | static uint8_t reportDescriptor[] = { |
samux | 0:61e5ecd27a36 | 729 | 0x06, LSB(0xFFAB), MSB(0xFFAB), |
samux | 0:61e5ecd27a36 | 730 | 0x0A, LSB(0x0200), MSB(0x0200), |
samux | 0:61e5ecd27a36 | 731 | 0xA1, 0x01, // Collection 0x01 |
samux | 0:61e5ecd27a36 | 732 | 0x75, 0x08, // report size = 8 bits |
samux | 0:61e5ecd27a36 | 733 | 0x15, 0x00, // logical minimum = 0 |
samux | 0:61e5ecd27a36 | 734 | 0x26, 0xFF, 0x00, // logical maximum = 255 |
samux | 0:61e5ecd27a36 | 735 | 0x95, input_length, // report count |
samux | 0:61e5ecd27a36 | 736 | 0x09, 0x01, // usage |
samux | 0:61e5ecd27a36 | 737 | 0x81, 0x02, // Input (array) |
samux | 0:61e5ecd27a36 | 738 | 0x95, output_length, // report count |
samux | 0:61e5ecd27a36 | 739 | 0x09, 0x02, // usage |
samux | 0:61e5ecd27a36 | 740 | 0x91, 0x02, // Output (array) |
samux | 0:61e5ecd27a36 | 741 | 0xC0 // end collection |
samux | 0:61e5ecd27a36 | 742 | |
samux | 0:61e5ecd27a36 | 743 | }; |
samux | 0:61e5ecd27a36 | 744 | reportLength = sizeof(reportDescriptor); |
samux | 0:61e5ecd27a36 | 745 | return reportDescriptor; |
samux | 0:61e5ecd27a36 | 746 | } |
samux | 0:61e5ecd27a36 | 747 | |
samux | 0:61e5ecd27a36 | 748 | uint8_t * USBMSD_HID::deviceDesc() { |
samux | 0:61e5ecd27a36 | 749 | static uint8_t deviceDescriptor[] = { |
samux | 0:61e5ecd27a36 | 750 | DEVICE_DESCRIPTOR_LENGTH, /* bLength */ |
samux | 0:61e5ecd27a36 | 751 | DEVICE_DESCRIPTOR, /* bDescriptorType */ |
samux | 0:61e5ecd27a36 | 752 | 0x10, /* bcdUSB (LSB) */ |
samux | 0:61e5ecd27a36 | 753 | 0x01, /* bcdUSB (MSB) */ |
samux | 0:61e5ecd27a36 | 754 | 0x00, /* bDeviceClass */ |
samux | 0:61e5ecd27a36 | 755 | 0x00, /* bDeviceSubClass */ |
samux | 0:61e5ecd27a36 | 756 | 0x00, /* bDeviceprotocol */ |
samux | 0:61e5ecd27a36 | 757 | MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */ |
samux | 0:61e5ecd27a36 | 758 | LSB(VENDOR_ID), /* idVendor (LSB) */ |
samux | 0:61e5ecd27a36 | 759 | MSB(VENDOR_ID), /* idVendor (MSB) */ |
samux | 0:61e5ecd27a36 | 760 | LSB(PRODUCT_ID), /* idProduct (LSB) */ |
samux | 0:61e5ecd27a36 | 761 | MSB(PRODUCT_ID), /* idProduct (MSB) */ |
samux | 0:61e5ecd27a36 | 762 | LSB(PRODUCT_RELEASE), /* bcdDevice (LSB) */ |
samux | 0:61e5ecd27a36 | 763 | MSB(PRODUCT_RELEASE), /* bcdDevice (MSB) */ |
samux | 0:61e5ecd27a36 | 764 | STRING_OFFSET_IMANUFACTURER, /* iManufacturer */ |
samux | 0:61e5ecd27a36 | 765 | STRING_OFFSET_IPRODUCT, /* iProduct */ |
samux | 0:61e5ecd27a36 | 766 | STRING_OFFSET_ISERIAL, /* iSerialNumber */ |
samux | 0:61e5ecd27a36 | 767 | 0x01 /* bNumConfigurations */ |
samux | 0:61e5ecd27a36 | 768 | }; |
samux | 0:61e5ecd27a36 | 769 | return deviceDescriptor; |
samux | 0:61e5ecd27a36 | 770 | } |
samux | 0:61e5ecd27a36 | 771 | |
samux | 0:61e5ecd27a36 | 772 | |
samux | 0:61e5ecd27a36 | 773 | uint8_t * USBMSD_HID::configurationDesc() |
samux | 0:61e5ecd27a36 | 774 | { |
samux | 0:61e5ecd27a36 | 775 | static uint8_t configDescriptor[] = { |
samux | 0:61e5ecd27a36 | 776 | |
samux | 0:61e5ecd27a36 | 777 | // Configuration 1 |
samux | 0:61e5ecd27a36 | 778 | 9, // bLength |
samux | 0:61e5ecd27a36 | 779 | 2, // bDescriptorType |
samux | 0:61e5ecd27a36 | 780 | LSB(9 + 9 + 7 + 7 + 9 + 9 + 7 + 7), // wTotalLength |
samux | 0:61e5ecd27a36 | 781 | MSB(9 + 9 + 7 + 7 + 9 + 9 + 7 + 7), |
samux | 0:61e5ecd27a36 | 782 | 0x02, // bNumInterfaces |
samux | 0:61e5ecd27a36 | 783 | 0x01, // bConfigurationValue: 0x01 is used to select this configuration |
samux | 0:61e5ecd27a36 | 784 | 0x00, // iConfiguration: no string to describe this configuration |
samux | 0:61e5ecd27a36 | 785 | 0xC0, // bmAttributes |
samux | 0:61e5ecd27a36 | 786 | C_POWER(500), // bMaxPower, device power consumption is 500 mA |
samux | 0:61e5ecd27a36 | 787 | |
samux | 0:61e5ecd27a36 | 788 | |
samux | 0:61e5ecd27a36 | 789 | |
samux | 0:61e5ecd27a36 | 790 | INTERFACE_DESCRIPTOR_LENGTH, // bLength |
samux | 0:61e5ecd27a36 | 791 | INTERFACE_DESCRIPTOR, // bDescriptorType |
samux | 0:61e5ecd27a36 | 792 | 0x00, // bInterfaceNumber |
samux | 0:61e5ecd27a36 | 793 | 0x00, // bAlternateSetting |
samux | 0:61e5ecd27a36 | 794 | 0x02, // bNumEndpoints |
samux | 0:61e5ecd27a36 | 795 | HID_CLASS, // bInterfaceClass |
samux | 0:61e5ecd27a36 | 796 | HID_SUBCLASS_NONE, // bInterfaceSubClass |
samux | 0:61e5ecd27a36 | 797 | HID_PROTOCOL_NONE, // bInterfaceProtocol |
samux | 0:61e5ecd27a36 | 798 | 0x00, // iInterface |
samux | 0:61e5ecd27a36 | 799 | |
samux | 0:61e5ecd27a36 | 800 | HID_DESCRIPTOR_LENGTH, // bLength |
samux | 0:61e5ecd27a36 | 801 | HID_DESCRIPTOR, // bDescriptorType |
samux | 0:61e5ecd27a36 | 802 | LSB(HID_VERSION_1_11), // bcdHID (LSB) |
samux | 0:61e5ecd27a36 | 803 | MSB(HID_VERSION_1_11), // bcdHID (MSB) |
samux | 0:61e5ecd27a36 | 804 | 0x00, // bCountryCode |
samux | 0:61e5ecd27a36 | 805 | 0x01, // bNumDescriptors |
samux | 0:61e5ecd27a36 | 806 | REPORT_DESCRIPTOR, // bDescriptorType |
samux | 0:61e5ecd27a36 | 807 | LSB(this->reportDescLength()), // wDescriptorLength (LSB) |
samux | 0:61e5ecd27a36 | 808 | MSB(this->reportDescLength()), // wDescriptorLength (MSB) |
samux | 0:61e5ecd27a36 | 809 | |
samux | 0:61e5ecd27a36 | 810 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
samux | 0:61e5ecd27a36 | 811 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
samux | 0:61e5ecd27a36 | 812 | PHY_TO_DESC(EPINT_IN), // bEndpointAddress |
samux | 0:61e5ecd27a36 | 813 | E_INTERRUPT, // bmAttributes |
samux | 0:61e5ecd27a36 | 814 | LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) |
samux | 0:61e5ecd27a36 | 815 | MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) |
samux | 0:61e5ecd27a36 | 816 | 10, // bInterval (milliseconds) |
samux | 0:61e5ecd27a36 | 817 | |
samux | 0:61e5ecd27a36 | 818 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
samux | 0:61e5ecd27a36 | 819 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
samux | 0:61e5ecd27a36 | 820 | PHY_TO_DESC(EPINT_OUT), // bEndpointAddress |
samux | 0:61e5ecd27a36 | 821 | E_INTERRUPT, // bmAttributes |
samux | 0:61e5ecd27a36 | 822 | LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) |
samux | 0:61e5ecd27a36 | 823 | MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) |
samux | 0:61e5ecd27a36 | 824 | 10, // bInterval (milliseconds) |
samux | 0:61e5ecd27a36 | 825 | |
samux | 0:61e5ecd27a36 | 826 | // Interface 0, Alternate Setting 0, MSC Class |
samux | 0:61e5ecd27a36 | 827 | 9, // bLength |
samux | 0:61e5ecd27a36 | 828 | 4, // bDescriptorType |
samux | 0:61e5ecd27a36 | 829 | 0x01, // bInterfaceNumber |
samux | 0:61e5ecd27a36 | 830 | 0x00, // bAlternateSetting |
samux | 0:61e5ecd27a36 | 831 | 0x02, // bNumEndpoints |
samux | 0:61e5ecd27a36 | 832 | 0x08, // bInterfaceClass |
samux | 0:61e5ecd27a36 | 833 | 0x06, // bInterfaceSubClass |
samux | 0:61e5ecd27a36 | 834 | 0x50, // bInterfaceProtocol |
samux | 0:61e5ecd27a36 | 835 | 0x04, // iInterface |
samux | 0:61e5ecd27a36 | 836 | |
samux | 0:61e5ecd27a36 | 837 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
samux | 0:61e5ecd27a36 | 838 | 7, // bLength |
samux | 0:61e5ecd27a36 | 839 | 5, // bDescriptorType |
samux | 0:61e5ecd27a36 | 840 | PHY_TO_DESC(EPBULK_IN), // bEndpointAddress |
samux | 0:61e5ecd27a36 | 841 | 0x02, // bmAttributes (0x02=bulk) |
samux | 0:61e5ecd27a36 | 842 | LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) |
samux | 0:61e5ecd27a36 | 843 | MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) |
samux | 0:61e5ecd27a36 | 844 | 0, // bInterval |
samux | 0:61e5ecd27a36 | 845 | |
samux | 0:61e5ecd27a36 | 846 | // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 |
samux | 0:61e5ecd27a36 | 847 | 7, // bLength |
samux | 0:61e5ecd27a36 | 848 | 5, // bDescriptorType |
samux | 0:61e5ecd27a36 | 849 | PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress |
samux | 0:61e5ecd27a36 | 850 | 0x02, // bmAttributes (0x02=bulk) |
samux | 0:61e5ecd27a36 | 851 | LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) |
samux | 0:61e5ecd27a36 | 852 | MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) |
samux | 0:61e5ecd27a36 | 853 | 0, // bInterval |
samux | 0:61e5ecd27a36 | 854 | |
samux | 0:61e5ecd27a36 | 855 | |
samux | 0:61e5ecd27a36 | 856 | }; |
samux | 0:61e5ecd27a36 | 857 | return configDescriptor; |
samux | 0:61e5ecd27a36 | 858 | } |