USBHost library with fixes

Dependencies:   mbed-rtos FATFileSystem

Dependents:   mbedica

Committer:
zrussell3
Date:
Thu Dec 13 19:24:21 2018 +0000
Revision:
0:b176d95bb38f
Modified USBHost library to fix modifier input

Who changed what in which revision?

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