Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

Committer:
thedo
Date:
Fri Jul 21 01:26:02 2017 +0000
Revision:
166:240bc5a0f42a
gr-peach-opencv-project-sd-card

Who changed what in which revision?

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