Simple USSD test.

Dependencies:   FATFileSystem mbed-rtos

Dependents:   CellularUSBModem

Fork of USBHost by mbed official

Committer:
mbed_official
Date:
Wed Mar 06 16:27:14 2013 +0000
Revision:
0:a554658735bf
Child:
4:b320d68e98e7
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:a554658735bf 1 /* Copyright (c) 2010-2012 mbed.org, MIT License
mbed_official 0:a554658735bf 2 *
mbed_official 0:a554658735bf 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mbed_official 0:a554658735bf 4 * and associated documentation files (the "Software"), to deal in the Software without
mbed_official 0:a554658735bf 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mbed_official 0:a554658735bf 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mbed_official 0:a554658735bf 7 * Software is furnished to do so, subject to the following conditions:
mbed_official 0:a554658735bf 8 *
mbed_official 0:a554658735bf 9 * The above copyright notice and this permission notice shall be included in all copies or
mbed_official 0:a554658735bf 10 * substantial portions of the Software.
mbed_official 0:a554658735bf 11 *
mbed_official 0:a554658735bf 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mbed_official 0:a554658735bf 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mbed_official 0:a554658735bf 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mbed_official 0:a554658735bf 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:a554658735bf 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbed_official 0:a554658735bf 17 */
mbed_official 0:a554658735bf 18
mbed_official 0:a554658735bf 19 #include "USBHostMSD.h"
mbed_official 0:a554658735bf 20
mbed_official 0:a554658735bf 21 #if USBHOST_MSD
mbed_official 0:a554658735bf 22
mbed_official 0:a554658735bf 23 #include "dbg.h"
mbed_official 0:a554658735bf 24
mbed_official 0:a554658735bf 25 #define CBW_SIGNATURE 0x43425355
mbed_official 0:a554658735bf 26 #define CSW_SIGNATURE 0x53425355
mbed_official 0:a554658735bf 27
mbed_official 0:a554658735bf 28 #define DEVICE_TO_HOST 0x80
mbed_official 0:a554658735bf 29 #define HOST_TO_DEVICE 0x00
mbed_official 0:a554658735bf 30
mbed_official 0:a554658735bf 31 #define GET_MAX_LUN (0xFE)
mbed_official 0:a554658735bf 32
mbed_official 0:a554658735bf 33 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
mbed_official 0:a554658735bf 34 {
mbed_official 0:a554658735bf 35 host = USBHost::getHostInst();
mbed_official 0:a554658735bf 36 init();
mbed_official 0:a554658735bf 37 }
mbed_official 0:a554658735bf 38
mbed_official 0:a554658735bf 39 void USBHostMSD::init() {
mbed_official 0:a554658735bf 40 dev_connected = false;
mbed_official 0:a554658735bf 41 dev = NULL;
mbed_official 0:a554658735bf 42 bulk_in = NULL;
mbed_official 0:a554658735bf 43 bulk_out = NULL;
mbed_official 0:a554658735bf 44 dev_connected = false;
mbed_official 0:a554658735bf 45 blockSize = 0;
mbed_official 0:a554658735bf 46 blockCount = 0;
mbed_official 0:a554658735bf 47 msd_intf = -1;
mbed_official 0:a554658735bf 48 msd_device_found = false;
mbed_official 0:a554658735bf 49 disk_init = false;
mbed_official 0:a554658735bf 50 dev_connected = false;
mbed_official 0:a554658735bf 51 }
mbed_official 0:a554658735bf 52
mbed_official 0:a554658735bf 53
mbed_official 0:a554658735bf 54 bool USBHostMSD::connected()
mbed_official 0:a554658735bf 55 {
mbed_official 0:a554658735bf 56 return dev_connected;
mbed_official 0:a554658735bf 57 }
mbed_official 0:a554658735bf 58
mbed_official 0:a554658735bf 59 bool USBHostMSD::connect()
mbed_official 0:a554658735bf 60 {
mbed_official 0:a554658735bf 61 U8 i;
mbed_official 0:a554658735bf 62
mbed_official 0:a554658735bf 63 if (dev_connected) {
mbed_official 0:a554658735bf 64 return true;
mbed_official 0:a554658735bf 65 }
mbed_official 0:a554658735bf 66
mbed_official 0:a554658735bf 67 for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mbed_official 0:a554658735bf 68 if ((dev = host->getDevice(i)) != NULL) {
mbed_official 0:a554658735bf 69
mbed_official 0:a554658735bf 70 if(host->enumerate(dev, this))
mbed_official 0:a554658735bf 71 break;
mbed_official 0:a554658735bf 72
mbed_official 0:a554658735bf 73 if (msd_device_found) {
mbed_official 0:a554658735bf 74 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN);
mbed_official 0:a554658735bf 75 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT);
mbed_official 0:a554658735bf 76
mbed_official 0:a554658735bf 77 if (!bulk_in || !bulk_out)
mbed_official 0:a554658735bf 78 break;
mbed_official 0:a554658735bf 79
mbed_official 0:a554658735bf 80 USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p]", dev->getVid(), dev->getPid(), dev);
mbed_official 0:a554658735bf 81 dev->setName("MSD");
mbed_official 0:a554658735bf 82 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);
mbed_official 0:a554658735bf 83
mbed_official 0:a554658735bf 84 dev_connected = true;
mbed_official 0:a554658735bf 85 return true;
mbed_official 0:a554658735bf 86 }
mbed_official 0:a554658735bf 87 } //if()
mbed_official 0:a554658735bf 88 } //for()
mbed_official 0:a554658735bf 89 return false;
mbed_official 0:a554658735bf 90 }
mbed_official 0:a554658735bf 91
mbed_official 0:a554658735bf 92 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 93 {
mbed_official 0:a554658735bf 94 // we don't check VID/PID for MSD driver
mbed_official 0:a554658735bf 95 }
mbed_official 0:a554658735bf 96
mbed_official 0:a554658735bf 97 /*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
mbed_official 0:a554658735bf 98 {
mbed_official 0:a554658735bf 99 if ((msd_intf == -1) &&
mbed_official 0:a554658735bf 100 (intf_class == MSD_CLASS) &&
mbed_official 0:a554658735bf 101 (intf_subclass == 0x06) &&
mbed_official 0:a554658735bf 102 (intf_protocol == 0x50)) {
mbed_official 0:a554658735bf 103 msd_intf = intf_nb;
mbed_official 0:a554658735bf 104 return true;
mbed_official 0:a554658735bf 105 }
mbed_official 0:a554658735bf 106 return false;
mbed_official 0:a554658735bf 107 }
mbed_official 0:a554658735bf 108
mbed_official 0:a554658735bf 109 /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
mbed_official 0:a554658735bf 110 {
mbed_official 0:a554658735bf 111 if (intf_nb == msd_intf) {
mbed_official 0:a554658735bf 112 if (type == BULK_ENDPOINT) {
mbed_official 0:a554658735bf 113 msd_device_found = true;
mbed_official 0:a554658735bf 114 return true;
mbed_official 0:a554658735bf 115 }
mbed_official 0:a554658735bf 116 }
mbed_official 0:a554658735bf 117 return false;
mbed_official 0:a554658735bf 118 }
mbed_official 0:a554658735bf 119
mbed_official 0:a554658735bf 120
mbed_official 0:a554658735bf 121 int USBHostMSD::testUnitReady() {
mbed_official 0:a554658735bf 122 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
mbed_official 0:a554658735bf 123 }
mbed_official 0:a554658735bf 124
mbed_official 0:a554658735bf 125 int USBHostMSD::readCapacity() {
mbed_official 0:a554658735bf 126 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
mbed_official 0:a554658735bf 127 uint8_t result[8];
mbed_official 0:a554658735bf 128 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8);
mbed_official 0:a554658735bf 129 if (status == 0) {
mbed_official 0:a554658735bf 130 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3];
mbed_official 0:a554658735bf 131 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7];
mbed_official 0:a554658735bf 132 USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d\r\n", dev, blockCount, blockSize);
mbed_official 0:a554658735bf 133 }
mbed_official 0:a554658735bf 134 return status;
mbed_official 0:a554658735bf 135 }
mbed_official 0:a554658735bf 136
mbed_official 0:a554658735bf 137
mbed_official 0:a554658735bf 138 int USBHostMSD::SCSIRequestSense() {
mbed_official 0:a554658735bf 139 uint8_t cmd[5] = {0x03,0,0,0,0x13};
mbed_official 0:a554658735bf 140 uint8_t result[19];
mbed_official 0:a554658735bf 141 int status = SCSITransfer(cmd, 5, DEVICE_TO_HOST, result, 19);
mbed_official 0:a554658735bf 142 return status;
mbed_official 0:a554658735bf 143 }
mbed_official 0:a554658735bf 144
mbed_official 0:a554658735bf 145
mbed_official 0:a554658735bf 146 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
mbed_official 0:a554658735bf 147 uint8_t evpd = (page_code == 0) ? 0 : 1;
mbed_official 0:a554658735bf 148 uint8_t cmd[6] = {0x12, (lun << 5) | evpd, page_code, 0, 36, 0};
mbed_official 0:a554658735bf 149 uint8_t result[36];
mbed_official 0:a554658735bf 150 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36);
mbed_official 0:a554658735bf 151 if (status == 0) {
mbed_official 0:a554658735bf 152 char vid_pid[9];
mbed_official 0:a554658735bf 153 memcpy(vid_pid, &result[8], 8);
mbed_official 0:a554658735bf 154 vid_pid[8] = 0;
mbed_official 0:a554658735bf 155 USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid);
mbed_official 0:a554658735bf 156
mbed_official 0:a554658735bf 157 memcpy(vid_pid, &result[16], 8);
mbed_official 0:a554658735bf 158 USB_INFO("MSD [dev: %p] - Produc ID: %s", dev, vid_pid);
mbed_official 0:a554658735bf 159
mbed_official 0:a554658735bf 160 memcpy(vid_pid, &result[32], 4);
mbed_official 0:a554658735bf 161 vid_pid[4] = 0;
mbed_official 0:a554658735bf 162 USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid);
mbed_official 0:a554658735bf 163 }
mbed_official 0:a554658735bf 164 return status;
mbed_official 0:a554658735bf 165 }
mbed_official 0:a554658735bf 166
mbed_official 0:a554658735bf 167 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
mbed_official 0:a554658735bf 168 // if ep stalled: send clear feature
mbed_official 0:a554658735bf 169 if (res == USB_TYPE_STALL_ERROR) {
mbed_official 0:a554658735bf 170 res = host->controlWrite( dev,
mbed_official 0:a554658735bf 171 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
mbed_official 0:a554658735bf 172 CLEAR_FEATURE,
mbed_official 0:a554658735bf 173 0,
mbed_official 0:a554658735bf 174 ep->getAddress(),
mbed_official 0:a554658735bf 175 NULL,
mbed_official 0:a554658735bf 176 0);
mbed_official 0:a554658735bf 177 // set state to IDLE if clear feature successful
mbed_official 0:a554658735bf 178 if (res == USB_TYPE_OK) {
mbed_official 0:a554658735bf 179 ep->setState(USB_TYPE_IDLE);
mbed_official 0:a554658735bf 180 }
mbed_official 0:a554658735bf 181 }
mbed_official 0:a554658735bf 182
mbed_official 0:a554658735bf 183 if (res != USB_TYPE_OK)
mbed_official 0:a554658735bf 184 return -1;
mbed_official 0:a554658735bf 185
mbed_official 0:a554658735bf 186 return 0;
mbed_official 0:a554658735bf 187 }
mbed_official 0:a554658735bf 188
mbed_official 0:a554658735bf 189
mbed_official 0:a554658735bf 190 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
mbed_official 0:a554658735bf 191
mbed_official 0:a554658735bf 192 int res = 0;
mbed_official 0:a554658735bf 193
mbed_official 0:a554658735bf 194 cbw.Signature = CBW_SIGNATURE;
mbed_official 0:a554658735bf 195 cbw.Tag = 0;
mbed_official 0:a554658735bf 196 cbw.DataLength = transfer_len;
mbed_official 0:a554658735bf 197 cbw.Flags = flags;
mbed_official 0:a554658735bf 198 cbw.LUN = 0;
mbed_official 0:a554658735bf 199 cbw.CBLength = cmd_len;
mbed_official 0:a554658735bf 200 memset(cbw.CB,0,sizeof(cbw.CB));
mbed_official 0:a554658735bf 201 if (cmd) {
mbed_official 0:a554658735bf 202 memcpy(cbw.CB,cmd,cmd_len);
mbed_official 0:a554658735bf 203 }
mbed_official 0:a554658735bf 204
mbed_official 0:a554658735bf 205 // send the cbw
mbed_official 0:a554658735bf 206 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
mbed_official 0:a554658735bf 207 if (checkResult(res, bulk_out))
mbed_official 0:a554658735bf 208 return -1;
mbed_official 0:a554658735bf 209
mbed_official 0:a554658735bf 210 // data stage if needed
mbed_official 0:a554658735bf 211 if (data) {
mbed_official 0:a554658735bf 212 if (flags == HOST_TO_DEVICE) {
mbed_official 0:a554658735bf 213
mbed_official 0:a554658735bf 214 res = host->bulkWrite(dev, bulk_out, data, transfer_len);
mbed_official 0:a554658735bf 215 if (checkResult(res, bulk_out))
mbed_official 0:a554658735bf 216 return -1;
mbed_official 0:a554658735bf 217
mbed_official 0:a554658735bf 218 } else if (flags == DEVICE_TO_HOST) {
mbed_official 0:a554658735bf 219
mbed_official 0:a554658735bf 220 res = host->bulkRead(dev, bulk_in, data, transfer_len);
mbed_official 0:a554658735bf 221 if (checkResult(res, bulk_in))
mbed_official 0:a554658735bf 222 return -1;
mbed_official 0:a554658735bf 223 }
mbed_official 0:a554658735bf 224 }
mbed_official 0:a554658735bf 225
mbed_official 0:a554658735bf 226 // status stage
mbed_official 0:a554658735bf 227 csw.Signature = 0;
mbed_official 0:a554658735bf 228 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
mbed_official 0:a554658735bf 229 if (checkResult(res, bulk_in))
mbed_official 0:a554658735bf 230 return -1;
mbed_official 0:a554658735bf 231
mbed_official 0:a554658735bf 232 if (csw.Signature != CSW_SIGNATURE) {
mbed_official 0:a554658735bf 233 return -1;
mbed_official 0:a554658735bf 234 }
mbed_official 0:a554658735bf 235
mbed_official 0:a554658735bf 236 // ModeSense?
mbed_official 0:a554658735bf 237 if ((csw.Status == 1) && (cmd[0] != 0x03))
mbed_official 0:a554658735bf 238 return SCSIRequestSense();
mbed_official 0:a554658735bf 239
mbed_official 0:a554658735bf 240 USB_DBG("recv csw: status: %d", csw.Status);
mbed_official 0:a554658735bf 241 return csw.Status;
mbed_official 0:a554658735bf 242 }
mbed_official 0:a554658735bf 243
mbed_official 0:a554658735bf 244
mbed_official 0:a554658735bf 245 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
mbed_official 0:a554658735bf 246 uint8_t cmd[10];
mbed_official 0:a554658735bf 247 memset(cmd,0,10);
mbed_official 0:a554658735bf 248 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
mbed_official 0:a554658735bf 249
mbed_official 0:a554658735bf 250 cmd[2] = (block >> 24) & 0xff;
mbed_official 0:a554658735bf 251 cmd[3] = (block >> 16) & 0xff;
mbed_official 0:a554658735bf 252 cmd[4] = (block >> 8) & 0xff;
mbed_official 0:a554658735bf 253 cmd[5] = block & 0xff;
mbed_official 0:a554658735bf 254
mbed_official 0:a554658735bf 255 cmd[7] = (nbBlock >> 8) & 0xff;
mbed_official 0:a554658735bf 256 cmd[8] = nbBlock & 0xff;
mbed_official 0:a554658735bf 257
mbed_official 0:a554658735bf 258 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
mbed_official 0:a554658735bf 259 }
mbed_official 0:a554658735bf 260
mbed_official 0:a554658735bf 261
mbed_official 0:a554658735bf 262 int USBHostMSD::disk_initialize() {
mbed_official 0:a554658735bf 263 USB_DBG("FILESYSTEM: init");
mbed_official 0:a554658735bf 264 U8 i, timeout = 10;
mbed_official 0:a554658735bf 265
mbed_official 0:a554658735bf 266 for (i = 0; i < timeout; i++) {
mbed_official 0:a554658735bf 267 if (!testUnitReady())
mbed_official 0:a554658735bf 268 break;
mbed_official 0:a554658735bf 269 }
mbed_official 0:a554658735bf 270
mbed_official 0:a554658735bf 271 if (i == timeout) {
mbed_official 0:a554658735bf 272 disk_init = false;
mbed_official 0:a554658735bf 273 return -1;
mbed_official 0:a554658735bf 274 }
mbed_official 0:a554658735bf 275 inquiry(0, 0);
mbed_official 0:a554658735bf 276 disk_init = 1;
mbed_official 0:a554658735bf 277 return readCapacity();
mbed_official 0:a554658735bf 278 }
mbed_official 0:a554658735bf 279
mbed_official 0:a554658735bf 280 int USBHostMSD::disk_write(const uint8_t *buffer, uint64_t block_number) {
mbed_official 0:a554658735bf 281 USB_DBG("FILESYSTEM: write block: %lld", block_number);
mbed_official 0:a554658735bf 282 if (!disk_init) {
mbed_official 0:a554658735bf 283 disk_initialize();
mbed_official 0:a554658735bf 284 }
mbed_official 0:a554658735bf 285 if (!disk_init)
mbed_official 0:a554658735bf 286 return -1;
mbed_official 0:a554658735bf 287 return dataTransfer((uint8_t *)buffer, block_number, 1, HOST_TO_DEVICE);
mbed_official 0:a554658735bf 288 }
mbed_official 0:a554658735bf 289
mbed_official 0:a554658735bf 290 int USBHostMSD::disk_read(uint8_t * buffer, uint64_t block_number) {
mbed_official 0:a554658735bf 291 USB_DBG("FILESYSTEM: read block %lld", block_number);
mbed_official 0:a554658735bf 292 if (!disk_init) {
mbed_official 0:a554658735bf 293 disk_initialize();
mbed_official 0:a554658735bf 294 }
mbed_official 0:a554658735bf 295 if (!disk_init)
mbed_official 0:a554658735bf 296 return -1;
mbed_official 0:a554658735bf 297 return dataTransfer((uint8_t *)buffer, block_number, 1, DEVICE_TO_HOST);
mbed_official 0:a554658735bf 298 }
mbed_official 0:a554658735bf 299
mbed_official 0:a554658735bf 300 uint64_t USBHostMSD::disk_sectors() {
mbed_official 0:a554658735bf 301 USB_DBG("FILESYSTEM: sectors");
mbed_official 0:a554658735bf 302 if (!disk_init) {
mbed_official 0:a554658735bf 303 disk_initialize();
mbed_official 0:a554658735bf 304 }
mbed_official 0:a554658735bf 305 if (!disk_init)
mbed_official 0:a554658735bf 306 return 0;
mbed_official 0:a554658735bf 307 return blockCount;
mbed_official 0:a554658735bf 308 }
mbed_official 0:a554658735bf 309
mbed_official 0:a554658735bf 310 #endif
mbed_official 0:a554658735bf 311