-
Notifications
You must be signed in to change notification settings - Fork 1
/
Modem_Huawei_E160x.c
303 lines (262 loc) · 12.3 KB
/
Modem_Huawei_E160x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
LUFA Powered Wireless 3G Modem Host
Copyright (C) Mike Alexander, 2010.
Copyright (C) Dean Camera, 2010.
*/
/*
Copyright 2010 Mike Alexander (mike [at] mikealex [dot] com)
Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations
* needed to communication with an attached USB device. Descriptors are special computer-readable structures
* which the host requests upon device enumeration, to determine the device's capabilities and functions.
*/
#define INCLUDE_FROM_CONFIGDESCRIPTOR_C
#include "ConfigDescriptor.h"
// Interface Class values for the CDC data protocol
#define CDC_CSCP_CDCClass 0xff
#define CDC_CSCP_ACMSubclass 0xff
#define CDC_CSCP_ATCommandProtocol 0xff
#define CDC_CSCP_CDCDataClass 0xff
#define CDC_CSCP_NoDataSubclass 0xff
#define CDC_CSCP_NoDataProtocol 0xff
/** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
* routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
* with compatible devices.
*
* This routine searches for a CDC interface descriptor containing bulk data IN and OUT endpoints, and an interrupt event endpoint.
*
* \return An error code from the \ref CDCHost_GetConfigDescriptorDataCodes_t enum.
*/
uint8_t ProcessConfigurationDescriptor(void)
{
uint8_t ConfigDescriptorData[512];
void* CurrConfigLocation = ConfigDescriptorData;
uint16_t CurrConfigBytesRem;
USB_Descriptor_Interface_t* CDCControlInterface = NULL;
USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
USB_Descriptor_Endpoint_t* NotificationEndpoint = NULL;
/* Retrieve the entire configuration descriptor into the allocated buffer */
switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
{
case HOST_GETCONFIG_Successful:
break;
case HOST_GETCONFIG_InvalidData:
return InvalidConfigDataReturned;
case HOST_GETCONFIG_BuffOverflow:
return DescriptorTooLarge;
default:
return ControlError;
}
while (!(DataINEndpoint) || !(DataOUTEndpoint) || !(NotificationEndpoint))
{
/* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
if (!(CDCControlInterface) ||
USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
DComp_NextCDCDataInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
{
/* Check if we have already found the control interface's notification endpoint or not */
if (NotificationEndpoint)
{
/* Get the next CDC data interface from the configuration descriptor */
if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
DComp_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
{
/* Descriptor not found, error out */
return NoCompatibleInterfaceFound;
}
/* Clear any found endpoints */
DataINEndpoint = NULL;
DataOUTEndpoint = NULL;
}
else
{
/* Get the next CDC control interface from the configuration descriptor */
if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
{
/* Descriptor not found, error out */
return NoCompatibleInterfaceFound;
}
/* Save the interface in case we need to refer back to it later */
CDCControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
/* Clear any found endpoints */
NotificationEndpoint = NULL;
}
/* Skip the remainder of the loop as we have not found an endpoint yet */
continue;
}
/* Retrieve the endpoint address from the endpoint descriptor */
USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
/* If the endpoint is a IN type endpoint */
if (EndpointData->EndpointAddress & ENDPOINT_DIR_IN)
{
/* Check if the found endpoint is a interrupt or bulk type descriptor */
if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
NotificationEndpoint = EndpointData;
else
DataINEndpoint = EndpointData;
}
else
{
DataOUTEndpoint = EndpointData;
}
}
/* Configure the CDC data IN pipe */
Pipe_ConfigurePipe(CDC_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
/* Configure the CDC data OUT pipe */
Pipe_ConfigurePipe(CDC_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
/* Configure the CDC notification pipe */
Pipe_ConfigurePipe(CDC_NOTIFICATION_PIPE, EP_TYPE_INTERRUPT, NotificationEndpoint->EndpointAddress, NotificationEndpoint->EndpointSize, 1);
Pipe_SetInterruptPeriod(NotificationEndpoint->PollingIntervalMS);
/* Valid data found, return success */
return SuccessfulConfigRead;
}
/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
* configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
* descriptor processing if an incompatible descriptor configuration is found.
*
* This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
*
* \return A value from the DSEARCH_Return_ErrorCodes_t enum
*/
uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
{
USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
if (Header->Type == DTYPE_Interface)
{
USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
/* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
if ((Interface->Class == CDC_CSCP_CDCClass) &&
(Interface->SubClass == CDC_CSCP_ACMSubclass) &&
(Interface->Protocol == CDC_CSCP_ATCommandProtocol))
{
return DESCRIPTOR_SEARCH_Found;
}
}
return DESCRIPTOR_SEARCH_NotFound;
}
/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
* configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
* descriptor processing if an incompatible descriptor configuration is found.
*
* This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
*
* \return A value from the DSEARCH_Return_ErrorCodes_t enum
*/
uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
{
USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
if (Header->Type == DTYPE_Interface)
{
USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
/* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
if ((Interface->Class == CDC_CSCP_CDCDataClass) &&
(Interface->SubClass == CDC_CSCP_NoDataSubclass) &&
(Interface->Protocol == CDC_CSCP_NoDataProtocol))
{
return DESCRIPTOR_SEARCH_Found;
}
}
return DESCRIPTOR_SEARCH_NotFound;
}
/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
* configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
* descriptor processing if an incompatible descriptor configuration is found.
*
* This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
* aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
* using a different comparator to determine if it is another CDC class interface).
*
* \return A value from the DSEARCH_Return_ErrorCodes_t enum
*/
uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor)
{
USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
if (Header->Type == DTYPE_Endpoint)
{
USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
if (((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK) ||
((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT))
{
return DESCRIPTOR_SEARCH_Found;
}
}
else if (Header->Type == DTYPE_Interface)
{
return DESCRIPTOR_SEARCH_Fail;
}
return DESCRIPTOR_SEARCH_NotFound;
}
// Each modem will have different initialisation parameters that must be sent to the modem.
// The format of the list is a command, followed by the expected response to that command. The list must be double-NULL terminated.
// These dial commands are concatenated with the ones in Network_xxx.c, and will be sent before the
// dial commands defined in Network_xxx.c
// Do not put the actual dial comand (usually ATDT*99#) in this list (put it in Network_xxx.c).
const char* ModemDialCommands[] =
{
"AT\r\n","OK",
"AT&FE0V1X1&D2&C1S0=0\r\n","OK",
NULL, NULL
};
// USB Modems make use of a feature called USB mode switching. You can read about it at http://www.draisberghof.de/usb_modeswitch/
// Depending on the modem you will have to define different commands to switch the modem from storage mode to modem mode.
// One way to determine the correct command is to run the modem under Windows and use USBMonitor
// (http://www.hhdsoftware.com/Downloads/usb-monitor.html) to determine what commands are sent to the modem.
// Another method is to look at the code at http://www.draisberghof.de/usb_modeswitch/ to see if the codes for your modem have already been determined.
//
// This function returns:
// true if the modem has been enumerated correctly
// false if there is an error, or if we must wait for the modem to disconnect
bool ProcessModemUSBStates(void)
{
uint8_t ErrorCode;
Debug_Print("Send configuration command\r\n");
if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
{
Debug_Print("Control error (Set Config)\r\n");
Debug_PrintHex(ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR); // Indicate error via status LEDs
return false;
}
Debug_Print("Looking for modem device...");
// Get and process the configuration descriptor data
// First time through we expect a non-modem device. Once the device has disconnected and re-attached it should be OK
if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
{
if (ErrorCode == ControlError)
{
Debug_Print("Control error (Get Configuration)\r\n");
Debug_PrintHex(ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR); // Indicate error via status LEDs
return false;
}
else
{
Debug_Print("Not a modem device - switching modes\r\n");
USB_Host_SuspendBus(); // This will cause the device to disconnect and reconnect in modem mode
return false; // Wait until USB device disconnects
}
}
Debug_Print("Modem device enumerated\r\n");
return true;
}