Support Isochronous transfer additionally

Dependents:   USBHostC270_example_GR-PEACH USBHostDac_example USBHostDac_Audio_in_out

Fork of USBHost_custom by Renesas

Committer:
HinoNaka
Date:
Fri Apr 21 07:23:33 2017 +0000
Revision:
41:5c3ebf7372ee
Parent:
24:868cbfe611a7
Support Isochronous transfer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 17:c7b1b8451598 1 /* Copyright (c) 2010-2012 mbed.org, MIT License
mbed_official 17:c7b1b8451598 2 *
mbed_official 17:c7b1b8451598 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mbed_official 17:c7b1b8451598 4 * and associated documentation files (the "Software"), to deal in the Software without
mbed_official 17:c7b1b8451598 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mbed_official 17:c7b1b8451598 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mbed_official 17:c7b1b8451598 7 * Software is furnished to do so, subject to the following conditions:
mbed_official 17:c7b1b8451598 8 *
mbed_official 17:c7b1b8451598 9 * The above copyright notice and this permission notice shall be included in all copies or
mbed_official 17:c7b1b8451598 10 * substantial portions of the Software.
mbed_official 17:c7b1b8451598 11 *
mbed_official 17:c7b1b8451598 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mbed_official 17:c7b1b8451598 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mbed_official 17:c7b1b8451598 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mbed_official 17:c7b1b8451598 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 17:c7b1b8451598 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbed_official 17:c7b1b8451598 17 */
mbed_official 17:c7b1b8451598 18
mbed_official 17:c7b1b8451598 19 #include "USBHostConf.h"
mbed_official 17:c7b1b8451598 20
mbed_official 17:c7b1b8451598 21 #ifdef USBHOST_3GMODULE
mbed_official 17:c7b1b8451598 22
mbed_official 18:37c948cf0dbf 23 #include "dbg.h"
mbed_official 18:37c948cf0dbf 24 #include <stdint.h>
mbed_official 17:c7b1b8451598 25 #include "rtos.h"
mbed_official 17:c7b1b8451598 26
mbed_official 17:c7b1b8451598 27 #include "WANDongle.h"
mbed_official 17:c7b1b8451598 28 #include "WANDongleInitializer.h"
mbed_official 17:c7b1b8451598 29
mbed_official 17:c7b1b8451598 30 WANDongle::WANDongle() : m_pInitializer(NULL), m_serialCount(0), m_totalInitializers(0)
mbed_official 17:c7b1b8451598 31 {
mbed_official 17:c7b1b8451598 32 host = USBHost::getHostInst();
mbed_official 17:c7b1b8451598 33 init();
mbed_official 17:c7b1b8451598 34 }
mbed_official 17:c7b1b8451598 35
mbed_official 17:c7b1b8451598 36
mbed_official 17:c7b1b8451598 37 bool WANDongle::connected() {
mbed_official 17:c7b1b8451598 38 return dev_connected;
mbed_official 17:c7b1b8451598 39 }
mbed_official 17:c7b1b8451598 40
mbed_official 17:c7b1b8451598 41 bool WANDongle::tryConnect()
mbed_official 17:c7b1b8451598 42 {
mbed_official 17:c7b1b8451598 43 //FIXME should run on USB thread
mbed_official 17:c7b1b8451598 44
mbed_official 18:37c948cf0dbf 45 USB_DBG("Trying to connect device");
mbed_official 17:c7b1b8451598 46
mbed_official 17:c7b1b8451598 47 if (dev_connected) {
mbed_official 24:868cbfe611a7 48 USB_DBG("Device is already connected!");
mbed_official 17:c7b1b8451598 49 return true;
mbed_official 17:c7b1b8451598 50 }
mbed_official 24:868cbfe611a7 51
mbed_official 17:c7b1b8451598 52 m_pInitializer = NULL;
mbed_official 24:868cbfe611a7 53
mbed_official 23:759ec18ee1a7 54 //Protect from concurrent access from USB thread
mbed_official 23:759ec18ee1a7 55 USBHost::Lock lock(host);
mbed_official 17:c7b1b8451598 56
mbed_official 17:c7b1b8451598 57 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++)
mbed_official 17:c7b1b8451598 58 {
mbed_official 17:c7b1b8451598 59 if ((dev = host->getDevice(i)) != NULL)
mbed_official 17:c7b1b8451598 60 {
mbed_official 17:c7b1b8451598 61 m_pInitializer = NULL; //Will be set in setVidPid callback
mbed_official 24:868cbfe611a7 62
mbed_official 18:37c948cf0dbf 63 USB_DBG("Enumerate");
mbed_official 17:c7b1b8451598 64 int ret = host->enumerate(dev, this);
mbed_official 17:c7b1b8451598 65 if(ret)
mbed_official 17:c7b1b8451598 66 {
mbed_official 17:c7b1b8451598 67 return false;
mbed_official 17:c7b1b8451598 68 }
mbed_official 24:868cbfe611a7 69
mbed_official 18:37c948cf0dbf 70 USB_DBG("Device has VID:%04x PID:%04x", dev->getVid(), dev->getPid());
mbed_official 24:868cbfe611a7 71
mbed_official 17:c7b1b8451598 72 if(m_pInitializer) //If an initializer has been found
mbed_official 17:c7b1b8451598 73 {
mbed_official 18:37c948cf0dbf 74 USB_DBG("m_pInitializer=%p", m_pInitializer);
mbed_official 18:37c948cf0dbf 75 USB_DBG("m_pInitializer->getSerialVid()=%04x", m_pInitializer->getSerialVid());
mbed_official 18:37c948cf0dbf 76 USB_DBG("m_pInitializer->getSerialPid()=%04x", m_pInitializer->getSerialPid());
mbed_official 17:c7b1b8451598 77 if ((dev->getVid() == m_pInitializer->getSerialVid()) && (dev->getPid() == m_pInitializer->getSerialPid()))
mbed_official 17:c7b1b8451598 78 {
mbed_official 18:37c948cf0dbf 79 USB_DBG("The dongle is in virtual serial mode");
mbed_official 17:c7b1b8451598 80 host->registerDriver(dev, 0, this, &WANDongle::init);
mbed_official 17:c7b1b8451598 81 m_serialCount = m_pInitializer->getSerialPortCount();
mbed_official 17:c7b1b8451598 82 if( m_serialCount > WANDONGLE_MAX_SERIAL_PORTS )
mbed_official 17:c7b1b8451598 83 {
mbed_official 17:c7b1b8451598 84 m_serialCount = WANDONGLE_MAX_SERIAL_PORTS;
mbed_official 17:c7b1b8451598 85 }
mbed_official 17:c7b1b8451598 86 for(int j = 0; j < m_serialCount; j++)
mbed_official 17:c7b1b8451598 87 {
mbed_official 18:37c948cf0dbf 88 USB_DBG("Connecting serial port #%d", j+1);
mbed_official 18:37c948cf0dbf 89 USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, false));
mbed_official 18:37c948cf0dbf 90 USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, true));
mbed_official 17:c7b1b8451598 91 m_serial[j].connect( dev, m_pInitializer->getEp(dev, j, false), m_pInitializer->getEp(dev, j, true) );
mbed_official 17:c7b1b8451598 92 }
mbed_official 24:868cbfe611a7 93
mbed_official 18:37c948cf0dbf 94 USB_DBG("Device connected");
mbed_official 24:868cbfe611a7 95
mbed_official 17:c7b1b8451598 96 dev_connected = true;
mbed_official 24:868cbfe611a7 97
mbed_official 24:868cbfe611a7 98
mbed_official 17:c7b1b8451598 99 return true;
mbed_official 17:c7b1b8451598 100 }
mbed_official 17:c7b1b8451598 101 else if ((dev->getVid() == m_pInitializer->getMSDVid()) && (dev->getPid() == m_pInitializer->getMSDPid()))
mbed_official 17:c7b1b8451598 102 {
mbed_official 18:37c948cf0dbf 103 USB_DBG("Vodafone K3370 dongle detected in MSD mode");
mbed_official 24:868cbfe611a7 104 //Try to switch
mbed_official 17:c7b1b8451598 105 if( m_pInitializer->switchMode(dev) )
mbed_official 17:c7b1b8451598 106 {
mbed_official 18:37c948cf0dbf 107 USB_DBG("Switched OK");
mbed_official 17:c7b1b8451598 108 return false; //Will be connected on a next iteration
mbed_official 17:c7b1b8451598 109 }
mbed_official 17:c7b1b8451598 110 else
mbed_official 17:c7b1b8451598 111 {
mbed_official 18:37c948cf0dbf 112 USB_ERR("Could not switch mode");
mbed_official 17:c7b1b8451598 113 return false;
mbed_official 17:c7b1b8451598 114 }
mbed_official 17:c7b1b8451598 115 }
mbed_official 17:c7b1b8451598 116 } //if()
mbed_official 17:c7b1b8451598 117 } //if()
mbed_official 17:c7b1b8451598 118 } //for()
mbed_official 17:c7b1b8451598 119 return false;
mbed_official 17:c7b1b8451598 120 }
mbed_official 17:c7b1b8451598 121
mbed_official 17:c7b1b8451598 122 bool WANDongle::disconnect()
mbed_official 17:c7b1b8451598 123 {
mbed_official 17:c7b1b8451598 124 dev_connected = false;
mbed_official 17:c7b1b8451598 125 for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
mbed_official 17:c7b1b8451598 126 {
mbed_official 17:c7b1b8451598 127 m_serial[i].disconnect();
mbed_official 17:c7b1b8451598 128 }
mbed_official 17:c7b1b8451598 129 return true;
mbed_official 17:c7b1b8451598 130 }
mbed_official 17:c7b1b8451598 131
mbed_official 17:c7b1b8451598 132 int WANDongle::getDongleType()
mbed_official 17:c7b1b8451598 133 {
mbed_official 17:c7b1b8451598 134 if( m_pInitializer != NULL )
mbed_official 17:c7b1b8451598 135 {
mbed_official 17:c7b1b8451598 136 return m_pInitializer->getType();
mbed_official 17:c7b1b8451598 137 }
mbed_official 17:c7b1b8451598 138 else
mbed_official 17:c7b1b8451598 139 {
mbed_official 17:c7b1b8451598 140 return WAN_DONGLE_TYPE_UNKNOWN;
mbed_official 17:c7b1b8451598 141 }
mbed_official 17:c7b1b8451598 142 }
mbed_official 17:c7b1b8451598 143
mbed_official 17:c7b1b8451598 144 IUSBHostSerial& WANDongle::getSerial(int index)
mbed_official 17:c7b1b8451598 145 {
mbed_official 17:c7b1b8451598 146 return m_serial[index];
mbed_official 17:c7b1b8451598 147 }
mbed_official 17:c7b1b8451598 148
mbed_official 17:c7b1b8451598 149 int WANDongle::getSerialCount()
mbed_official 17:c7b1b8451598 150 {
mbed_official 17:c7b1b8451598 151 return m_serialCount;
mbed_official 17:c7b1b8451598 152 }
mbed_official 17:c7b1b8451598 153
mbed_official 17:c7b1b8451598 154 //Private methods
mbed_official 17:c7b1b8451598 155 void WANDongle::init()
mbed_official 17:c7b1b8451598 156 {
mbed_official 17:c7b1b8451598 157 m_pInitializer = NULL;
mbed_official 17:c7b1b8451598 158 dev_connected = false;
mbed_official 17:c7b1b8451598 159 for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
mbed_official 17:c7b1b8451598 160 {
mbed_official 17:c7b1b8451598 161 m_serial[i].init(host);
mbed_official 17:c7b1b8451598 162 }
mbed_official 17:c7b1b8451598 163 }
mbed_official 17:c7b1b8451598 164
mbed_official 17:c7b1b8451598 165
mbed_official 17:c7b1b8451598 166 /*virtual*/ void WANDongle::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 17:c7b1b8451598 167 {
mbed_official 17:c7b1b8451598 168 WANDongleInitializer* initializer;
mbed_official 17:c7b1b8451598 169
mbed_official 18:37c948cf0dbf 170 for(int i = 0; i < m_totalInitializers; i++)
mbed_official 17:c7b1b8451598 171 {
mbed_official 17:c7b1b8451598 172 initializer = m_Initializers[i];
mbed_official 18:37c948cf0dbf 173 USB_DBG("initializer=%p", initializer);
mbed_official 18:37c948cf0dbf 174 USB_DBG("initializer->getSerialVid()=%04x", initializer->getSerialVid());
mbed_official 18:37c948cf0dbf 175 USB_DBG("initializer->getSerialPid()=%04x", initializer->getSerialPid());
mbed_official 17:c7b1b8451598 176 if ((dev->getVid() == initializer->getSerialVid()) && (dev->getPid() == initializer->getSerialPid()))
mbed_official 17:c7b1b8451598 177 {
mbed_official 18:37c948cf0dbf 178 USB_DBG("The dongle is in virtual serial mode");
mbed_official 17:c7b1b8451598 179 m_pInitializer = initializer;
mbed_official 17:c7b1b8451598 180 break;
mbed_official 17:c7b1b8451598 181 }
mbed_official 17:c7b1b8451598 182 else if ((dev->getVid() == initializer->getMSDVid()) && (dev->getPid() == initializer->getMSDPid()))
mbed_official 17:c7b1b8451598 183 {
mbed_official 18:37c948cf0dbf 184 USB_DBG("Dongle detected in MSD mode");
mbed_official 17:c7b1b8451598 185 m_pInitializer = initializer;
mbed_official 17:c7b1b8451598 186 break;
mbed_official 17:c7b1b8451598 187 }
mbed_official 17:c7b1b8451598 188 initializer++;
mbed_official 17:c7b1b8451598 189 } //for
mbed_official 17:c7b1b8451598 190 if(m_pInitializer)
mbed_official 17:c7b1b8451598 191 {
mbed_official 17:c7b1b8451598 192 m_pInitializer->setVidPid(vid, pid);
mbed_official 17:c7b1b8451598 193 }
mbed_official 17:c7b1b8451598 194 }
mbed_official 17:c7b1b8451598 195
mbed_official 17:c7b1b8451598 196 /*virtual*/ bool WANDongle::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 17:c7b1b8451598 197 {
mbed_official 17:c7b1b8451598 198 if(m_pInitializer)
mbed_official 17:c7b1b8451598 199 {
mbed_official 17:c7b1b8451598 200 return m_pInitializer->parseInterface(intf_nb, intf_class, intf_subclass, intf_protocol);
mbed_official 17:c7b1b8451598 201 }
mbed_official 17:c7b1b8451598 202 else
mbed_official 17:c7b1b8451598 203 {
mbed_official 17:c7b1b8451598 204 return false;
mbed_official 17:c7b1b8451598 205 }
mbed_official 17:c7b1b8451598 206 }
mbed_official 17:c7b1b8451598 207
mbed_official 17:c7b1b8451598 208 /*virtual*/ bool WANDongle::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
mbed_official 17:c7b1b8451598 209 {
mbed_official 17:c7b1b8451598 210 if(m_pInitializer)
mbed_official 17:c7b1b8451598 211 {
mbed_official 17:c7b1b8451598 212 return m_pInitializer->useEndpoint(intf_nb, type, dir);
mbed_official 17:c7b1b8451598 213 }
mbed_official 17:c7b1b8451598 214 else
mbed_official 17:c7b1b8451598 215 {
mbed_official 17:c7b1b8451598 216 return false;
mbed_official 17:c7b1b8451598 217 }
mbed_official 17:c7b1b8451598 218 }
mbed_official 17:c7b1b8451598 219
mbed_official 17:c7b1b8451598 220
mbed_official 17:c7b1b8451598 221 bool WANDongle::addInitializer(WANDongleInitializer* pInitializer)
mbed_official 17:c7b1b8451598 222 {
mbed_official 17:c7b1b8451598 223 if (m_totalInitializers >= WANDONGLE_MAX_INITIALIZERS)
mbed_official 17:c7b1b8451598 224 return false;
mbed_official 17:c7b1b8451598 225 m_Initializers[m_totalInitializers++] = pInitializer;
mbed_official 17:c7b1b8451598 226 return true;
mbed_official 17:c7b1b8451598 227 }
mbed_official 17:c7b1b8451598 228
mbed_official 17:c7b1b8451598 229 WANDongle::~WANDongle()
mbed_official 17:c7b1b8451598 230 {
mbed_official 18:37c948cf0dbf 231 for(int i = 0; i < m_totalInitializers; i++)
mbed_official 17:c7b1b8451598 232 delete m_Initializers[i];
mbed_official 17:c7b1b8451598 233 }
mbed_official 17:c7b1b8451598 234
mbed_official 17:c7b1b8451598 235 #endif /* USBHOST_3GMODULE */