libusbk-sys 0.2.0

Rust Windows library for accessing USB devices via libusbK
Documentation
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*!********************************************************************
libusbK - WDF USB driver.
Copyright (C) 2012 Travis Lee Robinson. All Rights Reserved.
libusb-win32.sourceforge.net

Development : Travis Lee Robinson  (libusbdotnet@gmail.com)
Testing     : Xiaofan Chen         (xiaofanc@gmail.com)

At the discretion of the user of this library, this software may be
licensed under the terms of the GNU Public License v3 or a BSD-Style
license as outlined in the following files:
* LICENSE-gpl3.txt
* LICENSE-bsd.txt

License files are located in a license folder at the root of source and
binary distributions.
********************************************************************!*/

#include "drv_common.h"
extern WDFDRIVER gWdfDriver;

// warning C4127: conditional expression is constant.
#pragma warning(disable: 4127)

#if (defined(ALLOC_PRAGMA) && defined(PAGING_ENABLED))
#pragma alloc_text(PAGE, Pipe_AbortAll)
#pragma alloc_text(PAGE, Pipe_Reset)
#pragma alloc_text(PAGE, Pipe_Abort)
#pragma alloc_text(PAGE, Pipe_Start)
#pragma alloc_text(PAGE, Pipe_Stop)
#pragma alloc_text(PAGE, Pipe_StartAll)
#pragma alloc_text(PAGE, Pipe_StopAll)
#pragma alloc_text(PAGE, Pipe_GetContextFromName)
#endif

// The prefix string for pipe file handles.
#define PIPENAME_PREFIX PIPE_


// macros to simplify building the default 'PipeNameToPipeID' array.
#define _DEF_PIPENAME_MAP(Name,PipeID) 	{#Name #PipeID,0x##PipeID,L#Name L#PipeID}

#define  DEF_PIPENAME_MAP(Name,PipeID) _DEF_PIPENAME_MAP(Name,PipeID)


// Structure for mapping a string to a pipe id.
typedef struct _PIPENAME_MAP
{
	LPCSTR Name;		// Display purposes only
	CCHAR PipeID;		// The endpoint address the PIPENAME_MAP is associated with.
	LPCWSTR NameW;		// Used to match the filename passed by the user.
} PIPENAME_MAP, *PPIPENAME_MAP;

// Default libusbK pipe name to pipe id map array.
CONST PIPENAME_MAP PipeNameToPipeID[] =
{
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 01), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 81),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 02), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 82),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 03), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 83),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 04), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 84),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 05), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 85),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 06), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 86),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 07), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 87),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 08), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 88),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 09), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 89),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 0A), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 8A),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 0B), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 8B),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 0C), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 8C),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 0D), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 8D),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 0E), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 8E),
	DEF_PIPENAME_MAP(PIPENAME_PREFIX, 0F), DEF_PIPENAME_MAP(PIPENAME_PREFIX, 8F),
};

NTSTATUS Pipe_Reset(__in PDEVICE_CONTEXT deviceContext,
                    __in UCHAR pipeID)
{
	NTSTATUS status = STATUS_INVALID_PARAMETER;
	PPIPE_CONTEXT pipeContext;

	PAGED_CODE();

	pipeContext = GetPipeContextByID(deviceContext, pipeID);
	if (pipeContext->Pipe)
	{
		USBMSG("pipeID=%02Xh\n", pipeID);

		status = WdfUsbTargetPipeResetSynchronously(pipeContext->Pipe, WDF_NO_HANDLE, NULL);
		if (!NT_SUCCESS(status))
		{
			USBERR("WdfUsbTargetPipeResetSynchronously failed pipeID=%02Xh status=%Xh\n", pipeID, status);
		}
		else
		{
			if (pipeContext->Queue)
			{
				PQUEUE_CONTEXT queueContext = GetQueueContext(pipeContext->Queue);
				queueContext->IsFreshPipeReset = TRUE;
			}
		}
	}
	else
	{
		USBERR("pipeID=%02Xh not found\n", pipeID);
	}

	return status;
}

NTSTATUS Pipe_Abort(__in PDEVICE_CONTEXT deviceContext,
                    __in UCHAR pipeID)
{
	NTSTATUS status = STATUS_INVALID_PARAMETER;
	PPIPE_CONTEXT pipeContext;

	PAGED_CODE();

	pipeContext = GetPipeContextByID(deviceContext, pipeID);
	if (pipeContext->Pipe)
	{
		USBMSG("pipeID=%02Xh\n", pipeID);

		status = WdfUsbTargetPipeAbortSynchronously(pipeContext->Pipe, WDF_NO_HANDLE, NULL);
		if (!NT_SUCCESS(status))
		{
			USBERR("WdfUsbTargetPipeResetSynchronously failed pipeID=%02Xh status=%Xh\n", pipeID, status);
		}
	}
	else
	{
		USBERR("pipeID=%02Xh not found\n", pipeID);
	}

	return status;
}

NTSTATUS Pipe_RefreshQueue(__in PDEVICE_CONTEXT deviceContext, __in PPIPE_CONTEXT pipeContext)
{
	NTSTATUS status;

	status = Pipe_Stop(pipeContext, WdfIoTargetCancelSentIo, TRUE, FALSE);
	if (!NT_SUCCESS(status))
	{
		USBERRN("Pipe_Stop failed. PipeID=%02Xh Status=%08Xh", pipeContext->PipeInformation.EndpointAddress, status);
		goto Done;
	}

	status = Pipe_Start(deviceContext, pipeContext, FALSE);
	if (!NT_SUCCESS(status))
	{
		USBERRN("Pipe_Start failed. PipeID=%02Xh Status=%08Xh", pipeContext->PipeInformation.EndpointAddress, status);
		goto Done;
	}

Done:
	return status;
}

NTSTATUS Pipe_Stop(__in PPIPE_CONTEXT pipeContext,
                   __in WDF_IO_TARGET_SENT_IO_ACTION WdfIoTargetSentIoAction,
                   __in BOOLEAN purgeQueue,
				   __in BOOLEAN stopIoTarget)
{
	NTSTATUS status = STATUS_SUCCESS;
	PAGED_CODE();

	if (pipeContext->IsValid == TRUE)
	{
		pipeContext->IsValid = FALSE; // mark context invalid.

		if (pipeContext->Queue && purgeQueue)	// stop queue, cancel any outstanding requests
			WdfIoQueuePurgeSynchronously(pipeContext->Queue);

		if (pipeContext->Pipe && stopIoTarget)
			PipeStop(pipeContext, WdfIoTargetSentIoAction); // stop pipe, cancel any outstanding requests
	}
	else
	{
		USBERRN("Invalid pipeContext");
		status = STATUS_INVALID_PIPE_STATE;
	}
	return status;
}

// sends an abort pipe request on all open pipes.
NTSTATUS Pipe_AbortAll(__in PDEVICE_CONTEXT deviceContext)
{
	UCHAR              interfaceIndex, pipeIndex;
	UCHAR              interfaceCount, pipeCount;
	NTSTATUS           status;

	PAGED_CODE();

	interfaceCount = deviceContext->InterfaceCount;
	for (interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++)
	{
		pipeCount = deviceContext->InterfaceContext[interfaceIndex].PipeCount;
		for (pipeIndex = 0; pipeIndex < pipeCount; pipeIndex++)
		{
			USBMSG("interface-number=%u pipe-id=%2Xh",
			       deviceContext->InterfaceContext[interfaceIndex].InterfaceDescriptor.bInterfaceNumber,
			       deviceContext->InterfaceContext[interfaceIndex].PipeContextByIndex[pipeIndex]->PipeInformation.EndpointAddress);

			status = WdfUsbTargetPipeAbortSynchronously(
			             deviceContext->InterfaceContext[interfaceIndex].PipeContextByIndex[pipeIndex]->Pipe,
			             WDF_NO_HANDLE, // WDFREQUEST
			             NULL);//PWDF_REQUEST_SEND_OPTIONS

			if (!NT_SUCCESS(status))
			{
				USBMSG("WdfUsbTargetPipeAbortSynchronously failed %Xh\n", status);
				break;
			}
		}
	}

	return STATUS_SUCCESS;
}

VOID Pipe_StopAll(__in PDEVICE_CONTEXT deviceContext, __in BOOLEAN stopIoTarget)
{
	PPIPE_CONTEXT pipeContext = NULL;
	UCHAR interfaceIndex, pipeIndex;
	UCHAR interfaceCount, pipeCount;

	PAGED_CODE();

	interfaceCount = deviceContext->InterfaceCount;
	for (interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++)
	{
		pipeCount = deviceContext->InterfaceContext[interfaceIndex].PipeCount;
		for (pipeIndex = 0; pipeIndex < pipeCount; pipeIndex++)
		{
			pipeContext = deviceContext->InterfaceContext[interfaceIndex].PipeContextByIndex[pipeIndex];
			Pipe_Stop(pipeContext, WdfIoTargetCancelSentIo, TRUE, stopIoTarget);
		}
	}
}

VOID Pipe_InitQueueConfig(
    __in PPIPE_CONTEXT pipeContext,
    __out WDF_IO_QUEUE_CONFIG* queueConfig)
{
	WDF_DRIVER_VERSION_AVAILABLE_PARAMS verParams;

	if (!pipeContext->Pipe)
	{
		// Default (Control) pipe
		WDF_IO_QUEUE_CONFIG_INIT(queueConfig,	WdfIoQueueDispatchSequential);
		queueConfig->EvtIoDeviceControl = PipeQueue_OnIoControl;
		return;
	}

	if (pipeContext->PipeInformation.PipeType == WdfUsbPipeTypeIsochronous || pipeContext->Policies.RawIO)
	{
		USBDBGN("Configuring parallel queue..");
		WDF_IO_QUEUE_CONFIG_INIT(queueConfig,	WdfIoQueueDispatchParallel);

		/*
		NOTE: WinUSB does not support RawIO OUT transfers; libusbK does.
		http://msdn.microsoft.com/en-us/library/windows/hardware/ff728833%28v=vs.85%29.aspx

		In libusbK, RawIO means we use a parallel queue.  Any pipe policies or functionality
		that could cause multiple IO transactions per request are no longer supported. IE:
		Transfer splitting, ALLOW_ARTIAL_READS, AUTO_FLUSH, IGNORE_SHORT_PACKETS, SHORT_PACKET_TERMINATE.
		*/

		WDF_DRIVER_VERSION_AVAILABLE_PARAMS_INIT(&verParams, KMDF_MAJOR_VERSION, KMDF_MINOR_VERSION);
		if (WdfDriverIsVersionAvailable(gWdfDriver, &verParams))
		{
#		if ((KMDF_MAJOR_VERSION==1 && KMDF_MINOR_VERSION >= 9) || (KMDF_MAJOR_VERSION > 1))
			if (pipeContext->SimulParallelRequests)
				queueConfig->Settings.Parallel.NumberOfPresentedRequests = pipeContext->SimulParallelRequests;
#		endif
		}
		else
		{
			DbgPrint("Expected library version %u.%u!\n",KMDF_MAJOR_VERSION, KMDF_MINOR_VERSION);
		}
	}
	else
	{
		USBDBGN("Configuring sequential queue..");
		WDF_IO_QUEUE_CONFIG_INIT(queueConfig, WdfIoQueueDispatchSequential);

		queueConfig->EvtIoStop = Queue_OnStop;
		queueConfig->EvtIoResume = Queue_OnResume;
	}

	queueConfig->EvtIoDeviceControl = PipeQueue_OnIoControl;
	if (USB_ENDPOINT_DIRECTION_IN(pipeContext->PipeInformation.EndpointAddress))
	{
		queueConfig->EvtIoRead = PipeQueue_OnRead;
	}
	else
	{
		queueConfig->EvtIoWrite = PipeQueue_OnWrite;
		queueConfig->AllowZeroLengthRequests = TRUE;
	}
}

NTSTATUS Pipe_InitQueue(
    __in PDEVICE_CONTEXT deviceContext,
    __in PPIPE_CONTEXT pipeContext,
    __out WDFQUEUE* queueRef)
{
	WDF_IO_QUEUE_CONFIG queueConfig;
	WDF_OBJECT_ATTRIBUTES objectAttributes;
	NTSTATUS status = STATUS_INVALID_HANDLE;
	WDFQUEUE queue = NULL;
	PQUEUE_CONTEXT queueContext;
	WDF_OBJECT_ATTRIBUTES memAttributes;

	WDF_OBJECT_ATTRIBUTES_INIT(&objectAttributes);
	objectAttributes.SynchronizationScope = WdfSynchronizationScopeQueue;

	*queueRef = NULL;

	// All queues get a context. At the very least, they will use a local copy of policy and pipe information
	WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&objectAttributes, QUEUE_CONTEXT);

	queueConfig.DispatchType = WdfIoQueueDispatchInvalid;
	Pipe_InitQueueConfig(pipeContext, &queueConfig);

	if (queueConfig.DispatchType != WdfIoQueueDispatchInvalid)
	{
		status = WdfIoQueueCreate(deviceContext->WdfDevice,
		                          &queueConfig,
		                          &objectAttributes,
		                          &queue);
		if (!NT_SUCCESS(status))
		{
			USBERR("WdfIoQueueCreate failed. pipeID=%02Xh status=%Xh\n", pipeContext->PipeInformation.EndpointAddress, status);
			goto Exit;
		}

		// Create the memory for partial read storage
		queueContext = GetQueueContext(queue);

		queueContext->IsFreshPipeReset = TRUE;

		// SET queueContext->OverOfs
		RtlZeroMemory(&queueContext->OverOfs, sizeof(queueContext->OverOfs));

		// SET queueContext->PipeHandle
		queueContext->PipeHandle = pipeContext->Pipe;

		// SET queueContext->Info
		RtlCopyMemory(&queueContext->Info, &pipeContext->PipeInformation, sizeof(queueContext->Info));

		WDF_OBJECT_ATTRIBUTES_INIT(&memAttributes);
		memAttributes.ParentObject = queue;

		// Only bulk and interrupt pipes have an OverMem buffer and it is only used
		// when the queue type is sequential. (RAW_IO=FALSE)
		if ((queueContext->Info.MaximumPacketSize) &&
		        (queueContext->Info.PipeType == WdfUsbPipeTypeBulk || queueContext->Info.PipeType == WdfUsbPipeTypeInterrupt))
		{
			// SET queueContext->OverMem
			// SET queueContext->OverBuf
			status = WdfMemoryCreate(&memAttributes, NonPagedPool, POOL_TAG, queueContext->Info.MaximumPacketSize, &queueContext->OverMem, &queueContext->OverBuf);
			if (!NT_SUCCESS(status))
			{
				USBERRN("WdfMemoryCreate failed. status=%08Xh", status);
				WdfObjectDelete(queue);
				goto Exit;
			}
		}
	}

	*queueRef = queue;

Exit:
	return status;
}


//
// PRE REQUIREMENTS:
// * pipeContext->IsValid == FALSE
// * Queue(if any) must be stopped
// * IoTarget must be stopped
//
// On return the pipe and queue are started and the context is marked valid again.
//
NTSTATUS Pipe_InitContext(__in PDEVICE_CONTEXT deviceContext,
                          __in PPIPE_CONTEXT pipeContext,
						  __in BOOLEAN startIoTarget)
{
	NTSTATUS status = STATUS_INVALID_HANDLE;
	WDFQUEUE queueOld	= pipeContext->Queue;
	WDFQUEUE queueNew	= NULL;

	if (!pipeContext->Pipe && pipeContext->PipeInformation.PipeType != WdfUsbPipeTypeControl)
	{
		USBERR("pipeID=%02Xh invalid pipe handle\n", pipeContext->PipeInformation.EndpointAddress);
		goto Done;
	}

	if (queueOld == NULL || ((pipeContext->PipeInformation.EndpointAddress & 0xF) && pipeContext->IsQueueDirty))
	{
		pipeContext->IsQueueDirty = FALSE;
		if (queueOld != NULL)
		{
			// We need to delete the old pipe queue.
			USBDBGN("pipeID=%02Xh Destroying old pipe queue.", pipeContext->PipeInformation.EndpointAddress);
			WdfObjectDelete(queueOld);
			queueOld = NULL;
		}
		USBDBGN("pipeID=%02Xh Creating pipe queue.", pipeContext->PipeInformation.EndpointAddress);
		status = Pipe_InitQueue(deviceContext, pipeContext, &queueNew);
		if (!NT_SUCCESS(status))
		{
			pipeContext->Queue = NULL;
			pipeContext->IsValid = FALSE;
			USBERRN("Pipe_InitQueue failed. pipeID=%02Xh status=%08Xh", pipeContext->PipeInformation.EndpointAddress, status);
			goto Done;
		}

		pipeContext->Queue = queueNew;
		
	}
	else
	{
		// Queue is already created and does not need to be updated.
		status = STATUS_SUCCESS;
		queueNew = queueOld;
	}
	if (!queueNew && NT_SUCCESS(status))
		status = STATUS_INVALID_PIPE_STATE;

	if (NT_SUCCESS(status))
	{
		if (pipeContext->PipeInformation.PipeType != WdfUsbPipeTypeControl)
		{
			if (startIoTarget)
			{
				// start pipe
				USBDBG("pipeID=%02Xh starting..\n", pipeContext->PipeInformation.EndpointAddress);
				
				status = PipeStart(pipeContext);
				if (!NT_SUCCESS(status))
				{
					pipeContext->IsValid = FALSE;
					USBERR("WdfIoTargetStart failed. status=%Xh\n", status);
					goto Done;
				}
			}
		}

		// start queue
		USBDBG("pipeID=%02Xh queue starting..\n", pipeContext->PipeInformation.EndpointAddress);
		WdfIoQueueStart(queueNew);
		pipeContext->IsValid = TRUE;

	}
	else
	{
		USBERR("WdfIoQueueCreate failed. status=%Xh\n", status);
		pipeContext->IsValid = FALSE;
		goto Done;
	}

Done:
	return status;
}

NTSTATUS Pipe_Start(__in PDEVICE_CONTEXT deviceContext,
                    __in PPIPE_CONTEXT pipeContext,
					__in BOOLEAN startIoTarget)
{
	NTSTATUS status = STATUS_INVALID_PIPE_STATE;

	PAGED_CODE();

	if (!pipeContext)
		return status;

	if (pipeContext->IsValid == FALSE)
	{
		status = Pipe_InitContext(deviceContext, pipeContext, startIoTarget);
		if (!NT_SUCCESS(status))
		{
			USBERR("pipeID=%02Xh failed initializing pipe\n",
			       pipeContext->PipeInformation.EndpointAddress);
		}
	}
	else
	{
		USBERRN("pipeContext already valid!");
	}
	return status;
}

VOID Pipe_StartAll(__in PDEVICE_CONTEXT deviceContext, __in BOOLEAN startIoTarget)
{
	PPIPE_CONTEXT pipeContext = NULL;
	UCHAR interfaceIndex, pipeIndex;
	UCHAR interfaceCount, pipeCount;
	NTSTATUS status;

	PAGED_CODE();

	interfaceCount = deviceContext->InterfaceCount;
	for (interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++)
	{
		pipeCount = deviceContext->InterfaceContext[interfaceIndex].PipeCount;
		for (pipeIndex = 0; pipeIndex < pipeCount; pipeIndex++)
		{
			pipeContext = deviceContext->InterfaceContext[interfaceIndex].PipeContextByIndex[pipeIndex];
			status = Pipe_Start(deviceContext, pipeContext, startIoTarget);
		}
	}
}


// This routine will pass the string pipe name and fetch the pipe handle.
PPIPE_CONTEXT Pipe_GetContextFromName(
    __in PDEVICE_CONTEXT DeviceContext,
    __in PUNICODE_STRING FileName)
{
	INT					nameLength, index;
	PPIPE_CONTEXT		pipeContext = NULL;
	PAGED_CODE();

	nameLength = (INT)(FileName->Length / sizeof(WCHAR));
	for (index = 0; index < sizeof(PipeNameToPipeID) / sizeof(PipeNameToPipeID[0]); index++)
	{
		INT checkLength = (INT)wcslen(PipeNameToPipeID[index].NameW);
		if (checkLength == nameLength &&
		        wmemcmp(FileName->Buffer, PipeNameToPipeID[index].NameW, nameLength) == 0)
		{
			pipeContext = GetPipeContextByID(DeviceContext, PipeNameToPipeID[index].PipeID);

			if (pipeContext == NULL || !pipeContext->IsValid || !pipeContext->Pipe || !pipeContext->Queue)
			{
				USBERR("pipe filename %s is valid but the pipe does not exist\n", PipeNameToPipeID[index].Name);
				return NULL;
			}

			return pipeContext; // a valid pipe was found.
		}
	}

	// The pipe name was not recognized.  Pipe names are case-sensitive and in the format:
	// 'PIPE_xx' where xx is the two digit hex endpoint address. i.e. PIPE_0A, PIPE_8A.
	USBERR("invalid pipe filename=%wZ\n", FileName->Buffer);
	return NULL;
}

NTSTATUS Pipe_InitDefaultContext(__in PDEVICE_CONTEXT deviceContext)
{
	PPIPE_CONTEXT pipeContext = GetPipeContextByID(deviceContext, 0);
	Policy_InitPipe(deviceContext, pipeContext);

	pipeContext->Pipe = WDF_NO_HANDLE;
	pipeContext->Queue = WDF_NO_HANDLE;

	pipeContext->PipeInformation.MaximumPacketSize		= deviceContext->UsbDeviceDescriptor.bMaxPacketSize0;
	pipeContext->PipeInformation.MaximumTransferSize	= MAX_CONTROL_TRANSFER_SIZE;
	pipeContext->PipeInformation.EndpointAddress		= 0x00;
	pipeContext->PipeInformation.PipeType				= WdfUsbPipeTypeControl;

	pipeContext->TimeoutPolicy							= 5000;

	return Pipe_InitContext(deviceContext, pipeContext, FALSE);
}

ULONG Pipe_CalcMaxTransferSize(
    __in BOOLEAN IsHS,
    __in WDF_USB_PIPE_TYPE pipeType,
    __in ULONG maxPacketSize,
    __in ULONG originalMaxTransferSize)
{
	ULONG maxTransferSize;

	switch (pipeType)
	{
	case WdfUsbPipeTypeIsochronous:
		maxTransferSize = IsHS ? (1024 * maxPacketSize) : (255 * maxPacketSize);
		break;
	case WdfUsbPipeTypeBulk:
	case WdfUsbPipeTypeInterrupt:
		maxTransferSize = maxPacketSize * 4096;
		break;
	default:
		return originalMaxTransferSize;
	}

	return maxTransferSize > originalMaxTransferSize ? originalMaxTransferSize : maxTransferSize;
}