os/kernelhwsrv/kerneltest/e32utils/testusbcldd/src/descriptors.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// f32test\testusbcldd\src\descriptors.cpp
sl@0
    15
// Platform independent USB client controller layer (PIL):
sl@0
    16
// USB descriptor handling and management.
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
#include "usbcdesc.h"
sl@0
    21
#include "dtestusblogdev.h"
sl@0
    22
sl@0
    23
// --- TUsbcDescriptorBase
sl@0
    24
sl@0
    25
TUsbcDescriptorBase::TUsbcDescriptorBase()
sl@0
    26
	:
sl@0
    27
#ifdef USB_SUPPORTS_SET_DESCRIPTOR_REQUEST
sl@0
    28
	iIndex(0),
sl@0
    29
#endif
sl@0
    30
	iBufPtr(NULL, 0)
sl@0
    31
  	{
sl@0
    32
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorBase::TUsbcDescriptorBase()")));
sl@0
    33
	}
sl@0
    34
sl@0
    35
sl@0
    36
TUsbcDescriptorBase::~TUsbcDescriptorBase()
sl@0
    37
	{
sl@0
    38
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorBase::~TUsbcDescriptorBase()")));
sl@0
    39
	}
sl@0
    40
sl@0
    41
sl@0
    42
void TUsbcDescriptorBase::SetByte(TUint aPosition, TUint8 aValue)
sl@0
    43
	{
sl@0
    44
	iBufPtr[aPosition] = aValue;
sl@0
    45
	}
sl@0
    46
sl@0
    47
sl@0
    48
void TUsbcDescriptorBase::SetWord(TUint aPosition, TUint16 aValue)
sl@0
    49
	{
sl@0
    50
	*reinterpret_cast<TUint16*>(&iBufPtr[aPosition]) = SWAP_BYTES_16(aValue);
sl@0
    51
	}
sl@0
    52
sl@0
    53
sl@0
    54
TUint8 TUsbcDescriptorBase::Byte(TUint aPosition) const
sl@0
    55
	{
sl@0
    56
	return iBufPtr[aPosition];
sl@0
    57
	}
sl@0
    58
sl@0
    59
sl@0
    60
TUint16 TUsbcDescriptorBase::Word(TUint aPosition) const
sl@0
    61
	{
sl@0
    62
	return SWAP_BYTES_16(*reinterpret_cast<const TUint16*>(&iBufPtr[aPosition]));
sl@0
    63
	}
sl@0
    64
sl@0
    65
sl@0
    66
void TUsbcDescriptorBase::GetDescriptorData(TDes8& aBuffer) const
sl@0
    67
	{
sl@0
    68
	aBuffer = iBufPtr;
sl@0
    69
	}
sl@0
    70
sl@0
    71
sl@0
    72
TInt TUsbcDescriptorBase::GetDescriptorData(TUint8* aBuffer) const
sl@0
    73
	{
sl@0
    74
	__MEMCPY(aBuffer, iBufPtr.Ptr(), Size());
sl@0
    75
	return Size();
sl@0
    76
	}
sl@0
    77
sl@0
    78
sl@0
    79
TInt TUsbcDescriptorBase::GetDescriptorData(TUint8* aBuffer, TInt aMaxSize) const
sl@0
    80
	{
sl@0
    81
	if (aMaxSize < Size())
sl@0
    82
		{
sl@0
    83
		// No use to copy only half a descriptor
sl@0
    84
		return 0;
sl@0
    85
		}
sl@0
    86
	return GetDescriptorData(aBuffer);
sl@0
    87
	}
sl@0
    88
sl@0
    89
sl@0
    90
const TDes8& TUsbcDescriptorBase::DescriptorData() const
sl@0
    91
	{
sl@0
    92
	return iBufPtr;
sl@0
    93
	}
sl@0
    94
sl@0
    95
sl@0
    96
TDes8& TUsbcDescriptorBase::DescriptorData()
sl@0
    97
	{
sl@0
    98
	return iBufPtr;
sl@0
    99
	}
sl@0
   100
sl@0
   101
sl@0
   102
TInt TUsbcDescriptorBase::Size() const
sl@0
   103
	{
sl@0
   104
	return iBufPtr.Size();
sl@0
   105
	}
sl@0
   106
sl@0
   107
sl@0
   108
TUint8 TUsbcDescriptorBase::Type() const
sl@0
   109
	{
sl@0
   110
	return iBufPtr[1];
sl@0
   111
	}
sl@0
   112
sl@0
   113
sl@0
   114
void TUsbcDescriptorBase::SetBufferPointer(const TDesC8& aDes)
sl@0
   115
	{
sl@0
   116
	iBufPtr.Set(const_cast<TUint8*>(aDes.Ptr()), aDes.Size(), aDes.Size());
sl@0
   117
	}
sl@0
   118
sl@0
   119
sl@0
   120
// --- TUsbcDeviceDescriptor
sl@0
   121
sl@0
   122
TUsbcDeviceDescriptor::TUsbcDeviceDescriptor()
sl@0
   123
	: iBuf()
sl@0
   124
  	{
sl@0
   125
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDeviceDescriptor::TUsbcDeviceDescriptor()")));
sl@0
   126
	}
sl@0
   127
sl@0
   128
sl@0
   129
TUsbcDeviceDescriptor* TUsbcDeviceDescriptor::New(TUint8 aDeviceClass, TUint8 aDeviceSubClass,
sl@0
   130
												  TUint8 aDeviceProtocol, TUint8 aMaxPacketSize0,
sl@0
   131
												  TUint16 aVendorId, TUint16 aProductId,
sl@0
   132
												  TUint16 aDeviceRelease, TUint8 aNumConfigurations)
sl@0
   133
	{
sl@0
   134
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDeviceDescriptor::New()")));
sl@0
   135
	TUsbcDeviceDescriptor* self = new TUsbcDeviceDescriptor();
sl@0
   136
	if (self)
sl@0
   137
		{
sl@0
   138
		if (self->Construct(aDeviceClass, aDeviceSubClass, aDeviceProtocol, aMaxPacketSize0, aVendorId,
sl@0
   139
							aProductId, aDeviceRelease, aNumConfigurations) != KErrNone)
sl@0
   140
			{
sl@0
   141
			delete self;
sl@0
   142
			return NULL;
sl@0
   143
			}
sl@0
   144
		}
sl@0
   145
	return self;
sl@0
   146
	}
sl@0
   147
sl@0
   148
sl@0
   149
TInt TUsbcDeviceDescriptor::Construct(TUint8 aDeviceClass, TUint8 aDeviceSubClass, TUint8 aDeviceProtocol,
sl@0
   150
									  TUint8 aMaxPacketSize0, TUint16 aVendorId, TUint16 aProductId,
sl@0
   151
									  TUint16 aDeviceRelease, TUint8 aNumConfigurations)
sl@0
   152
	{
sl@0
   153
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDeviceDescriptor::Construct()")));
sl@0
   154
	iBuf.SetMax();
sl@0
   155
	SetBufferPointer(iBuf);
sl@0
   156
	iBuf[0] = (TUint8)iBuf.Size();									// bLength
sl@0
   157
	iBuf[1] = KUsbDescType_Device;							// bDescriptorType
sl@0
   158
 	SetWord(2, KUsbcUsbVersion);							// bcdUSB
sl@0
   159
	iBuf[4] = aDeviceClass;									// bDeviceClass
sl@0
   160
	iBuf[5] = aDeviceSubClass;								// bDeviceSubClass
sl@0
   161
	iBuf[6] = aDeviceProtocol;								// bDeviceProtocol
sl@0
   162
	iBuf[7] = aMaxPacketSize0;								// bMaxPacketSize0
sl@0
   163
	SetWord(8, aVendorId);									// idVendor
sl@0
   164
	SetWord(10, aProductId);								// idProduct
sl@0
   165
	SetWord(12, aDeviceRelease);							// bcdDevice
sl@0
   166
	iBuf[14] = 0;											// iManufacturer
sl@0
   167
	iBuf[15] = 0;											// iProduct
sl@0
   168
	iBuf[16] = 0;											// iSerialNumber
sl@0
   169
	iBuf[17] = aNumConfigurations;							// bNumConfigurations
sl@0
   170
	return KErrNone;
sl@0
   171
	}
sl@0
   172
sl@0
   173
// --- TUsbcConfigDescriptor
sl@0
   174
sl@0
   175
TUsbcConfigDescriptor::TUsbcConfigDescriptor()
sl@0
   176
	: iBuf()
sl@0
   177
  	{
sl@0
   178
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcConfigDescriptor::TUsbcConfigDescriptor()")));
sl@0
   179
	}
sl@0
   180
sl@0
   181
sl@0
   182
TUsbcConfigDescriptor* TUsbcConfigDescriptor::New(TUint8 aConfigurationValue, TBool aSelfPowered,
sl@0
   183
												  TBool aRemoteWakeup, TUint8 aMaxPower)
sl@0
   184
	{
sl@0
   185
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcConfigDescriptor::New()")));
sl@0
   186
	TUsbcConfigDescriptor* self = new TUsbcConfigDescriptor();
sl@0
   187
	if (self)
sl@0
   188
		{
sl@0
   189
		if (self->Construct(aConfigurationValue, aSelfPowered, aRemoteWakeup, aMaxPower) != KErrNone)
sl@0
   190
			{
sl@0
   191
			delete self;
sl@0
   192
			return NULL;
sl@0
   193
			}
sl@0
   194
		}
sl@0
   195
	return self;
sl@0
   196
	}
sl@0
   197
sl@0
   198
sl@0
   199
TInt TUsbcConfigDescriptor::Construct(TUint8 aConfigurationValue, TBool aSelfPowered,
sl@0
   200
									   TBool aRemoteWakeup, TUint8 aMaxPower)
sl@0
   201
	{
sl@0
   202
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcConfigDescriptor::Construct()")));
sl@0
   203
	iBuf.SetMax();
sl@0
   204
	SetBufferPointer(iBuf);
sl@0
   205
	iBuf[0] = (TUint8)iBuf.Size();									// bLength
sl@0
   206
	iBuf[1] = KUsbDescType_Config;							// bDescriptorType
sl@0
   207
	SetWord(2, KUsbDescSize_Config);						// wTotalLength
sl@0
   208
	iBuf[4] = 0;											// bNumInterfaces
sl@0
   209
	iBuf[5] = aConfigurationValue;							// bConfigurationValue
sl@0
   210
	iBuf[6] = 0;											// iConfiguration
sl@0
   211
	iBuf[7] = (TUint8)(0x80 | (aSelfPowered ? 0x40 : 0) | (aRemoteWakeup ? 0x20 : 0)); // bmAttributes (bit 7 always 1)
sl@0
   212
	iBuf[8] = (TUint8)(aMaxPower / 2);								// MaxPower (2mA units!)
sl@0
   213
	return KErrNone;
sl@0
   214
	}
sl@0
   215
sl@0
   216
sl@0
   217
// --- TUsbcInterfaceDescriptor
sl@0
   218
sl@0
   219
TUsbcInterfaceDescriptor::TUsbcInterfaceDescriptor()
sl@0
   220
	: iBuf()
sl@0
   221
  	{
sl@0
   222
 	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcInterfaceDescriptor::TUsbcInterfaceDescriptor()")));
sl@0
   223
	}
sl@0
   224
sl@0
   225
sl@0
   226
TUsbcInterfaceDescriptor* TUsbcInterfaceDescriptor::New(TUint8 aInterfaceNumber, TUint8 aAlternateSetting,
sl@0
   227
														TInt aNumEndpoints, const TUsbcClassInfo& aClassInfo)
sl@0
   228
	{
sl@0
   229
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcInterfaceDescriptor::New()")));
sl@0
   230
	TUsbcInterfaceDescriptor* self = new TUsbcInterfaceDescriptor();
sl@0
   231
	if (self)
sl@0
   232
		{
sl@0
   233
		if (self->Construct(aInterfaceNumber, aAlternateSetting, aNumEndpoints, aClassInfo) != KErrNone)
sl@0
   234
			{
sl@0
   235
			delete self;
sl@0
   236
			return NULL;
sl@0
   237
			}
sl@0
   238
		}
sl@0
   239
	return self;
sl@0
   240
	}
sl@0
   241
sl@0
   242
sl@0
   243
TInt TUsbcInterfaceDescriptor::Construct(TUint8 aInterfaceNumber, TUint8 aAlternateSetting,
sl@0
   244
										 TInt aNumEndpoints, const TUsbcClassInfo& aClassInfo)
sl@0
   245
	{
sl@0
   246
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcInterfaceDescriptor::Construct()")));
sl@0
   247
	iBuf.SetMax();
sl@0
   248
	SetBufferPointer(iBuf);
sl@0
   249
	iBuf[0] = (TUint8)iBuf.Size();									// bLength
sl@0
   250
	iBuf[1] = KUsbDescType_Interface;						// bDescriptorType
sl@0
   251
	iBuf[2] = aInterfaceNumber;								// bInterfaceNumber
sl@0
   252
	iBuf[3] = aAlternateSetting;							// bAlternateSetting
sl@0
   253
	iBuf[4] = (TUint8)aNumEndpoints;								// bNumEndpoints
sl@0
   254
	iBuf[5] = (TUint8)aClassInfo.iClassNum;							// bInterfaceClass
sl@0
   255
	iBuf[6] = (TUint8)aClassInfo.iSubClassNum;						// bInterfaceSubClass
sl@0
   256
	iBuf[7] = (TUint8)aClassInfo.iProtocolNum;						// bInterfaceProtocol
sl@0
   257
	iBuf[8] = 0;											// iInterface
sl@0
   258
	return KErrNone;
sl@0
   259
	}
sl@0
   260
sl@0
   261
sl@0
   262
// --- TUsbcEndpointDescriptor
sl@0
   263
sl@0
   264
TUsbcEndpointDescriptor::TUsbcEndpointDescriptor()
sl@0
   265
	: iBuf()
sl@0
   266
  	{
sl@0
   267
 	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcEndpointDescriptor::TUsbcEndpointDescriptor()")));
sl@0
   268
	}
sl@0
   269
sl@0
   270
sl@0
   271
TUsbcEndpointDescriptor* TUsbcEndpointDescriptor::New(TUint8 aEndpointAddress,
sl@0
   272
													  const TUsbcEndpointInfo& aEpInfo)
sl@0
   273
	{
sl@0
   274
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcEndpointDescriptor::New()")));
sl@0
   275
	TUsbcEndpointDescriptor* self = new TUsbcEndpointDescriptor();
sl@0
   276
	if (self)
sl@0
   277
		{
sl@0
   278
		if (self->Construct(aEndpointAddress, aEpInfo) != KErrNone)
sl@0
   279
			{
sl@0
   280
			delete self;
sl@0
   281
			return NULL;
sl@0
   282
			}
sl@0
   283
		}
sl@0
   284
	return self;
sl@0
   285
	}
sl@0
   286
sl@0
   287
sl@0
   288
TInt TUsbcEndpointDescriptor::Construct(TUint8 aEndpointAddress, const TUsbcEndpointInfo& aEpInfo)
sl@0
   289
	{
sl@0
   290
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcEndpointDescriptor::Construct()")));
sl@0
   291
	iBuf.SetMax();
sl@0
   292
	SetBufferPointer(iBuf);
sl@0
   293
	iBuf[0] = (TUint8)iBuf.Size();									// bLength
sl@0
   294
	iBuf[1] = KUsbDescType_Endpoint;						// bDescriptorType
sl@0
   295
 	iBuf[2] = aEndpointAddress;								// bEndpointAddress
sl@0
   296
	iBuf[3] = (TUint8)EpTypeMask2Value(aEpInfo.iType);				// bmAttributes
sl@0
   297
	SetWord(4, (TUint8)aEpInfo.iSize);								// wMaxPacketSize
sl@0
   298
	iBuf[6] = (TUint8)aEpInfo.iInterval;							// bInterval
sl@0
   299
	return KErrNone;
sl@0
   300
	}
sl@0
   301
sl@0
   302
sl@0
   303
// --- TUsbcAudioEndpointDescriptor
sl@0
   304
sl@0
   305
TUsbcAudioEndpointDescriptor::TUsbcAudioEndpointDescriptor()
sl@0
   306
	: iBuf()
sl@0
   307
  	{
sl@0
   308
 	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcAudioEndpointDescriptor::TUsbcAudioEndpointDescriptor()")));
sl@0
   309
	}
sl@0
   310
sl@0
   311
sl@0
   312
TUsbcAudioEndpointDescriptor* TUsbcAudioEndpointDescriptor::New(TUint8 aEndpointAddress,
sl@0
   313
																const TUsbcEndpointInfo& aEpInfo)
sl@0
   314
	{
sl@0
   315
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcAudioEndpointDescriptor::New()")));
sl@0
   316
	TUsbcAudioEndpointDescriptor* self = new TUsbcAudioEndpointDescriptor();
sl@0
   317
	if (self)
sl@0
   318
		{
sl@0
   319
		if (self->Construct(aEndpointAddress, aEpInfo) != KErrNone)
sl@0
   320
			{
sl@0
   321
			delete self;
sl@0
   322
			return NULL;
sl@0
   323
			}
sl@0
   324
		}
sl@0
   325
	return self;
sl@0
   326
	}
sl@0
   327
sl@0
   328
sl@0
   329
TInt TUsbcAudioEndpointDescriptor::Construct(TUint8 aEndpointAddress, const TUsbcEndpointInfo& aEpInfo)
sl@0
   330
	{
sl@0
   331
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcAudioEndpointDescriptor::Construct()")));
sl@0
   332
	iBuf.SetMax();
sl@0
   333
	SetBufferPointer(iBuf);
sl@0
   334
	iBuf[0] = (TUint8)iBuf.Size();									// bLength
sl@0
   335
	iBuf[1] = KUsbDescType_Endpoint;						// bDescriptorType
sl@0
   336
 	iBuf[2] = aEndpointAddress;								// bEndpointAddress
sl@0
   337
	iBuf[3] = (TUint8)EpTypeMask2Value(aEpInfo.iType);				// bmAttributes
sl@0
   338
	SetWord(4, (TUint8)aEpInfo.iSize);								// wMaxPacketSize
sl@0
   339
	iBuf[6] = (TUint8)aEpInfo.iInterval;							// bInterval
sl@0
   340
	iBuf[7] = 0;
sl@0
   341
	iBuf[8] = 0;
sl@0
   342
	return KErrNone;
sl@0
   343
	}
sl@0
   344
sl@0
   345
sl@0
   346
// --- TUsbcClassSpecificDescriptor
sl@0
   347
sl@0
   348
TUsbcClassSpecificDescriptor::TUsbcClassSpecificDescriptor()
sl@0
   349
	: iBuf(NULL)
sl@0
   350
  	{
sl@0
   351
 	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcClassSpecificDescriptor::TUsbcClassSpecificDescriptor()")));
sl@0
   352
	}
sl@0
   353
sl@0
   354
sl@0
   355
TUsbcClassSpecificDescriptor::~TUsbcClassSpecificDescriptor()
sl@0
   356
	{
sl@0
   357
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcClassSpecificDescriptor::~TUsbcClassSpecificDescriptor()")));
sl@0
   358
	delete iBuf;
sl@0
   359
	}
sl@0
   360
sl@0
   361
sl@0
   362
TUsbcClassSpecificDescriptor* TUsbcClassSpecificDescriptor::New(TUint8 aType, TInt aSize)
sl@0
   363
	{
sl@0
   364
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcClassSpecificDescriptor::New()")));
sl@0
   365
	TUsbcClassSpecificDescriptor* self = new TUsbcClassSpecificDescriptor();
sl@0
   366
	if (self)
sl@0
   367
		{
sl@0
   368
		if (self->Construct(aType, aSize) != KErrNone)
sl@0
   369
			{
sl@0
   370
			delete self;
sl@0
   371
			return NULL;
sl@0
   372
			}
sl@0
   373
		}
sl@0
   374
	return self;
sl@0
   375
	}
sl@0
   376
sl@0
   377
sl@0
   378
TInt TUsbcClassSpecificDescriptor::Construct(TUint8 aType, TInt aSize)
sl@0
   379
	{
sl@0
   380
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcClassSpecificDescriptor::Construct()")));
sl@0
   381
	__NEWPLATBUF(iBuf, aSize);
sl@0
   382
	if (!iBuf)
sl@0
   383
		{
sl@0
   384
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Allocation of CS desc buffer failed")));
sl@0
   385
		return KErrNoMemory;
sl@0
   386
		}
sl@0
   387
	SetBufferPointer(*iBuf);
sl@0
   388
	SetByte(1, aType);										// bDescriptorType
sl@0
   389
	return KErrNone;
sl@0
   390
	}
sl@0
   391
sl@0
   392
sl@0
   393
// --- TUsbcStringDescriptorBase
sl@0
   394
sl@0
   395
TUsbcStringDescriptorBase::TUsbcStringDescriptorBase()
sl@0
   396
	: /*iIndex(0),*/ iSBuf(0), iBufPtr(NULL, 0)
sl@0
   397
	{
sl@0
   398
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcStringDescriptorBase::TUsbcStringDescriptorBase()")));
sl@0
   399
	}
sl@0
   400
sl@0
   401
sl@0
   402
TUsbcStringDescriptorBase::~TUsbcStringDescriptorBase()
sl@0
   403
	{
sl@0
   404
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcStringDescriptorBase::~TUsbcStringDescriptorBase()")));
sl@0
   405
	}
sl@0
   406
sl@0
   407
sl@0
   408
TUint16 TUsbcStringDescriptorBase::Word(TUint aPosition) const
sl@0
   409
	{
sl@0
   410
	if (aPosition <= 1)
sl@0
   411
		{
sl@0
   412
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("TUsbcStringDescriptorBase::Word: Error: Word(%d) in string descriptor!"), aPosition));
sl@0
   413
		return 0;
sl@0
   414
		}
sl@0
   415
	else
sl@0
   416
		{
sl@0
   417
		// since iBufPtr[0] is actually string descriptor byte index 2,
sl@0
   418
		// we have to subtract 2 from the absolute position.
sl@0
   419
		return SWAP_BYTES_16(*reinterpret_cast<const TUint16*>(&iBufPtr[aPosition - 2]));
sl@0
   420
		}
sl@0
   421
	}
sl@0
   422
sl@0
   423
sl@0
   424
TInt TUsbcStringDescriptorBase::GetDescriptorData(TUint8* aBuffer) const
sl@0
   425
	{
sl@0
   426
	aBuffer[0] = iSBuf[0];
sl@0
   427
	aBuffer[1] = iSBuf[1];
sl@0
   428
	__MEMCPY(&aBuffer[2], iBufPtr.Ptr(), iBufPtr.Size());
sl@0
   429
	return Size();
sl@0
   430
	}
sl@0
   431
sl@0
   432
sl@0
   433
TInt TUsbcStringDescriptorBase::GetDescriptorData(TUint8* aBuffer, TInt aMaxSize) const
sl@0
   434
	{
sl@0
   435
	if (aMaxSize < Size())
sl@0
   436
		{
sl@0
   437
		// No use to copy only half a string
sl@0
   438
		return 0;
sl@0
   439
		}
sl@0
   440
	return GetDescriptorData(aBuffer);
sl@0
   441
	}
sl@0
   442
sl@0
   443
sl@0
   444
const TDes8& TUsbcStringDescriptorBase::StringData() const
sl@0
   445
	{
sl@0
   446
	return iBufPtr;
sl@0
   447
	}
sl@0
   448
sl@0
   449
sl@0
   450
TDes8& TUsbcStringDescriptorBase::StringData()
sl@0
   451
	{
sl@0
   452
	return iBufPtr;
sl@0
   453
	}
sl@0
   454
sl@0
   455
sl@0
   456
TInt TUsbcStringDescriptorBase::Size() const
sl@0
   457
	{
sl@0
   458
	return iSBuf[0];
sl@0
   459
	}
sl@0
   460
sl@0
   461
sl@0
   462
void TUsbcStringDescriptorBase::SetBufferPointer(const TDesC8& aDes)
sl@0
   463
	{
sl@0
   464
	iBufPtr.Set(const_cast<TUint8*>(aDes.Ptr()), aDes.Size(), aDes.Size());
sl@0
   465
	}
sl@0
   466
sl@0
   467
sl@0
   468
// --- TUsbcStringDescriptor
sl@0
   469
sl@0
   470
TUsbcStringDescriptor::TUsbcStringDescriptor()
sl@0
   471
	: iBuf(NULL)
sl@0
   472
	{
sl@0
   473
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcStringDescriptor::TUsbcStringDescriptor()")));
sl@0
   474
	}
sl@0
   475
sl@0
   476
sl@0
   477
TUsbcStringDescriptor::~TUsbcStringDescriptor()
sl@0
   478
	{
sl@0
   479
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcStringDescriptor::~TUsbcStringDescriptor()")));
sl@0
   480
	delete iBuf;
sl@0
   481
	}
sl@0
   482
sl@0
   483
sl@0
   484
TUsbcStringDescriptor* TUsbcStringDescriptor::New(const TDesC8& aString)
sl@0
   485
	{
sl@0
   486
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcStringDescriptor::New")));
sl@0
   487
	TUsbcStringDescriptor* self = new TUsbcStringDescriptor();
sl@0
   488
	if (self)
sl@0
   489
		{
sl@0
   490
		if (self->Construct(aString) != KErrNone)
sl@0
   491
			{
sl@0
   492
			delete self;
sl@0
   493
			return NULL;
sl@0
   494
			}
sl@0
   495
		}
sl@0
   496
	return self;
sl@0
   497
	}
sl@0
   498
sl@0
   499
sl@0
   500
TInt TUsbcStringDescriptor::Construct(const TDesC8& aString)
sl@0
   501
	{
sl@0
   502
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcStringDescriptor::Construct")));
sl@0
   503
	__NEWPLATBUF(iBuf, aString.Size());
sl@0
   504
	if (!iBuf)
sl@0
   505
		{
sl@0
   506
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Allocation of string buffer failed")));
sl@0
   507
		return KErrNoMemory;
sl@0
   508
		}
sl@0
   509
	SetBufferPointer(*iBuf);
sl@0
   510
	iBufPtr.Copy(aString);
sl@0
   511
	iSBuf.SetMax();
sl@0
   512
	iSBuf[0] = (TUint8)(iBuf->Size() + 2); // Bytes
sl@0
   513
	iSBuf[1] = KUsbDescType_String;
sl@0
   514
	return KErrNone;
sl@0
   515
	}
sl@0
   516
sl@0
   517
sl@0
   518
// --- TUsbcLangIdDescriptor
sl@0
   519
sl@0
   520
TUsbcLangIdDescriptor::TUsbcLangIdDescriptor()
sl@0
   521
	: iBuf(NULL)
sl@0
   522
	{
sl@0
   523
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcLangIdDescriptor::TUsbcLangIdDescriptor()")));
sl@0
   524
	}
sl@0
   525
sl@0
   526
sl@0
   527
TUsbcLangIdDescriptor::~TUsbcLangIdDescriptor()
sl@0
   528
	{
sl@0
   529
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcLangIdDescriptor::~TUsbcLangIdDescriptor()")));
sl@0
   530
	}
sl@0
   531
sl@0
   532
sl@0
   533
TUsbcLangIdDescriptor* TUsbcLangIdDescriptor::New(TUint16 aLangId)
sl@0
   534
	{
sl@0
   535
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcLangIdDescriptor::New")));
sl@0
   536
	TUsbcLangIdDescriptor* self = new TUsbcLangIdDescriptor();
sl@0
   537
	if (self)
sl@0
   538
		{
sl@0
   539
		if (self->Construct(aLangId) != KErrNone)
sl@0
   540
			{
sl@0
   541
			delete self;
sl@0
   542
			return NULL;
sl@0
   543
			}
sl@0
   544
		}
sl@0
   545
	return self;
sl@0
   546
	}
sl@0
   547
sl@0
   548
sl@0
   549
TInt TUsbcLangIdDescriptor::Construct(TUint16 aLangId)
sl@0
   550
	{
sl@0
   551
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcLangIdDescriptor::Construct")));
sl@0
   552
	iBuf.SetMax();
sl@0
   553
	SetBufferPointer(iBuf);
sl@0
   554
	iBufPtr[0] = LowByte(SWAP_BYTES_16(aLangId));			// Language ID value
sl@0
   555
	iBufPtr[1] = HighByte(SWAP_BYTES_16(aLangId));
sl@0
   556
	iSBuf.SetMax();
sl@0
   557
	iSBuf[0] = (TUint8)(iBuf.Size() + 2);								// Bytes
sl@0
   558
	iSBuf[1] = KUsbDescType_String;
sl@0
   559
	return KErrNone;
sl@0
   560
	}
sl@0
   561
sl@0
   562
sl@0
   563
// --- TUsbcDescriptorPool
sl@0
   564
sl@0
   565
TUsbcDescriptorPool::TUsbcDescriptorPool(TUint8* aEp0_TxBuf)
sl@0
   566
//
sl@0
   567
//  The constructor for this class.
sl@0
   568
//
sl@0
   569
	: iDescriptors(4), iStrings(4),	iIfcIdx(0), iEp0_TxBuf(aEp0_TxBuf) // 4 = granularity
sl@0
   570
	{
sl@0
   571
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::TUsbcDescriptorPool()")));
sl@0
   572
	}
sl@0
   573
sl@0
   574
sl@0
   575
TUsbcDescriptorPool::~TUsbcDescriptorPool()
sl@0
   576
	{
sl@0
   577
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::~TUsbcDescriptorPool()")));
sl@0
   578
	// The destructor of each <class T> object is called before the objects themselves are destroyed.
sl@0
   579
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  iDescriptors.Count(): %d"), iDescriptors.Count()));
sl@0
   580
	iDescriptors.ResetAndDestroy();
sl@0
   581
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  iStrings.Count(): %d"), iStrings.Count()));
sl@0
   582
	iStrings.ResetAndDestroy();
sl@0
   583
	}
sl@0
   584
sl@0
   585
sl@0
   586
TInt TUsbcDescriptorPool::Init(TUsbcDeviceDescriptor* aDeviceDesc, TUsbcConfigDescriptor* aConfigDesc,
sl@0
   587
							   TUsbcLangIdDescriptor* aLangId, TUsbcStringDescriptor* aManufacturer,
sl@0
   588
							   TUsbcStringDescriptor* aProduct, TUsbcStringDescriptor* aSerialNum,
sl@0
   589
							   TUsbcStringDescriptor* aConfig)
sl@0
   590
	{
sl@0
   591
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::Init()")));
sl@0
   592
	iDescriptors.Insert(aDeviceDesc, 0);
sl@0
   593
	iDescriptors.Insert(aConfigDesc, 1);
sl@0
   594
	if (!aLangId || !aManufacturer || !aProduct || !aSerialNum || !aConfig)
sl@0
   595
		{
sl@0
   596
		// USB spec p. 202 says: "A USB device may omit all string descriptors."
sl@0
   597
		// So, either ALL string descriptors are supplied or none at all.
sl@0
   598
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: No string descriptor(s)")));
sl@0
   599
		return KErrArgument;
sl@0
   600
		}
sl@0
   601
	iStrings.Insert(aLangId, 0);
sl@0
   602
	iStrings.Insert(aManufacturer, 1);
sl@0
   603
	iStrings.Insert(aProduct, 2);
sl@0
   604
	iStrings.Insert(aSerialNum, 3);
sl@0
   605
	iStrings.Insert(aConfig, 4);
sl@0
   606
	// set string indices
sl@0
   607
	iDescriptors[0]->SetByte(14, 1);						// Device.iManufacturer
sl@0
   608
	iDescriptors[0]->SetByte(15, 2);						// Device.iProduct
sl@0
   609
	iDescriptors[0]->SetByte(16, 3);						// Device.iSerialNumber
sl@0
   610
	iDescriptors[1]->SetByte( 6, 4);						// Config.iConfiguration
sl@0
   611
	return KErrNone;
sl@0
   612
	}
sl@0
   613
sl@0
   614
sl@0
   615
TInt TUsbcDescriptorPool::FindDescriptor(TUint8 aType, TUint8 aIndex, TUint16 aLangid, TInt& aSize) const
sl@0
   616
	{
sl@0
   617
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::FindDescriptor()")));
sl@0
   618
	TInt result = KErrGeneral;
sl@0
   619
sl@0
   620
	switch(aType)
sl@0
   621
		{
sl@0
   622
	case KUsbDescType_Device:
sl@0
   623
		if (aLangid != 0)
sl@0
   624
			{
sl@0
   625
			__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: bad langid: 0x%04x"), aLangid));
sl@0
   626
			result = KErrGeneral;							// bad langid
sl@0
   627
			}
sl@0
   628
		else if (aIndex > 0)
sl@0
   629
			{
sl@0
   630
			__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: bad device index: %d"), aIndex));
sl@0
   631
			result = KErrGeneral;							// we have only one device
sl@0
   632
			}
sl@0
   633
		else
sl@0
   634
			{
sl@0
   635
			aSize = GetDeviceDescriptor();
sl@0
   636
			result = KErrNone;
sl@0
   637
			}
sl@0
   638
		break;
sl@0
   639
	case KUsbDescType_Config:
sl@0
   640
		if (aLangid != 0)
sl@0
   641
			{
sl@0
   642
			__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: bad langid: 0x%04x"), aLangid));
sl@0
   643
			result = KErrGeneral;							// bad langid
sl@0
   644
			}
sl@0
   645
		else if (aIndex > 0)
sl@0
   646
			{
sl@0
   647
			__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: bad config index: %d"), aIndex));
sl@0
   648
			result = KErrGeneral;							// we have only one configuration
sl@0
   649
			}
sl@0
   650
		else
sl@0
   651
			{
sl@0
   652
			aSize = GetConfigDescriptor();
sl@0
   653
			result = KErrNone;
sl@0
   654
			}
sl@0
   655
		break;
sl@0
   656
	case KUsbDescType_String:
sl@0
   657
		if ((aLangid != 0) &&								// 0 addresses the LangId array
sl@0
   658
			(aLangid != iStrings[0]->Word(2)))				// we have just one (this) language
sl@0
   659
			{
sl@0
   660
			__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: bad langid (0x%04x requested, 0x%04x supported)"),
sl@0
   661
											  aLangid, iStrings[0]->Word(2)));
sl@0
   662
			result = KErrGeneral;							// bad langid
sl@0
   663
			}
sl@0
   664
		else if (aIndex >= iStrings.Count())
sl@0
   665
			{
sl@0
   666
			__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: bad string index: %d"), aIndex));
sl@0
   667
			result = KErrGeneral;
sl@0
   668
			}
sl@0
   669
		else
sl@0
   670
			{
sl@0
   671
			aSize = GetStringDescriptor(aIndex);
sl@0
   672
			result = KErrNone;
sl@0
   673
			}
sl@0
   674
		break;
sl@0
   675
	case KUsbDescType_CS_Interface:
sl@0
   676
		/* fall through */
sl@0
   677
	case KUsbDescType_CS_Endpoint:
sl@0
   678
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Warning: finding of class specific descriptors not supported")));
sl@0
   679
		break;
sl@0
   680
	default:
sl@0
   681
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: unknown descriptor type requested: %d"), aType));
sl@0
   682
		result = KErrGeneral;
sl@0
   683
		break;
sl@0
   684
		}
sl@0
   685
sl@0
   686
	return result;
sl@0
   687
	}
sl@0
   688
sl@0
   689
sl@0
   690
void TUsbcDescriptorPool::InsertDescriptor(TUsbcDescriptorBase* aDesc)
sl@0
   691
	{
sl@0
   692
	switch (aDesc->Type())
sl@0
   693
		{
sl@0
   694
	case KUsbDescType_Device:
sl@0
   695
		InsertDevDesc(aDesc);
sl@0
   696
		break;
sl@0
   697
	case KUsbDescType_Config:
sl@0
   698
		InsertConfigDesc(aDesc);
sl@0
   699
		break;
sl@0
   700
	case KUsbDescType_Interface:
sl@0
   701
		InsertIfcDesc(aDesc);
sl@0
   702
		break;
sl@0
   703
	case KUsbDescType_Endpoint:
sl@0
   704
		InsertEpDesc(aDesc);
sl@0
   705
		break;
sl@0
   706
	case KUsbDescType_CS_Interface:
sl@0
   707
		/* fall through */
sl@0
   708
	case KUsbDescType_CS_Endpoint:
sl@0
   709
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Warning: inserting class specific descriptors not supported")));
sl@0
   710
		break;
sl@0
   711
	default:
sl@0
   712
		__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::InsertDescriptor: Error: invalid type")));
sl@0
   713
		break;
sl@0
   714
		}
sl@0
   715
	}
sl@0
   716
sl@0
   717
sl@0
   718
void TUsbcDescriptorPool::SetIfcStringDescriptor(TUsbcStringDescriptor* aDesc, TInt aNumber, TInt aSetting)
sl@0
   719
	{
sl@0
   720
	TInt i = FindIfcDescriptor(aNumber, aSetting);
sl@0
   721
	if (i < 0)
sl@0
   722
		{
sl@0
   723
		__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::SetIfcStringDescriptor: error")));
sl@0
   724
		return;
sl@0
   725
		}
sl@0
   726
	// Set (append) string descriptor for specified interface
sl@0
   727
	iStrings.Append(aDesc);
sl@0
   728
	// Update this ifc descriptors' string index field
sl@0
   729
	const TInt str_idx = iStrings.Count() - 1;
sl@0
   730
	iDescriptors[i]->SetByte(8, (TUint8)str_idx);
sl@0
   731
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  String for ifc %d/%d (@ pos %d): \"%S\""), aNumber, aSetting, str_idx,
sl@0
   732
									&iStrings[str_idx]->StringData()));
sl@0
   733
	}
sl@0
   734
sl@0
   735
sl@0
   736
void TUsbcDescriptorPool::DeleteIfcDescriptor(TInt aNumber, TInt aSetting)
sl@0
   737
	{
sl@0
   738
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::DeleteIfcDescriptor(%d, %d)"), aNumber, aSetting));
sl@0
   739
	TInt i = FindIfcDescriptor(aNumber, aSetting);
sl@0
   740
	if (i < 0)
sl@0
   741
		{
sl@0
   742
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING(" > Error: descriptor not found")));
sl@0
   743
		return;
sl@0
   744
		}
sl@0
   745
	// Delete (if necessary) specified interface's string descriptor
sl@0
   746
	TInt si = iDescriptors[i]->Byte(8);
sl@0
   747
	if (si != 0)
sl@0
   748
		{
sl@0
   749
		DeleteString(si);
sl@0
   750
		}
sl@0
   751
	// Delete specified ifc setting + all its cs descriptors + all its endpoints + all their cs descriptors:
sl@0
   752
	// find position of the next interface descriptor: we need to delete everything in between
sl@0
   753
	const TInt count = iDescriptors.Count();
sl@0
   754
	TInt j = i, n = 1;
sl@0
   755
	while (++j < count && iDescriptors[j]->Type() != KUsbDescType_Interface)
sl@0
   756
		++n;
sl@0
   757
	DeleteDescriptors(i, n);
sl@0
   758
	// Update all the following interfaces' bInterfaceNumber field if required
sl@0
   759
	// (because these descriptors might have moved down by one position)
sl@0
   760
	UpdateIfcNumbers(aNumber);
sl@0
   761
	// Update (if necessary) all interfaces' string index field
sl@0
   762
	if (si != 0)
sl@0
   763
		{
sl@0
   764
		UpdateIfcStringIndexes(si);
sl@0
   765
		}
sl@0
   766
	iIfcIdx = 0;											// ifc index no longer valid
sl@0
   767
	}
sl@0
   768
sl@0
   769
sl@0
   770
// The TC in many of the following functions stands for 'ThreadCopy',
sl@0
   771
// because that's what's happening there.
sl@0
   772
sl@0
   773
TInt TUsbcDescriptorPool::GetDeviceDescriptorTC(DThread* aThread, TDes8& aBuffer) const
sl@0
   774
	{
sl@0
   775
	return __THREADWRITE(aThread, &aBuffer, iDescriptors[0]->DescriptorData());
sl@0
   776
	}
sl@0
   777
sl@0
   778
sl@0
   779
TInt TUsbcDescriptorPool::SetDeviceDescriptorTC(DThread* aThread, const TDes8& aBuffer)
sl@0
   780
	{
sl@0
   781
	TBuf8<KUsbDescSize_Device> device;
sl@0
   782
	TInt r = __THREADREAD(aThread, &aBuffer, device);
sl@0
   783
	if (r != KErrNone)
sl@0
   784
		{
sl@0
   785
		return r;
sl@0
   786
		}
sl@0
   787
	iDescriptors[0]->SetByte(2, device[2]);					// bcdUSB
sl@0
   788
	iDescriptors[0]->SetByte(3, device[3]);					// bcdUSB (part II)
sl@0
   789
	iDescriptors[0]->SetByte(4, device[4]);					// bDeviceClass
sl@0
   790
	iDescriptors[0]->SetByte(5, device[5]);					// bDeviceSubClass
sl@0
   791
	iDescriptors[0]->SetByte(6, device[6]);					// bDeviceProtocol
sl@0
   792
	iDescriptors[0]->SetByte(8, device[8]);					// idVendor
sl@0
   793
	iDescriptors[0]->SetByte(9, device[9]);					// idVendor (part II)
sl@0
   794
	iDescriptors[0]->SetByte(10, device[10]);				// idProduct
sl@0
   795
	iDescriptors[0]->SetByte(11, device[11]);				// idProduct (part II)
sl@0
   796
	iDescriptors[0]->SetByte(12, device[12]);				// bcdDevice
sl@0
   797
	iDescriptors[0]->SetByte(13, device[13]);				// bcdDevice (part II)
sl@0
   798
	return KErrNone;
sl@0
   799
	}
sl@0
   800
sl@0
   801
sl@0
   802
TInt TUsbcDescriptorPool::GetConfigurationDescriptorTC(DThread* aThread, TDes8& aBuffer) const
sl@0
   803
	{
sl@0
   804
	return __THREADWRITE(aThread, &aBuffer, iDescriptors[1]->DescriptorData());
sl@0
   805
	}
sl@0
   806
sl@0
   807
sl@0
   808
TInt TUsbcDescriptorPool::SetConfigurationDescriptorTC(DThread* aThread, const TDes8& aBuffer)
sl@0
   809
	{
sl@0
   810
	TBuf8<KUsbDescSize_Config> config;
sl@0
   811
	TInt r = __THREADREAD(aThread, &aBuffer, config);
sl@0
   812
	if (r != KErrNone)
sl@0
   813
		{
sl@0
   814
		return r;
sl@0
   815
		}
sl@0
   816
	iDescriptors[1]->SetByte(7, config[7]);					// bmAttributes
sl@0
   817
	iDescriptors[1]->SetByte(8, config[8]);					// bMaxPower
sl@0
   818
	return KErrNone;
sl@0
   819
	}
sl@0
   820
sl@0
   821
sl@0
   822
TInt TUsbcDescriptorPool::GetInterfaceDescriptorTC(DThread* aThread, TDes8& aBuffer,
sl@0
   823
												   TInt aInterface, TInt aSetting) const
sl@0
   824
	{
sl@0
   825
	TInt i = FindIfcDescriptor(aInterface, aSetting);
sl@0
   826
	if (i < 0)
sl@0
   827
		{
sl@0
   828
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such interface")));
sl@0
   829
		return KErrNotFound;
sl@0
   830
		}
sl@0
   831
	return __THREADWRITE(aThread, &aBuffer, iDescriptors[i]->DescriptorData());
sl@0
   832
	}
sl@0
   833
sl@0
   834
sl@0
   835
TInt TUsbcDescriptorPool::SetInterfaceDescriptor(const TDes8& aBuffer, TInt aInterface, TInt aSetting)
sl@0
   836
	{
sl@0
   837
	TInt i = FindIfcDescriptor(aInterface, aSetting);
sl@0
   838
	if (i < 0)
sl@0
   839
		{
sl@0
   840
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such interface")));
sl@0
   841
		return KErrNotFound;
sl@0
   842
		}
sl@0
   843
	iDescriptors[i]->SetByte(2, aBuffer[2]);				// bInterfaceNumber
sl@0
   844
	iDescriptors[i]->SetByte(5, aBuffer[5]);				// bInterfaceClass
sl@0
   845
	iDescriptors[i]->SetByte(6, aBuffer[6]);				// bInterfaceSubClass
sl@0
   846
	iDescriptors[i]->SetByte(7, aBuffer[7]);				// bInterfaceProtocol
sl@0
   847
	return KErrNone;
sl@0
   848
	}
sl@0
   849
sl@0
   850
sl@0
   851
TInt TUsbcDescriptorPool::GetEndpointDescriptorTC(DThread* aThread, TDes8& aBuffer,
sl@0
   852
												  TInt aInterface, TInt aSetting, TUint8 aEndpointAddress) const
sl@0
   853
	{
sl@0
   854
	TInt i = FindEpDescriptor(aInterface, aSetting, aEndpointAddress);
sl@0
   855
	if (i < 0)
sl@0
   856
		{
sl@0
   857
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint")));
sl@0
   858
		return KErrNotFound;
sl@0
   859
		}
sl@0
   860
	return __THREADWRITE(aThread, &aBuffer, iDescriptors[i]->DescriptorData());
sl@0
   861
	}
sl@0
   862
sl@0
   863
sl@0
   864
TInt TUsbcDescriptorPool::SetEndpointDescriptorTC(DThread* aThread, const TDes8& aBuffer,
sl@0
   865
												  TInt aInterface, TInt aSetting, TUint8 aEndpointAddress)
sl@0
   866
	{
sl@0
   867
	TInt i = FindEpDescriptor(aInterface, aSetting, aEndpointAddress);
sl@0
   868
	if (i < 0)
sl@0
   869
		{
sl@0
   870
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint")));
sl@0
   871
		return KErrNotFound;
sl@0
   872
		}
sl@0
   873
	TBuf8<KUsbDescSize_AudioEndpoint> ep;					// it could be an audio endpoint
sl@0
   874
	TInt r = __THREADREAD(aThread, &aBuffer, ep);
sl@0
   875
	if (r != KErrNone)
sl@0
   876
		{
sl@0
   877
		return r;
sl@0
   878
		}
sl@0
   879
	iDescriptors[i]->SetByte(3, ep[3]);						// bmAttributes
sl@0
   880
	iDescriptors[i]->SetByte(6, ep[6]);						// bInterval
sl@0
   881
	if (static_cast<TUint>(iDescriptors[i]->Size()) == KUsbDescSize_AudioEndpoint)
sl@0
   882
		{
sl@0
   883
		iDescriptors[i]->SetByte(7, ep[7]);					// bRefresh
sl@0
   884
		iDescriptors[i]->SetByte(8, ep[8]);					// bSynchAddress
sl@0
   885
		}
sl@0
   886
	return KErrNone;
sl@0
   887
	}
sl@0
   888
sl@0
   889
sl@0
   890
TInt TUsbcDescriptorPool::GetEndpointDescriptorSize(TInt aInterface, TInt aSetting, TUint8 aEndpointAddress,
sl@0
   891
													TInt& aSize) const
sl@0
   892
	{
sl@0
   893
	TInt i = FindEpDescriptor(aInterface, aSetting, aEndpointAddress);
sl@0
   894
	if (i < 0)
sl@0
   895
		{
sl@0
   896
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint")));
sl@0
   897
		return KErrNotFound;
sl@0
   898
		}
sl@0
   899
	aSize = iDescriptors[i]->Size();
sl@0
   900
	return KErrNone;
sl@0
   901
	}
sl@0
   902
sl@0
   903
sl@0
   904
TInt TUsbcDescriptorPool::GetCSInterfaceDescriptorTC(DThread* aThread, TDes8& aBuffer,
sl@0
   905
													 TInt aInterface, TInt aSetting) const
sl@0
   906
	{
sl@0
   907
	// first find the interface
sl@0
   908
	TInt i = FindIfcDescriptor(aInterface, aSetting);
sl@0
   909
	if (i < 0)
sl@0
   910
		{
sl@0
   911
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such interface")));
sl@0
   912
		return KErrNotFound;
sl@0
   913
		}
sl@0
   914
	TInt r = KErrNotFound;
sl@0
   915
	TInt offset = 0;
sl@0
   916
	const TInt count = iDescriptors.Count();
sl@0
   917
	while (++i < count && iDescriptors[i]->Type() == KUsbDescType_CS_Interface)
sl@0
   918
		{
sl@0
   919
		r = __THREADWRITEOFFSET(aThread, &aBuffer, iDescriptors[i]->DescriptorData(), offset);
sl@0
   920
		if (r != KErrNone)
sl@0
   921
			break;
sl@0
   922
		offset += iDescriptors[i]->Size();
sl@0
   923
		}
sl@0
   924
	return r;
sl@0
   925
	}
sl@0
   926
sl@0
   927
sl@0
   928
TInt TUsbcDescriptorPool::SetCSInterfaceDescriptorTC(DThread* aThread, const TDes8& aBuffer,
sl@0
   929
													 TInt aInterface, TInt aSetting, TInt aSize)
sl@0
   930
	{
sl@0
   931
	// first find the interface
sl@0
   932
	TInt i = FindIfcDescriptor(aInterface, aSetting);
sl@0
   933
	if (i < 0)
sl@0
   934
		{
sl@0
   935
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such interface")));
sl@0
   936
		return KErrNotFound;
sl@0
   937
		}
sl@0
   938
	// find a position where to insert the new class specific interface descriptor(s)
sl@0
   939
	const TInt count = iDescriptors.Count();
sl@0
   940
	while (++i < count && iDescriptors[i]->Type() == KUsbDescType_CS_Interface)
sl@0
   941
		;
sl@0
   942
	// create a new cs descriptor
sl@0
   943
	TUsbcClassSpecificDescriptor* desc = TUsbcClassSpecificDescriptor::New(KUsbDescType_CS_Interface, aSize);
sl@0
   944
	if (!desc)
sl@0
   945
		{
sl@0
   946
		return KErrNoMemory;
sl@0
   947
		}
sl@0
   948
	iDescriptors.Insert(desc, i);
sl@0
   949
	if (iDescriptors[1])
sl@0
   950
		{
sl@0
   951
		// if there's a config descriptor (and not a NULL pointer), we update its wTotalLength field
sl@0
   952
		iDescriptors[1]->SetWord(2, (TUint8)(iDescriptors[1]->Word(2) + aSize));
sl@0
   953
		}
sl@0
   954
	// copy contents from user side
sl@0
   955
	return __THREADREAD(aThread, &aBuffer, iDescriptors[i]->DescriptorData());
sl@0
   956
	}
sl@0
   957
sl@0
   958
sl@0
   959
TInt TUsbcDescriptorPool::GetCSInterfaceDescriptorSize(TInt aInterface, TInt aSetting, TInt& aSize) const
sl@0
   960
	{
sl@0
   961
	// first find the interface
sl@0
   962
	TInt i = FindIfcDescriptor(aInterface, aSetting);
sl@0
   963
	if (i < 0)
sl@0
   964
		{
sl@0
   965
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such interface")));
sl@0
   966
		return KErrNotFound;
sl@0
   967
		}
sl@0
   968
	TInt r = KErrNotFound;
sl@0
   969
	TInt size = 0;
sl@0
   970
	const TInt count = iDescriptors.Count();
sl@0
   971
	while (++i < count && iDescriptors[i]->Type() == KUsbDescType_CS_Interface)
sl@0
   972
		{
sl@0
   973
		size += iDescriptors[i]->Size();
sl@0
   974
		r = KErrNone;
sl@0
   975
		}
sl@0
   976
	if (r == KErrNone)
sl@0
   977
		aSize = size;
sl@0
   978
	return r;
sl@0
   979
	}
sl@0
   980
sl@0
   981
sl@0
   982
TInt TUsbcDescriptorPool::GetCSEndpointDescriptorTC(DThread* aThread, TDes8& aBuffer, TInt aInterface,
sl@0
   983
													TInt aSetting, TUint8 aEndpointAddress) const
sl@0
   984
	{
sl@0
   985
	// first find the endpoint
sl@0
   986
	TInt i = FindEpDescriptor(aInterface, aSetting, aEndpointAddress);
sl@0
   987
	if (i < 0)
sl@0
   988
		{
sl@0
   989
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint")));
sl@0
   990
		return KErrNotFound;
sl@0
   991
		}
sl@0
   992
	TInt r = KErrNotFound;
sl@0
   993
	TInt offset = 0;
sl@0
   994
	const TInt count = iDescriptors.Count();
sl@0
   995
	while (++i < count && iDescriptors[i]->Type() == KUsbDescType_CS_Endpoint)
sl@0
   996
		{
sl@0
   997
		__THREADWRITEOFFSET(aThread, &aBuffer, iDescriptors[i]->DescriptorData(), offset);
sl@0
   998
		if (r != KErrNone)
sl@0
   999
			break;
sl@0
  1000
		offset += iDescriptors[i]->Size();
sl@0
  1001
		}
sl@0
  1002
	return r;
sl@0
  1003
	}
sl@0
  1004
sl@0
  1005
sl@0
  1006
TInt TUsbcDescriptorPool::SetCSEndpointDescriptorTC(DThread* aThread, const TDes8& aBuffer, TInt aInterface,
sl@0
  1007
													TInt aSetting, TUint8 aEndpointAddress, TInt aSize)
sl@0
  1008
	{
sl@0
  1009
	// first find the endpoint
sl@0
  1010
	TInt i = FindEpDescriptor(aInterface, aSetting, aEndpointAddress);
sl@0
  1011
	if (i < 0)
sl@0
  1012
		{
sl@0
  1013
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint")));
sl@0
  1014
		return KErrNotFound;
sl@0
  1015
		}
sl@0
  1016
	// find a position where to insert the new class specific endpoint descriptor(s)
sl@0
  1017
	const TInt count = iDescriptors.Count();
sl@0
  1018
	while (++i < count && iDescriptors[i]->Type() == KUsbDescType_CS_Endpoint)
sl@0
  1019
		;
sl@0
  1020
	// create a new cs descriptor
sl@0
  1021
	TUsbcClassSpecificDescriptor* desc = TUsbcClassSpecificDescriptor::New(KUsbDescType_CS_Endpoint, aSize);
sl@0
  1022
	if (!desc)
sl@0
  1023
		{
sl@0
  1024
		return KErrNoMemory;
sl@0
  1025
		}
sl@0
  1026
	iDescriptors.Insert(desc, i);
sl@0
  1027
	if (iDescriptors[1])
sl@0
  1028
		{
sl@0
  1029
		// if there's a config descriptor (and not a NULL pointer), we update its wTotalLength field
sl@0
  1030
		iDescriptors[1]->SetWord(2, (TUint8)(iDescriptors[1]->Word(2) + aSize));
sl@0
  1031
		}
sl@0
  1032
	// copy contents from user side
sl@0
  1033
	return __THREADREAD(aThread, &aBuffer, iDescriptors[i]->DescriptorData());
sl@0
  1034
	}
sl@0
  1035
sl@0
  1036
sl@0
  1037
TInt TUsbcDescriptorPool::GetCSEndpointDescriptorSize(TInt aInterface, TInt aSetting,
sl@0
  1038
													  TUint8 aEndpointAddress, TInt& aSize) const
sl@0
  1039
	{
sl@0
  1040
	// first find the endpoint
sl@0
  1041
	TInt i = FindEpDescriptor(aInterface, aSetting, aEndpointAddress);
sl@0
  1042
	if (i < 0)
sl@0
  1043
		{
sl@0
  1044
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint")));
sl@0
  1045
		return KErrNotFound;
sl@0
  1046
		}
sl@0
  1047
	TInt r = KErrNotFound;
sl@0
  1048
	TInt size = 0;
sl@0
  1049
	const TInt count = iDescriptors.Count();
sl@0
  1050
	while (++i < count && iDescriptors[i]->Type() == KUsbDescType_CS_Endpoint)
sl@0
  1051
		{
sl@0
  1052
		size += iDescriptors[i]->Size();
sl@0
  1053
		r = KErrNone;
sl@0
  1054
		}
sl@0
  1055
	if (r == KErrNone)
sl@0
  1056
		aSize = size;
sl@0
  1057
	return r;
sl@0
  1058
	}
sl@0
  1059
sl@0
  1060
sl@0
  1061
TInt TUsbcDescriptorPool::GetManufacturerStringDescriptorTC(DThread* aThread, TDes8& aString) const
sl@0
  1062
	{
sl@0
  1063
	return GetDeviceStringDescriptorTC(aThread, aString, KUsbDescStringIndex_Manufact);
sl@0
  1064
	}
sl@0
  1065
sl@0
  1066
sl@0
  1067
TInt TUsbcDescriptorPool::SetManufacturerStringDescriptorTC(DThread* aThread, const TDes8& aString)
sl@0
  1068
	{
sl@0
  1069
	return SetDeviceStringDescriptorTC(aThread, aString, KUsbDescStringIndex_Manufact);
sl@0
  1070
	}
sl@0
  1071
sl@0
  1072
sl@0
  1073
TInt TUsbcDescriptorPool::GetProductStringDescriptorTC(DThread* aThread, TDes8& aString) const
sl@0
  1074
	{
sl@0
  1075
	return GetDeviceStringDescriptorTC(aThread, aString, KUsbDescStringIndex_Product);
sl@0
  1076
	}
sl@0
  1077
sl@0
  1078
sl@0
  1079
TInt TUsbcDescriptorPool::SetProductStringDescriptorTC(DThread* aThread, const TDes8& aString)
sl@0
  1080
	{
sl@0
  1081
	return SetDeviceStringDescriptorTC(aThread, aString, KUsbDescStringIndex_Product);
sl@0
  1082
	}
sl@0
  1083
sl@0
  1084
sl@0
  1085
TInt TUsbcDescriptorPool::GetSerialNumberStringDescriptorTC(DThread* aThread, TDes8& aString) const
sl@0
  1086
	{
sl@0
  1087
	return GetDeviceStringDescriptorTC(aThread, aString, KUsbDescStringIndex_Serial);
sl@0
  1088
	}
sl@0
  1089
sl@0
  1090
sl@0
  1091
TInt TUsbcDescriptorPool::SetSerialNumberStringDescriptorTC(DThread* aThread, const TDes8& aString)
sl@0
  1092
	{
sl@0
  1093
	return SetDeviceStringDescriptorTC(aThread, aString, KUsbDescStringIndex_Serial);
sl@0
  1094
	}
sl@0
  1095
sl@0
  1096
sl@0
  1097
TInt TUsbcDescriptorPool::GetConfigurationStringDescriptorTC(DThread* aThread, TDes8& aString) const
sl@0
  1098
	{
sl@0
  1099
	TBuf8<KUsbDescSize_Config> config_desc;
sl@0
  1100
	iDescriptors[1]->GetDescriptorData(config_desc);
sl@0
  1101
	const TInt str_idx = config_desc[KUsbDescStringIndex_Config];
sl@0
  1102
	if ((str_idx > 0) && iStrings[str_idx])
sl@0
  1103
		{
sl@0
  1104
		__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  String @ pos %d (conf $): \"%S\""),
sl@0
  1105
										str_idx, &iStrings[str_idx]->StringData()));
sl@0
  1106
		return __THREADWRITE(aThread, &aString, iStrings[str_idx]->StringData());
sl@0
  1107
		}
sl@0
  1108
	else
sl@0
  1109
		{
sl@0
  1110
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no string descriptor @ pos %d!"), str_idx));
sl@0
  1111
		return KErrNotFound;
sl@0
  1112
		}
sl@0
  1113
	}
sl@0
  1114
sl@0
  1115
sl@0
  1116
TInt TUsbcDescriptorPool::SetConfigurationStringDescriptorTC(DThread* aThread, const TDes8& aString)
sl@0
  1117
	{
sl@0
  1118
	// we don't know the length of the string, so we have to allocate memory dynamically
sl@0
  1119
	TUint strlen = __THREADDESLEN(aThread, &aString);
sl@0
  1120
	if (strlen > KUsbStringDescStringMaxSize)
sl@0
  1121
		{
sl@0
  1122
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Warning: $ descriptor too long - string will be truncated")));
sl@0
  1123
		strlen = KUsbStringDescStringMaxSize;
sl@0
  1124
		}
sl@0
  1125
sl@0
  1126
	HBuf8Plat* strbuf = NULL;
sl@0
  1127
	__NEWPLATBUF(strbuf, strlen);
sl@0
  1128
	if (!strbuf)
sl@0
  1129
		{
sl@0
  1130
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Memory allocation for config $ desc string failed (1)")));
sl@0
  1131
		return KErrNoMemory;
sl@0
  1132
		}
sl@0
  1133
sl@0
  1134
	TInt r;	
sl@0
  1135
	__THREADREADPLATBUF(aThread, &aString, strbuf, r);
sl@0
  1136
	if (r != KErrNone)
sl@0
  1137
		{
sl@0
  1138
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Thread read error")));
sl@0
  1139
		delete strbuf;
sl@0
  1140
		return r;
sl@0
  1141
		}
sl@0
  1142
	TUsbcStringDescriptor* sd = TUsbcStringDescriptor::New(*strbuf);
sl@0
  1143
	if (!sd)
sl@0
  1144
		{
sl@0
  1145
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Memory allocation for config $ desc failed (2)")));
sl@0
  1146
		delete strbuf;
sl@0
  1147
		return KErrNoMemory;
sl@0
  1148
		}
sl@0
  1149
	TBuf8<KUsbDescSize_Config> config_desc;
sl@0
  1150
	config_desc.FillZ(config_desc.MaxLength());
sl@0
  1151
	iDescriptors[1]->GetDescriptorData(config_desc);
sl@0
  1152
	ExchangeStringDescriptor(config_desc[KUsbDescStringIndex_Config], sd);
sl@0
  1153
	delete strbuf;
sl@0
  1154
	return r;
sl@0
  1155
	}
sl@0
  1156
sl@0
  1157
sl@0
  1158
// --- private ---
sl@0
  1159
sl@0
  1160
void TUsbcDescriptorPool::InsertDevDesc(TUsbcDescriptorBase* aDesc)
sl@0
  1161
	{
sl@0
  1162
	TInt count = iDescriptors.Count();
sl@0
  1163
	if (count > 0)
sl@0
  1164
		{
sl@0
  1165
		DeleteDescriptors(0);								// if there's already something at pos. 0, delete it
sl@0
  1166
		}
sl@0
  1167
	iDescriptors.Insert(aDesc, 0);							// in any case: put the new descriptor at position 0
sl@0
  1168
	}
sl@0
  1169
sl@0
  1170
sl@0
  1171
void TUsbcDescriptorPool::InsertConfigDesc(TUsbcDescriptorBase* aDesc)
sl@0
  1172
	{
sl@0
  1173
	TInt count = iDescriptors.Count();
sl@0
  1174
	if (count == 0)
sl@0
  1175
		{
sl@0
  1176
		TUsbcDescriptorBase* const iNullDesc = NULL;
sl@0
  1177
		iDescriptors.Append(iNullDesc);						// if array's empty, put a dummy in position 0
sl@0
  1178
		}
sl@0
  1179
	else if (count > 1)
sl@0
  1180
		{
sl@0
  1181
		DeleteDescriptors(1);								// if there's already something at pos. 1, delete it
sl@0
  1182
		}
sl@0
  1183
	iDescriptors.Insert(aDesc, 1);							// in any case: put the new descriptor at position 1
sl@0
  1184
	// Currently this code assumes, that the config descriptor is inserted _before_ any interface
sl@0
  1185
	// or endpoint descriptors!
sl@0
  1186
	if (iDescriptors.Count() != 2)
sl@0
  1187
		{
sl@0
  1188
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("TUsbcDescriptorPool::InsertConfigDesc: config descriptor will be invalid!")));
sl@0
  1189
		}
sl@0
  1190
	}
sl@0
  1191
sl@0
  1192
sl@0
  1193
void TUsbcDescriptorPool::InsertIfcDesc(TUsbcDescriptorBase* aDesc)
sl@0
  1194
	{
sl@0
  1195
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::InsertIfcDesc()")));
sl@0
  1196
	TInt count = iDescriptors.Count();
sl@0
  1197
	if (count < 2)
sl@0
  1198
		{
sl@0
  1199
		TUsbcDescriptorBase* const iNullDesc = NULL;
sl@0
  1200
		iDescriptors.Append(iNullDesc);						// if array's too small, put some dummies in
sl@0
  1201
		iDescriptors.Append(iNullDesc);
sl@0
  1202
		}
sl@0
  1203
	TBool interface_exists = EFalse;						// set to 'true' if we're adding an alternate
sl@0
  1204
															// setting to an already existing interface
sl@0
  1205
	TInt i = 2;
sl@0
  1206
	while (i < count)
sl@0
  1207
		{
sl@0
  1208
		if (iDescriptors[i]->Type() == KUsbDescType_Interface)
sl@0
  1209
			{
sl@0
  1210
			if (iDescriptors[i]->Byte(2) > aDesc->Byte(2))
sl@0
  1211
				{
sl@0
  1212
				// our interface number is less than the one's just found => insert before it (= here)
sl@0
  1213
				break;
sl@0
  1214
				}
sl@0
  1215
			else if (iDescriptors[i]->Byte(2) == aDesc->Byte(2))
sl@0
  1216
				{
sl@0
  1217
				interface_exists = ETrue;
sl@0
  1218
				// same interface number => look at settings number
sl@0
  1219
				if (iDescriptors[i]->Byte(3) > aDesc->Byte(3))
sl@0
  1220
					{
sl@0
  1221
					// our setting number is less than the one's found => insert before (= here)
sl@0
  1222
					break;
sl@0
  1223
					}
sl@0
  1224
				else if (iDescriptors[i]->Byte(3) == aDesc->Byte(3))
sl@0
  1225
					{
sl@0
  1226
					__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("TUsbcDescriptorPool::InsertIfcDesc: error: first delete old one!")));
sl@0
  1227
					return;
sl@0
  1228
					}
sl@0
  1229
				}
sl@0
  1230
			}
sl@0
  1231
		++i;
sl@0
  1232
		}
sl@0
  1233
	iDescriptors.Insert(aDesc, i);							// in any case: put the new descriptor at position i
sl@0
  1234
	if (iDescriptors[1])
sl@0
  1235
		{
sl@0
  1236
		// if there's a config descriptor (and not a NULL pointer), update its wTotalLength field...
sl@0
  1237
		iDescriptors[1]->SetWord(2, (TUint8)(iDescriptors[1]->Word(2) + KUsbDescSize_Interface));
sl@0
  1238
		//  and increment bNumInterfaces if this is the first setting for the interface
sl@0
  1239
		if (!interface_exists)
sl@0
  1240
			iDescriptors[1]->SetByte(4, (TUint8)(iDescriptors[1]->Byte(4) + 1));
sl@0
  1241
		}
sl@0
  1242
	iIfcIdx = i;
sl@0
  1243
	}
sl@0
  1244
sl@0
  1245
sl@0
  1246
void TUsbcDescriptorPool::InsertEpDesc(TUsbcDescriptorBase* aDesc)
sl@0
  1247
	{
sl@0
  1248
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::InsertEpDesc()")));
sl@0
  1249
	if (iIfcIdx == 0)
sl@0
  1250
		{
sl@0
  1251
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("TUsbcDescriptorPool::InsertEpDesc: error: only after interface")));
sl@0
  1252
		return;
sl@0
  1253
		}
sl@0
  1254
	TInt count = iDescriptors.Count();
sl@0
  1255
	TInt i = iIfcIdx + 1;
sl@0
  1256
	while (i < count)
sl@0
  1257
		{
sl@0
  1258
		if (iDescriptors[i]->Type() != KUsbDescType_Endpoint)
sl@0
  1259
			break;
sl@0
  1260
		++i;
sl@0
  1261
		}
sl@0
  1262
	iDescriptors.Insert(aDesc, i);							// put the new descriptor at position i
sl@0
  1263
	if (iDescriptors[1])
sl@0
  1264
		{
sl@0
  1265
		// if there's a config descriptor (and not a NULL pointer), update its wTotalLength field
sl@0
  1266
		iDescriptors[1]->SetWord(2, (TUint8)(iDescriptors[1]->Word(2) + aDesc->Size()));
sl@0
  1267
		}
sl@0
  1268
	}
sl@0
  1269
sl@0
  1270
sl@0
  1271
TInt TUsbcDescriptorPool::FindIfcDescriptor(TInt aIfcNumber, TInt aIfcSetting) const
sl@0
  1272
	{
sl@0
  1273
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::FindIfcDescriptor(%d, %d)"),
sl@0
  1274
									aIfcNumber, aIfcSetting));
sl@0
  1275
	TInt count = iDescriptors.Count();
sl@0
  1276
	for (TInt i = 2; i < count; ++i)
sl@0
  1277
		{
sl@0
  1278
		if ((iDescriptors[i]->Type() == KUsbDescType_Interface) &&
sl@0
  1279
			(iDescriptors[i]->Byte(2) == aIfcNumber) &&
sl@0
  1280
			(iDescriptors[i]->Byte(3) == aIfcSetting))
sl@0
  1281
			{
sl@0
  1282
			return i;
sl@0
  1283
			}
sl@0
  1284
		}
sl@0
  1285
	__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such interface")));
sl@0
  1286
	return -1;
sl@0
  1287
	}
sl@0
  1288
sl@0
  1289
sl@0
  1290
TInt TUsbcDescriptorPool::FindEpDescriptor(TInt aIfcNumber, TInt aIfcSetting, TUint8 aEpAddress) const
sl@0
  1291
	{
sl@0
  1292
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::FindEpDescriptor(%d, %d, 0x%02x)"),
sl@0
  1293
									aIfcNumber, aIfcSetting, aEpAddress));
sl@0
  1294
	// first find the interface
sl@0
  1295
	TInt ifc = FindIfcDescriptor(aIfcNumber, aIfcSetting);
sl@0
  1296
	if (ifc < 0)
sl@0
  1297
		{
sl@0
  1298
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such interface")));
sl@0
  1299
		return ifc;
sl@0
  1300
		}
sl@0
  1301
	TInt count = iDescriptors.Count();
sl@0
  1302
	// then, before the next interface, try to locate the endpoint
sl@0
  1303
	for (TInt i = ifc + 1; i < count; ++i)
sl@0
  1304
		{
sl@0
  1305
		if (iDescriptors[i]->Type() == KUsbDescType_Interface)
sl@0
  1306
			{
sl@0
  1307
			__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint before next interface")));
sl@0
  1308
			return -1;
sl@0
  1309
			}
sl@0
  1310
		else if ((iDescriptors[i]->Type() == KUsbDescType_Endpoint) &&
sl@0
  1311
				 (iDescriptors[i]->Byte(2) == aEpAddress))
sl@0
  1312
			{
sl@0
  1313
			return i;										// found
sl@0
  1314
			}
sl@0
  1315
		}
sl@0
  1316
	__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no such endpoint")));
sl@0
  1317
	return -1;
sl@0
  1318
	}
sl@0
  1319
sl@0
  1320
sl@0
  1321
void TUsbcDescriptorPool::DeleteDescriptors(TInt aIndex, TInt aCount)
sl@0
  1322
	{
sl@0
  1323
	if (aCount <= 0)
sl@0
  1324
		{
sl@0
  1325
		return;
sl@0
  1326
		}
sl@0
  1327
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  Removing descriptors at index %d:"), aIndex));
sl@0
  1328
	while (aCount--)
sl@0
  1329
		{
sl@0
  1330
		// in this loop we don't decrement aIndex, because after deleting an element
sl@0
  1331
		// aIndex is already indexing the next one!
sl@0
  1332
		TUsbcDescriptorBase* ptr = iDescriptors[aIndex];
sl@0
  1333
		if (iDescriptors[1])
sl@0
  1334
			{
sl@0
  1335
			// if there's a config descriptor (and not a NULL pointer),
sl@0
  1336
			if (ptr->Type() == KUsbDescType_Interface)
sl@0
  1337
				{
sl@0
  1338
				__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  - an interface descriptor")));
sl@0
  1339
				// if it's an interface descriptor:
sl@0
  1340
				// we update its wTotalLength field...
sl@0
  1341
				iDescriptors[1]->SetWord(2, (TUint8)(iDescriptors[1]->Word(2) - KUsbDescSize_Interface));
sl@0
  1342
				}
sl@0
  1343
			else if (ptr->Type() == KUsbDescType_Endpoint)
sl@0
  1344
				{
sl@0
  1345
				__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  - an endpoint descriptor")));
sl@0
  1346
				// if it's an endpoint descriptor:
sl@0
  1347
				// we only update its wTotalLength field
sl@0
  1348
				iDescriptors[1]->SetWord(2, (TUint8)(iDescriptors[1]->Word(2) - ptr->Size()));
sl@0
  1349
				}
sl@0
  1350
			else if (ptr->Type() == KUsbDescType_CS_Interface || ptr->Type() == KUsbDescType_CS_Endpoint)
sl@0
  1351
				{
sl@0
  1352
				__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  - a class specific descriptor")));
sl@0
  1353
				// if it's an class specific descriptor:
sl@0
  1354
				// we only update its wTotalLength field
sl@0
  1355
				iDescriptors[1]->SetWord(2, (TUint8)(iDescriptors[1]->Word(2) - ptr->Size()));
sl@0
  1356
				}
sl@0
  1357
			}
sl@0
  1358
		iDescriptors.Remove(aIndex);
sl@0
  1359
		delete ptr;
sl@0
  1360
		}
sl@0
  1361
	}
sl@0
  1362
sl@0
  1363
sl@0
  1364
void TUsbcDescriptorPool::DeleteString(TInt aIndex)
sl@0
  1365
	{
sl@0
  1366
	TUsbcStringDescriptorBase* ptr = iStrings[aIndex];
sl@0
  1367
	iStrings.Remove(aIndex);
sl@0
  1368
	delete ptr;
sl@0
  1369
	}
sl@0
  1370
sl@0
  1371
sl@0
  1372
void TUsbcDescriptorPool::UpdateIfcNumbers(TInt aNumber)
sl@0
  1373
	{
sl@0
  1374
	const TInt count = iDescriptors.Count();
sl@0
  1375
	for (TInt i = 2; i < count; ++i)
sl@0
  1376
		{
sl@0
  1377
		if ((iDescriptors[i]->Type() == KUsbDescType_Interface) &&
sl@0
  1378
			(iDescriptors[i]->Byte(2) == aNumber))
sl@0
  1379
			{
sl@0
  1380
			// there's still an interface with 'number' so we don't need to update anything
sl@0
  1381
			return;
sl@0
  1382
			}
sl@0
  1383
		}
sl@0
  1384
	// if we haven't returned yet, we decrement bNumInterfaces
sl@0
  1385
	iDescriptors[1]->SetByte(4, (TUint8)(iDescriptors[1]->Byte(4) - 1));
sl@0
  1386
	}
sl@0
  1387
sl@0
  1388
sl@0
  1389
void TUsbcDescriptorPool::UpdateIfcStringIndexes(TInt aStringIndex)
sl@0
  1390
	{
sl@0
  1391
	// aStringIndex is the index value of the string descriptor that has just been removed.
sl@0
  1392
	// We update all ifc descriptors with a string index value that is greater than aStringIndex,
sl@0
  1393
	// because those strings moved all down by one position.
sl@0
  1394
	//
sl@0
  1395
	TInt count = iDescriptors.Count();
sl@0
  1396
	for (TInt i = 2; i < count; ++i)
sl@0
  1397
		{
sl@0
  1398
		if ((iDescriptors[i]->Type() == KUsbDescType_Interface) &&
sl@0
  1399
			(iDescriptors[i]->Byte(8) > aStringIndex))
sl@0
  1400
			{
sl@0
  1401
			iDescriptors[i]->SetByte(8, (TUint8)(iDescriptors[i]->Byte(8) - 1));
sl@0
  1402
			}
sl@0
  1403
		}
sl@0
  1404
	}
sl@0
  1405
sl@0
  1406
sl@0
  1407
//
sl@0
  1408
// Only used for Ep0 standard requests, so target buffer could be hard-wired.
sl@0
  1409
//
sl@0
  1410
TInt TUsbcDescriptorPool::GetDeviceDescriptor() const
sl@0
  1411
	{
sl@0
  1412
	return iDescriptors[0]->GetDescriptorData(iEp0_TxBuf, KUsbcBufSz_Ep0Tx);
sl@0
  1413
	}
sl@0
  1414
sl@0
  1415
sl@0
  1416
//
sl@0
  1417
// Only used for Ep0 standard requests, so target buffer could be hard-wired.
sl@0
  1418
//
sl@0
  1419
TInt TUsbcDescriptorPool::GetConfigDescriptor() const
sl@0
  1420
	{
sl@0
  1421
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::GetConfigDescriptor()")));
sl@0
  1422
	TInt copied = 0;
sl@0
  1423
	TInt count = iDescriptors.Count();
sl@0
  1424
	TUint8* buf = iEp0_TxBuf;
sl@0
  1425
	for (TInt i = 1; i < count; ++i)
sl@0
  1426
		{
sl@0
  1427
		__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING(" > desc[%02d]: type = 0x%02x size = %d "),
sl@0
  1428
										i, iDescriptors[i]->Type(), iDescriptors[i]->Size()));
sl@0
  1429
		const TInt size = iDescriptors[i]->GetDescriptorData(buf, KUsbcBufSz_Ep0Tx - copied);
sl@0
  1430
		if (size == 0)
sl@0
  1431
			{
sl@0
  1432
			// There was no buffer space to copy the descriptor -> no use to proceed
sl@0
  1433
			break;
sl@0
  1434
			}
sl@0
  1435
		copied += size;
sl@0
  1436
		if (copied >= KUsbcBufSz_Ep0Tx)
sl@0
  1437
			{
sl@0
  1438
			// There's no buffer space left -> we need to stop copying here
sl@0
  1439
			break;
sl@0
  1440
			}
sl@0
  1441
		buf += size;
sl@0
  1442
		}
sl@0
  1443
	return copied;
sl@0
  1444
	}
sl@0
  1445
sl@0
  1446
sl@0
  1447
//
sl@0
  1448
// Only used for Ep0 standard requests, so target buffer could be hard-wired.
sl@0
  1449
//
sl@0
  1450
TInt TUsbcDescriptorPool::GetStringDescriptor(TInt aIndex) const
sl@0
  1451
	{
sl@0
  1452
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("TUsbcDescriptorPool::GetStringDescriptor()")));
sl@0
  1453
	// I really would have liked to display here the descriptor contents, but without trailing zero
sl@0
  1454
	// we got a problem: how could we tell printf where the string ends? We would have to
sl@0
  1455
	// dynamically allocate memory (since we don't know the size in advance), copy the descriptor
sl@0
  1456
	// contents there, append a zero, and give this to printf. That's a bit too much effort...
sl@0
  1457
	if (iStrings[aIndex])
sl@0
  1458
		{
sl@0
  1459
		__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  String @ pos %d"), aIndex));
sl@0
  1460
		TInt size = iStrings[aIndex]->GetDescriptorData(iEp0_TxBuf, KUsbcBufSz_Ep0Tx);
sl@0
  1461
		return size;
sl@0
  1462
		}
sl@0
  1463
	else
sl@0
  1464
		{
sl@0
  1465
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no descriptor @ pos %d!"), aIndex));
sl@0
  1466
		return 0;
sl@0
  1467
		}
sl@0
  1468
	}
sl@0
  1469
sl@0
  1470
sl@0
  1471
TInt TUsbcDescriptorPool::GetDeviceStringDescriptorTC(DThread* aThread, TDes8& aString, TInt aIndex) const
sl@0
  1472
	{
sl@0
  1473
	TBuf8<KUsbDescSize_Device> dev_desc;
sl@0
  1474
	iDescriptors[0]->GetDescriptorData(dev_desc);
sl@0
  1475
	const TInt str_idx = dev_desc[aIndex];
sl@0
  1476
	if ((str_idx > 0) && iStrings[str_idx])
sl@0
  1477
		{
sl@0
  1478
		__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  String @ pos %d (device $): \"%S\""),
sl@0
  1479
										str_idx, &iStrings[str_idx]->StringData()));
sl@0
  1480
		return __THREADWRITE(aThread, &aString, iStrings[str_idx]->StringData());
sl@0
  1481
		}
sl@0
  1482
	else
sl@0
  1483
		{
sl@0
  1484
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: no string descriptor @ pos %d!"), str_idx));
sl@0
  1485
		return KErrNotFound;
sl@0
  1486
		}
sl@0
  1487
	}
sl@0
  1488
sl@0
  1489
sl@0
  1490
TInt TUsbcDescriptorPool::SetDeviceStringDescriptorTC(DThread* aThread, const TDes8& aString, TInt aIndex)
sl@0
  1491
	{
sl@0
  1492
	// we don't know the length of the string, so we have to allocate memory dynamically
sl@0
  1493
	TUint strlen = __THREADDESLEN(aThread, &aString);
sl@0
  1494
	if (strlen > KUsbStringDescStringMaxSize)
sl@0
  1495
		{
sl@0
  1496
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Warning: $ descriptor too long - string will be truncated")));
sl@0
  1497
		strlen = KUsbStringDescStringMaxSize;
sl@0
  1498
		}
sl@0
  1499
	
sl@0
  1500
	HBuf8Plat* strbuf = NULL;
sl@0
  1501
	__NEWPLATBUF(strbuf, strlen);
sl@0
  1502
	if (!strbuf)
sl@0
  1503
		{
sl@0
  1504
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Memory allocation for dev $ desc string failed (1)")));
sl@0
  1505
		return KErrNoMemory;
sl@0
  1506
		}
sl@0
  1507
sl@0
  1508
	TInt r;	
sl@0
  1509
	__THREADREADPLATBUF(aThread, &aString, strbuf, r);
sl@0
  1510
	if (r != KErrNone)
sl@0
  1511
		{
sl@0
  1512
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Thread read error")));
sl@0
  1513
		delete strbuf;
sl@0
  1514
		return r;
sl@0
  1515
		}
sl@0
  1516
	TUsbcStringDescriptor* sd = TUsbcStringDescriptor::New(*strbuf);
sl@0
  1517
	if (!sd)
sl@0
  1518
		{
sl@0
  1519
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: Memory allocation for dev $ desc failed (2)")));
sl@0
  1520
		delete strbuf;
sl@0
  1521
		return KErrNoMemory;
sl@0
  1522
		}
sl@0
  1523
	TBuf8<KUsbDescSize_Device> dev_desc;
sl@0
  1524
	dev_desc.FillZ(dev_desc.MaxLength());
sl@0
  1525
	iDescriptors[0]->GetDescriptorData(dev_desc);
sl@0
  1526
	ExchangeStringDescriptor(dev_desc[aIndex], sd);
sl@0
  1527
	delete strbuf;
sl@0
  1528
	return r;
sl@0
  1529
	}
sl@0
  1530
sl@0
  1531
sl@0
  1532
TInt TUsbcDescriptorPool::ExchangeStringDescriptor(TInt aIndex, const TUsbcStringDescriptor* aDesc)
sl@0
  1533
	{
sl@0
  1534
	if (aIndex <= 0)
sl@0
  1535
		{
sl@0
  1536
		__KTRACE_OPT(KPANIC, Kern::Printf(__KSTRING("  Error: invalid string descriptor index: %d!"), aIndex));
sl@0
  1537
		return KErrArgument;
sl@0
  1538
		}
sl@0
  1539
	__KTRACE_OPT(KUSB, Kern::Printf(__KSTRING("  Exchanging string descriptor @ index %d"), aIndex));
sl@0
  1540
	DeleteString(aIndex);
sl@0
  1541
	iStrings.Insert(aDesc, aIndex);
sl@0
  1542
	return KErrNone;
sl@0
  1543
	}
sl@0
  1544
sl@0
  1545
sl@0
  1546
// -eof-