1 /* Copyright (c) 2010-2012 mbed.org, MIT License
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:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
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.
19 #include "USBHostConf.h"
21 #ifdef USBHOST_3GMODULE
27 #include "WANDongle.h"
28 #include "WANDongleInitializer.h"
30 WANDongle::WANDongle() : m_pInitializer(NULL
), m_serialCount(0), m_totalInitializers(0)
32 host
= USBHost::getHostInst();
37 bool WANDongle::connected() {
41 bool WANDongle::tryConnect()
43 //FIXME should run on USB thread
45 USB_DBG("Trying to connect device");
48 USB_DBG("Device is already connected!");
52 m_pInitializer
= NULL
;
54 //Protect from concurrent access from USB thread
55 USBHost::Lock
lock(host
);
57 for (int i
= 0; i
< MAX_DEVICE_CONNECTED
; i
++)
59 if ((dev
= host
->getDevice(i
)) != NULL
)
61 m_pInitializer
= NULL
; //Will be set in setVidPid callback
64 int ret
= host
->enumerate(dev
, this);
70 USB_DBG("Device has VID:%04x PID:%04x", dev
->getVid(), dev
->getPid());
72 if(m_pInitializer
) //If an initializer has been found
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()))
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
)
84 m_serialCount
= WANDONGLE_MAX_SERIAL_PORTS
;
86 for(int j
= 0; j
< m_serialCount
; j
++)
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) );
94 USB_DBG("Device connected");
101 else if ((dev
->getVid() == m_pInitializer
->getMSDVid()) && (dev
->getPid() == m_pInitializer
->getMSDPid()))
103 USB_DBG("Vodafone K3370 dongle detected in MSD mode");
105 if( m_pInitializer
->switchMode(dev
) )
107 USB_DBG("Switched OK");
108 return false; //Will be connected on a next iteration
112 USB_ERR("Could not switch mode");
122 bool WANDongle::disconnect()
124 dev_connected
= false;
125 for(int i
= 0; i
< WANDONGLE_MAX_SERIAL_PORTS
; i
++)
127 m_serial
[i
].disconnect();
132 int WANDongle::getDongleType()
134 if( m_pInitializer
!= NULL
)
136 return m_pInitializer
->getType();
140 return WAN_DONGLE_TYPE_UNKNOWN
;
144 IUSBHostSerial
& WANDongle::getSerial(int index
)
146 return m_serial
[index
];
149 int WANDongle::getSerialCount()
151 return m_serialCount
;
155 void WANDongle::init()
157 m_pInitializer
= NULL
;
158 dev_connected
= false;
159 for(int i
= 0; i
< WANDONGLE_MAX_SERIAL_PORTS
; i
++)
161 m_serial
[i
].init(host
);
166 /*virtual*/ void WANDongle::setVidPid(uint16_t vid
, uint16_t pid
)
168 WANDongleInitializer
* initializer
;
170 for(int i
= 0; i
< m_totalInitializers
; i
++)
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()))
178 USB_DBG("The dongle is in virtual serial mode");
179 m_pInitializer
= initializer
;
182 else if ((dev
->getVid() == initializer
->getMSDVid()) && (dev
->getPid() == initializer
->getMSDPid()))
184 USB_DBG("Dongle detected in MSD mode");
185 m_pInitializer
= initializer
;
192 m_pInitializer
->setVidPid(vid
, pid
);
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
200 return m_pInitializer
->parseInterface(intf_nb
, intf_class
, intf_subclass
, intf_protocol
);
208 /*virtual*/ bool WANDongle::useEndpoint(uint8_t intf_nb
, ENDPOINT_TYPE type
, ENDPOINT_DIRECTION dir
) //Must return true if the endpoint will be used
212 return m_pInitializer
->useEndpoint(intf_nb
, type
, dir
);
221 bool WANDongle::addInitializer(WANDongleInitializer
* pInitializer
)
223 if (m_totalInitializers
>= WANDONGLE_MAX_INITIALIZERS
)
225 m_Initializers
[m_totalInitializers
++] = pInitializer
;
229 WANDongle::~WANDongle()
231 for(int i
= 0; i
< m_totalInitializers
; i
++)
232 delete m_Initializers
[i
];
235 #endif /* USBHOST_3GMODULE */