2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
mbed_official
Date:
Thu Oct 17 10:15:19 2013 +0100
Revision:
18:37c948cf0dbf
Parent:
17:c7b1b8451598
Child:
23:759ec18ee1a7
Synchronized with git revision 7585a23e6c29c41da4793a46a746ac3ec743b780

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 17:c7b1b8451598 23 #define __DEBUG__ 0
mbed_official 17:c7b1b8451598 24 #ifndef __MODULE__
mbed_official 17:c7b1b8451598 25 #define __MODULE__ "WANDongle.cpp"
mbed_official 17:c7b1b8451598 26 #endif
mbed_official 17:c7b1b8451598 27
mbed_official 18:37c948cf0dbf 28 #include "dbg.h"
mbed_official 18:37c948cf0dbf 29 #include <stdint.h>
mbed_official 17:c7b1b8451598 30 #include "rtos.h"
mbed_official 17:c7b1b8451598 31
mbed_official 17:c7b1b8451598 32 #include "WANDongle.h"
mbed_official 17:c7b1b8451598 33 #include "WANDongleInitializer.h"
mbed_official 17:c7b1b8451598 34
mbed_official 17:c7b1b8451598 35 WANDongle::WANDongle() : m_pInitializer(NULL), m_serialCount(0), m_totalInitializers(0)
mbed_official 17:c7b1b8451598 36 {
mbed_official 17:c7b1b8451598 37 host = USBHost::getHostInst();
mbed_official 17:c7b1b8451598 38 init();
mbed_official 17:c7b1b8451598 39 }
mbed_official 17:c7b1b8451598 40
mbed_official 17:c7b1b8451598 41
mbed_official 17:c7b1b8451598 42 bool WANDongle::connected() {
mbed_official 17:c7b1b8451598 43 return dev_connected;
mbed_official 17:c7b1b8451598 44 }
mbed_official 17:c7b1b8451598 45
mbed_official 17:c7b1b8451598 46 bool WANDongle::tryConnect()
mbed_official 17:c7b1b8451598 47 {
mbed_official 17:c7b1b8451598 48 //FIXME should run on USB thread
mbed_official 17:c7b1b8451598 49
mbed_official 18:37c948cf0dbf 50 USB_DBG("Trying to connect device");
mbed_official 17:c7b1b8451598 51
mbed_official 17:c7b1b8451598 52 if (dev_connected) {
mbed_official 17:c7b1b8451598 53 return true;
mbed_official 17:c7b1b8451598 54 }
mbed_official 17:c7b1b8451598 55
mbed_official 17:c7b1b8451598 56 m_pInitializer = NULL;
mbed_official 17:c7b1b8451598 57
mbed_official 17:c7b1b8451598 58 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++)
mbed_official 17:c7b1b8451598 59 {
mbed_official 17:c7b1b8451598 60 if ((dev = host->getDevice(i)) != NULL)
mbed_official 17:c7b1b8451598 61 {
mbed_official 17:c7b1b8451598 62 m_pInitializer = NULL; //Will be set in setVidPid callback
mbed_official 17:c7b1b8451598 63
mbed_official 18:37c948cf0dbf 64 USB_DBG("Enumerate");
mbed_official 17:c7b1b8451598 65 int ret = host->enumerate(dev, this);
mbed_official 17:c7b1b8451598 66 if(ret)
mbed_official 17:c7b1b8451598 67 {
mbed_official 17:c7b1b8451598 68 return false;
mbed_official 17:c7b1b8451598 69 }
mbed_official 17:c7b1b8451598 70
mbed_official 18:37c948cf0dbf 71 USB_DBG("Device has VID:%04x PID:%04x", dev->getVid(), dev->getPid());
mbed_official 17:c7b1b8451598 72
mbed_official 17:c7b1b8451598 73 if(m_pInitializer) //If an initializer has been found
mbed_official 17:c7b1b8451598 74 {
mbed_official 18:37c948cf0dbf 75 USB_DBG("m_pInitializer=%p", m_pInitializer);
mbed_official 18:37c948cf0dbf 76 USB_DBG("m_pInitializer->getSerialVid()=%04x", m_pInitializer->getSerialVid());
mbed_official 18:37c948cf0dbf 77 USB_DBG("m_pInitializer->getSerialPid()=%04x", m_pInitializer->getSerialPid());
mbed_official 17:c7b1b8451598 78 if ((dev->getVid() == m_pInitializer->getSerialVid()) && (dev->getPid() == m_pInitializer->getSerialPid()))
mbed_official 17:c7b1b8451598 79 {
mbed_official 18:37c948cf0dbf 80 USB_DBG("The dongle is in virtual serial mode");
mbed_official 17:c7b1b8451598 81 host->registerDriver(dev, 0, this, &WANDongle::init);
mbed_official 17:c7b1b8451598 82 m_serialCount = m_pInitializer->getSerialPortCount();
mbed_official 17:c7b1b8451598 83 if( m_serialCount > WANDONGLE_MAX_SERIAL_PORTS )
mbed_official 17:c7b1b8451598 84 {
mbed_official 17:c7b1b8451598 85 m_serialCount = WANDONGLE_MAX_SERIAL_PORTS;
mbed_official 17:c7b1b8451598 86 }
mbed_official 17:c7b1b8451598 87 for(int j = 0; j < m_serialCount; j++)
mbed_official 17:c7b1b8451598 88 {
mbed_official 18:37c948cf0dbf 89 USB_DBG("Connecting serial port #%d", j+1);
mbed_official 18:37c948cf0dbf 90 USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, false));
mbed_official 18:37c948cf0dbf 91 USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, true));
mbed_official 17:c7b1b8451598 92 m_serial[j].connect( dev, m_pInitializer->getEp(dev, j, false), m_pInitializer->getEp(dev, j, true) );
mbed_official 17:c7b1b8451598 93 }
mbed_official 17:c7b1b8451598 94
mbed_official 18:37c948cf0dbf 95 USB_DBG("Device connected");
mbed_official 17:c7b1b8451598 96
mbed_official 17:c7b1b8451598 97 dev_connected = true;
mbed_official 17:c7b1b8451598 98
mbed_official 17:c7b1b8451598 99
mbed_official 17:c7b1b8451598 100 return true;
mbed_official 17:c7b1b8451598 101 }
mbed_official 17:c7b1b8451598 102 else if ((dev->getVid() == m_pInitializer->getMSDVid()) && (dev->getPid() == m_pInitializer->getMSDPid()))
mbed_official 17:c7b1b8451598 103 {
mbed_official 18:37c948cf0dbf 104 USB_DBG("Vodafone K3370 dongle detected in MSD mode");
mbed_official 17:c7b1b8451598 105 //Try to switch
mbed_official 17:c7b1b8451598 106 if( m_pInitializer->switchMode(dev) )
mbed_official 17:c7b1b8451598 107 {
mbed_official 18:37c948cf0dbf 108 USB_DBG("Switched OK");
mbed_official 17:c7b1b8451598 109 return false; //Will be connected on a next iteration
mbed_official 17:c7b1b8451598 110 }
mbed_official 17:c7b1b8451598 111 else
mbed_official 17:c7b1b8451598 112 {
mbed_official 18:37c948cf0dbf 113 USB_ERR("Could not switch mode");
mbed_official 17:c7b1b8451598 114 return false;
mbed_official 17:c7b1b8451598 115 }
mbed_official 17:c7b1b8451598 116 }
mbed_official 17:c7b1b8451598 117 } //if()
mbed_official 17:c7b1b8451598 118 } //if()
mbed_official 17:c7b1b8451598 119 } //for()
mbed_official 17:c7b1b8451598 120 return false;
mbed_official 17:c7b1b8451598 121 }
mbed_official 17:c7b1b8451598 122
mbed_official 17:c7b1b8451598 123 bool WANDongle::disconnect()
mbed_official 17:c7b1b8451598 124 {
mbed_official 17:c7b1b8451598 125 dev_connected = false;
mbed_official 17:c7b1b8451598 126 for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
mbed_official 17:c7b1b8451598 127 {
mbed_official 17:c7b1b8451598 128 m_serial[i].disconnect();
mbed_official 17:c7b1b8451598 129 }
mbed_official 17:c7b1b8451598 130 return true;
mbed_official 17:c7b1b8451598 131 }
mbed_official 17:c7b1b8451598 132
mbed_official 17:c7b1b8451598 133 int WANDongle::getDongleType()
mbed_official 17:c7b1b8451598 134 {
mbed_official 17:c7b1b8451598 135 if( m_pInitializer != NULL )
mbed_official 17:c7b1b8451598 136 {
mbed_official 17:c7b1b8451598 137 return m_pInitializer->getType();
mbed_official 17:c7b1b8451598 138 }
mbed_official 17:c7b1b8451598 139 else
mbed_official 17:c7b1b8451598 140 {
mbed_official 17:c7b1b8451598 141 return WAN_DONGLE_TYPE_UNKNOWN;
mbed_official 17:c7b1b8451598 142 }
mbed_official 17:c7b1b8451598 143 }
mbed_official 17:c7b1b8451598 144
mbed_official 17:c7b1b8451598 145 IUSBHostSerial& WANDongle::getSerial(int index)
mbed_official 17:c7b1b8451598 146 {
mbed_official 17:c7b1b8451598 147 return m_serial[index];
mbed_official 17:c7b1b8451598 148 }
mbed_official 17:c7b1b8451598 149
mbed_official 17:c7b1b8451598 150 int WANDongle::getSerialCount()
mbed_official 17:c7b1b8451598 151 {
mbed_official 17:c7b1b8451598 152 return m_serialCount;
mbed_official 17:c7b1b8451598 153 }
mbed_official 17:c7b1b8451598 154
mbed_official 17:c7b1b8451598 155 //Private methods
mbed_official 17:c7b1b8451598 156 void WANDongle::init()
mbed_official 17:c7b1b8451598 157 {
mbed_official 17:c7b1b8451598 158 m_pInitializer = NULL;
mbed_official 17:c7b1b8451598 159 dev_connected = false;
mbed_official 17:c7b1b8451598 160 for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
mbed_official 17:c7b1b8451598 161 {
mbed_official 17:c7b1b8451598 162 m_serial[i].init(host);
mbed_official 17:c7b1b8451598 163 }
mbed_official 17:c7b1b8451598 164 }
mbed_official 17:c7b1b8451598 165
mbed_official 17:c7b1b8451598 166
mbed_official 17:c7b1b8451598 167 /*virtual*/ void WANDongle::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 17:c7b1b8451598 168 {
mbed_official 17:c7b1b8451598 169 WANDongleInitializer* initializer;
mbed_official 17:c7b1b8451598 170
mbed_official 18:37c948cf0dbf 171 for(int i = 0; i < m_totalInitializers; i++)
mbed_official 17:c7b1b8451598 172 {
mbed_official 17:c7b1b8451598 173 initializer = m_Initializers[i];
mbed_official 18:37c948cf0dbf 174 USB_DBG("initializer=%p", initializer);
mbed_official 18:37c948cf0dbf 175 USB_DBG("initializer->getSerialVid()=%04x", initializer->getSerialVid());
mbed_official 18:37c948cf0dbf 176 USB_DBG("initializer->getSerialPid()=%04x", initializer->getSerialPid());
mbed_official 17:c7b1b8451598 177 if ((dev->getVid() == initializer->getSerialVid()) && (dev->getPid() == initializer->getSerialPid()))
mbed_official 17:c7b1b8451598 178 {
mbed_official 18:37c948cf0dbf 179 USB_DBG("The dongle is in virtual serial mode");
mbed_official 17:c7b1b8451598 180 m_pInitializer = initializer;
mbed_official 17:c7b1b8451598 181 break;
mbed_official 17:c7b1b8451598 182 }
mbed_official 17:c7b1b8451598 183 else if ((dev->getVid() == initializer->getMSDVid()) && (dev->getPid() == initializer->getMSDPid()))
mbed_official 17:c7b1b8451598 184 {
mbed_official 18:37c948cf0dbf 185 USB_DBG("Dongle detected in MSD mode");
mbed_official 17:c7b1b8451598 186 m_pInitializer = initializer;
mbed_official 17:c7b1b8451598 187 break;
mbed_official 17:c7b1b8451598 188 }
mbed_official 17:c7b1b8451598 189 initializer++;
mbed_official 17:c7b1b8451598 190 } //for
mbed_official 17:c7b1b8451598 191 if(m_pInitializer)
mbed_official 17:c7b1b8451598 192 {
mbed_official 17:c7b1b8451598 193 m_pInitializer->setVidPid(vid, pid);
mbed_official 17:c7b1b8451598 194 }
mbed_official 17:c7b1b8451598 195 }
mbed_official 17:c7b1b8451598 196
mbed_official 17:c7b1b8451598 197 /*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 198 {
mbed_official 17:c7b1b8451598 199 if(m_pInitializer)
mbed_official 17:c7b1b8451598 200 {
mbed_official 17:c7b1b8451598 201 return m_pInitializer->parseInterface(intf_nb, intf_class, intf_subclass, intf_protocol);
mbed_official 17:c7b1b8451598 202 }
mbed_official 17:c7b1b8451598 203 else
mbed_official 17:c7b1b8451598 204 {
mbed_official 17:c7b1b8451598 205 return false;
mbed_official 17:c7b1b8451598 206 }
mbed_official 17:c7b1b8451598 207 }
mbed_official 17:c7b1b8451598 208
mbed_official 17:c7b1b8451598 209 /*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 210 {
mbed_official 17:c7b1b8451598 211 if(m_pInitializer)
mbed_official 17:c7b1b8451598 212 {
mbed_official 17:c7b1b8451598 213 return m_pInitializer->useEndpoint(intf_nb, type, dir);
mbed_official 17:c7b1b8451598 214 }
mbed_official 17:c7b1b8451598 215 else
mbed_official 17:c7b1b8451598 216 {
mbed_official 17:c7b1b8451598 217 return false;
mbed_official 17:c7b1b8451598 218 }
mbed_official 17:c7b1b8451598 219 }
mbed_official 17:c7b1b8451598 220
mbed_official 17:c7b1b8451598 221
mbed_official 17:c7b1b8451598 222 bool WANDongle::addInitializer(WANDongleInitializer* pInitializer)
mbed_official 17:c7b1b8451598 223 {
mbed_official 17:c7b1b8451598 224 if (m_totalInitializers >= WANDONGLE_MAX_INITIALIZERS)
mbed_official 17:c7b1b8451598 225 return false;
mbed_official 17:c7b1b8451598 226 m_Initializers[m_totalInitializers++] = pInitializer;
mbed_official 17:c7b1b8451598 227 return true;
mbed_official 17:c7b1b8451598 228 }
mbed_official 17:c7b1b8451598 229
mbed_official 17:c7b1b8451598 230 WANDongle::~WANDongle()
mbed_official 17:c7b1b8451598 231 {
mbed_official 18:37c948cf0dbf 232 for(int i = 0; i < m_totalInitializers; i++)
mbed_official 17:c7b1b8451598 233 delete m_Initializers[i];
mbed_official 17:c7b1b8451598 234 }
mbed_official 17:c7b1b8451598 235
mbed_official 17:c7b1b8451598 236 #endif /* USBHOST_3GMODULE */