Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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