|
1 /* |
|
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Adheres to the UsbMan USB Class Controller API and talks to C32 |
|
16 * to manage the stub2.CSY that is used to provide a virtual |
|
17 * serial port service to clients |
|
18 * |
|
19 */ |
|
20 |
|
21 /** |
|
22 @file |
|
23 */ |
|
24 |
|
25 #include "Stub2CC.h" |
|
26 #include <usb_std.h> |
|
27 #include <es_ini.h> |
|
28 #include <d32usbc.h> |
|
29 #include <usb/usblogger.h> |
|
30 |
|
31 #ifdef __FLOG_ACTIVE |
|
32 _LIT8(KLogComponent, "STUB2CC"); |
|
33 #endif |
|
34 |
|
35 |
|
36 #include "usbmaninternalconstants.h" |
|
37 |
|
38 |
|
39 // Panic category |
|
40 _LIT( Kstub2CcPanicCategory, "Usbstub2Cc" ); |
|
41 |
|
42 |
|
43 /** |
|
44 * Panic codes for the USB stub2 Class Controller. |
|
45 */ |
|
46 enum Tstub2CcPanic |
|
47 { |
|
48 /** Class called while in an illegal state */ |
|
49 EBadApiCall = 0, |
|
50 /** Asynchronous function called (not needed, as all requests complete synchronously) */ |
|
51 EUnusedFunction = 1, |
|
52 /** Error reading ini file. */ |
|
53 EPanicBadIniFile = 2, |
|
54 /** Bad value for the iNumberOfstub2Functions member.*/ |
|
55 EPanicBadNumberOfstub2Functions = 3, |
|
56 |
|
57 EPanicUnexpectedStatus, |
|
58 EPanicUnexpectedState |
|
59 |
|
60 }; |
|
61 |
|
62 _LIT16(KIfcName, "SubCC 1 Interface"); |
|
63 const TInt KMaxPacketTypeInterrupt = 64; |
|
64 const TInt KPollInterval = 128; |
|
65 |
|
66 |
|
67 |
|
68 /** |
|
69 * Constructs a CUsbstub2ClassController object |
|
70 * |
|
71 * @param aOwner USB Device that owns and manages the class |
|
72 * |
|
73 * @return A new CUsbstub2ClassController object |
|
74 */ |
|
75 CUsbstub2ClassController* CUsbstub2ClassController::NewL( |
|
76 MUsbClassControllerNotify& aOwner) |
|
77 { |
|
78 CUsbstub2ClassController* r = new (ELeave) CUsbstub2ClassController(aOwner); |
|
79 CleanupStack::PushL(r); |
|
80 r->ConstructL(); |
|
81 CleanupStack::Pop(); |
|
82 return r; |
|
83 } |
|
84 |
|
85 /** |
|
86 * Destructor |
|
87 */ |
|
88 CUsbstub2ClassController::~CUsbstub2ClassController() |
|
89 { |
|
90 Cancel(); |
|
91 |
|
92 iTimer.Close(); |
|
93 |
|
94 #ifndef __WINS__ |
|
95 iLdd.Close(); |
|
96 #endif |
|
97 |
|
98 } |
|
99 |
|
100 |
|
101 |
|
102 /** |
|
103 * Constructor. |
|
104 * |
|
105 * @param aOwner USB Device that owns and manages the class |
|
106 */ |
|
107 CUsbstub2ClassController::CUsbstub2ClassController( |
|
108 MUsbClassControllerNotify& aOwner) |
|
109 : CUsbClassControllerPlugIn(aOwner, Kstub2StartupPriority), |
|
110 iStartDelay(Kstub2CCDefaultDelay), |
|
111 iStopDelay(Kstub2CCDefaultDelay), |
|
112 iFailToStart(EFalse), |
|
113 iFailToStop(EFalse) |
|
114 |
|
115 { |
|
116 iTimer.CreateLocal(); |
|
117 } |
|
118 |
|
119 |
|
120 |
|
121 /** |
|
122 * 2nd Phase Construction. |
|
123 */ |
|
124 void CUsbstub2ClassController::ConstructL() |
|
125 { |
|
126 //read INI file |
|
127 TInt ret; |
|
128 CESockIniData* ini = 0; |
|
129 _LIT(KIniFile, "c:\\testdata\\config\\stub2cc.ini"); |
|
130 TRAP(ret, ini=CESockIniData::NewL(KIniFile)); |
|
131 if(ret!=KErrNone) |
|
132 return; |
|
133 |
|
134 CleanupStack::PushL(ini); |
|
135 |
|
136 TInt val; |
|
137 if ((ini->FindVar(_L("0x10203287"),_L("StartDelay"), val))) |
|
138 { |
|
139 iStartDelay = val; |
|
140 } |
|
141 if ((ini->FindVar(_L("0x10203287"),_L("StopDelay"), val))) |
|
142 { |
|
143 iStopDelay = val; |
|
144 } |
|
145 if ((ini->FindVar(_L("0x10203287"),_L("FailToStart"), val)) && val!=0) |
|
146 { |
|
147 iFailToStart = ETrue; |
|
148 } |
|
149 if ((ini->FindVar(_L("0x10203287"),_L("FailToStop"), val)) && val!=0 ) |
|
150 { |
|
151 iFailToStop = ETrue; |
|
152 } |
|
153 CleanupStack::PopAndDestroy(ini); |
|
154 } |
|
155 |
|
156 /** |
|
157 * Called by UsbMan when it wants to start the USB stub2 class. This always |
|
158 * completes immediately. |
|
159 * |
|
160 * @param aStatus The caller's request status, filled in with an error code |
|
161 */ |
|
162 void CUsbstub2ClassController::Start(TRequestStatus& aStatus) |
|
163 { |
|
164 LOG_FUNC |
|
165 |
|
166 aStatus = KRequestPending; |
|
167 iReportStatus = &aStatus; |
|
168 //If we are already started then just complete the request. |
|
169 if (iState == EUsbServiceStarted) |
|
170 { |
|
171 User::RequestComplete(iReportStatus, KErrNone); |
|
172 return; |
|
173 } |
|
174 |
|
175 if (iFailToStart) |
|
176 { |
|
177 User::RequestComplete(iReportStatus, KErrGeneral); |
|
178 return; |
|
179 } |
|
180 |
|
181 iState = EUsbServiceStarting; |
|
182 #ifndef __WINS__ |
|
183 TInt ret = iLdd.Open(0); |
|
184 LOGTEXT2(_L8("Open LDD, ret=%d"), ret); |
|
185 ret = SetUpInterface(); |
|
186 LOGTEXT2(_L8("SetUpInterface(), ret=%d"), ret); |
|
187 #endif |
|
188 |
|
189 |
|
190 iTimer.After(iStatus, iStartDelay*1000); //convert from usec to msec |
|
191 SetActive(); |
|
192 } |
|
193 |
|
194 /** |
|
195 * Called by UsbMan when it wants to stop the USB stub2 class. |
|
196 * |
|
197 * @param aStatus The caller's request status: always set to KErrNone |
|
198 */ |
|
199 void CUsbstub2ClassController::Stop(TRequestStatus& aStatus) |
|
200 { |
|
201 LOG_FUNC |
|
202 |
|
203 aStatus = KRequestPending; |
|
204 iReportStatus = &aStatus; |
|
205 //If we are already idle then just complete the request. |
|
206 if (iState == EUsbServiceIdle) |
|
207 { |
|
208 User::RequestComplete(iReportStatus, KErrNone); |
|
209 return; |
|
210 } |
|
211 |
|
212 if (iFailToStop) |
|
213 { |
|
214 User::RequestComplete(iReportStatus, KErrGeneral); |
|
215 return; |
|
216 } |
|
217 |
|
218 iState = EUsbServiceStopping; |
|
219 |
|
220 #ifndef __WINS__ |
|
221 iLdd.Close(); |
|
222 #endif |
|
223 |
|
224 iTimer.After(iStatus, iStopDelay*1000); //convert from usec to msec |
|
225 SetActive(); |
|
226 } |
|
227 |
|
228 /** |
|
229 * Gets information about the descriptor which this class provides. |
|
230 * |
|
231 * @param aDescriptorInfo Descriptor info structure filled in by this function |
|
232 */ |
|
233 void CUsbstub2ClassController::GetDescriptorInfo(TUsbDescriptor& aDescriptorInfo) const |
|
234 { |
|
235 LOG_FUNC |
|
236 |
|
237 aDescriptorInfo.iLength = Kstub2DescriptorLength; |
|
238 aDescriptorInfo.iNumInterfaces = Kstub2NumberOfInterfacesPerstub2Function; |
|
239 } |
|
240 |
|
241 |
|
242 /** |
|
243 * Standard active object RunL. |
|
244 */ |
|
245 void CUsbstub2ClassController::RunL() |
|
246 { |
|
247 LOG_FUNC |
|
248 |
|
249 __ASSERT_DEBUG( iStatus == KErrNone, _USB_PANIC(Kstub2CcPanicCategory, EPanicUnexpectedStatus) ); |
|
250 switch (iState) |
|
251 { |
|
252 case EUsbServiceStarting: |
|
253 iState = EUsbServiceStarted; |
|
254 break; |
|
255 case EUsbServiceStopping: |
|
256 iState = EUsbServiceIdle; |
|
257 break; |
|
258 default: |
|
259 _USB_PANIC(Kstub2CcPanicCategory, EPanicUnexpectedState); |
|
260 } |
|
261 *iReportStatus = KErrNone; |
|
262 User::RequestComplete(iReportStatus, iStatus.Int()); |
|
263 } |
|
264 |
|
265 /** |
|
266 * Standard active object cancellation function. Never called because this |
|
267 * class has no asynchronous requests. |
|
268 */ |
|
269 void CUsbstub2ClassController::DoCancel() |
|
270 { |
|
271 |
|
272 if (IsActive()) |
|
273 { |
|
274 iTimer.Cancel(); |
|
275 } |
|
276 switch (iState) |
|
277 { |
|
278 case EUsbServiceStarting: |
|
279 iState = EUsbServiceIdle; |
|
280 break; |
|
281 case EUsbServiceStopping: |
|
282 iState = EUsbServiceStarted; |
|
283 break; |
|
284 default: |
|
285 _USB_PANIC(Kstub2CcPanicCategory, EPanicUnexpectedState); |
|
286 } |
|
287 *iReportStatus = KErrNone; |
|
288 User::RequestComplete(iReportStatus, KErrCancel); |
|
289 } |
|
290 |
|
291 /** |
|
292 * Standard active object error function. Never called because this class has |
|
293 * no asynchronous requests, and hence its RunL is never called. |
|
294 * |
|
295 * @param aError The error code (unused) |
|
296 * @return Always KErrNone to avoid an active scheduler panic |
|
297 */ |
|
298 TInt CUsbstub2ClassController::RunError(TInt /*aError*/) |
|
299 { |
|
300 __ASSERT_DEBUG( EFalse, _USB_PANIC(Kstub2CcPanicCategory, EUnusedFunction) ); |
|
301 return KErrNone; |
|
302 } |
|
303 |
|
304 TInt CUsbstub2ClassController::SetUpInterface() |
|
305 /** |
|
306 * Set up the interface for use. This involves finding a "Interrupt IN" |
|
307 * endpoint and, if found, configuring the interface. |
|
308 */ |
|
309 { |
|
310 LOGTEXT(_L8(">>CCdcControlInterface::SetUpInterface")); |
|
311 |
|
312 TUsbDeviceCaps dCaps; |
|
313 TInt ret = iLdd.DeviceCaps(dCaps); |
|
314 LOGTEXT(_L8("\tchecking result of DeviceCaps")); |
|
315 if ( ret ) |
|
316 { |
|
317 LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), ret); |
|
318 return ret; |
|
319 } |
|
320 |
|
321 const TUint KRequiredNumberOfEndpoints = 1; // in addition to endpoint 0. |
|
322 |
|
323 const TUint totalEndpoints = static_cast<TUint>(dCaps().iTotalEndpoints); |
|
324 LOGTEXT2(_L8("\tiTotalEndpoints = %d"), totalEndpoints); |
|
325 if ( totalEndpoints < KRequiredNumberOfEndpoints ) |
|
326 { |
|
327 LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), |
|
328 KErrGeneral); |
|
329 return KErrGeneral; |
|
330 } |
|
331 |
|
332 // Endpoints |
|
333 TUsbcEndpointData data[KUsbcMaxEndpoints]; |
|
334 TPtr8 dataptr(reinterpret_cast<TUint8*>(data), sizeof(data), sizeof(data)); |
|
335 ret = iLdd.EndpointCaps(dataptr); |
|
336 LOGTEXT(_L8("\tchecking result of EndpointCaps")); |
|
337 if ( ret ) |
|
338 { |
|
339 LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), ret); |
|
340 return ret; |
|
341 } |
|
342 |
|
343 // Set the active interface |
|
344 TUsbcInterfaceInfoBuf ifc; |
|
345 TBool epFound = EFalse; |
|
346 for ( TUint i = 0 ; i < totalEndpoints ; i++ ) |
|
347 { |
|
348 const TUsbcEndpointCaps* caps = &data[i].iCaps; |
|
349 __ASSERT_DEBUG(caps,_USB_PANIC(Kstub2CcPanicCategory, EPanicUnexpectedStatus)); |
|
350 |
|
351 if (data[i].iInUse) |
|
352 { |
|
353 continue; |
|
354 } |
|
355 |
|
356 if ((caps->iTypesAndDir & (KUsbEpTypeInterrupt | KUsbEpDirIn)) == |
|
357 (KUsbEpTypeInterrupt | KUsbEpDirIn)) |
|
358 { |
|
359 // EEndpoint1 is interrupt endpoint |
|
360 ifc().iEndpointData[0].iType = KUsbEpTypeInterrupt; |
|
361 ifc().iEndpointData[0].iDir = KUsbEpDirIn; |
|
362 |
|
363 //get the max packet size it can potentially support |
|
364 //it's possible that it can support Isoch (1023) which is greater |
|
365 //than max for Int at 64 |
|
366 TInt maxSize = Min(caps->MaxPacketSize(), KMaxPacketTypeInterrupt); |
|
367 |
|
368 ifc().iEndpointData[0].iSize = maxSize; |
|
369 |
|
370 ifc().iEndpointData[0].iInterval = KPollInterval; |
|
371 epFound = ETrue; |
|
372 break; |
|
373 } |
|
374 } |
|
375 LOGTEXT(_L8("\tchecking epFound")); |
|
376 if ( !epFound ) |
|
377 { |
|
378 LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), |
|
379 KErrGeneral); |
|
380 return KErrGeneral; |
|
381 } |
|
382 |
|
383 TName ifcName(KIfcName); |
|
384 ifc().iString = &ifcName; |
|
385 ifc().iTotalEndpointsUsed = KRequiredNumberOfEndpoints; |
|
386 // Codes taken from USBCDC 1.1. |
|
387 ifc().iClass.iClassNum = 0x02; // Table 15- Communication Interface Class |
|
388 ifc().iClass.iSubClassNum = 0x02; // Table 16- Abstract Control Model |
|
389 ifc().iClass.iProtocolNum = 0x01; // Table 17- Hayes compatible |
|
390 |
|
391 LOGTEXT(_L8("\tabout to call SetInterface")); |
|
392 // Zero effectively indicates that alternate interfaces are not used. |
|
393 ret = iLdd.SetInterface(0, ifc); |
|
394 |
|
395 LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), ret); |
|
396 return ret; |
|
397 } |
|
398 |
|
399 // |
|
400 // End of file |