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