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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
Queue.c
Abstract:
This file contains dispatch routines for create,
close, device-control, read & write.
Environment:
Kernel mode
--*/
#include "private.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, UsbSamp_EvtDeviceFileCreate)
#pragma alloc_text(PAGE, UsbSamp_EvtIoDeviceControl)
#pragma alloc_text(PAGE, UsbSamp_EvtIoRead)
#pragma alloc_text(PAGE, UsbSamp_EvtIoWrite)
#pragma alloc_text(PAGE, GetPipeFromName)
#pragma alloc_text(PAGE, ResetPipe)
#pragma alloc_text(PAGE, ResetDevice)
#endif
VOID
UsbSamp_EvtDeviceFileCreate(
_In_ WDFDEVICE Device,
_In_ WDFREQUEST Request,
_In_ WDFFILEOBJECT FileObject
)
/*++
Routine Description:
The framework calls a driver's EvtDeviceFileCreate callback
when the framework receives an IRP_MJ_CREATE request.
The system sends this request when a user application opens the
device to perform an I/O operation, such as reading or writing a file.
This callback is called synchronously, in the context of the thread
that created the IRP_MJ_CREATE request.
Arguments:
Device - Handle to a framework device object.
FileObject - Pointer to fileobject that represents the open handle.
CreateParams - copy of the create IO_STACK_LOCATION
Return Value:
NT status code
--*/
{
NTSTATUS status = STATUS_UNSUCCESSFUL;
PUNICODE_STRING fileName;
PFILE_CONTEXT pFileContext;
PDEVICE_CONTEXT pDevContext;
WDFUSBPIPE pipe;
UsbSamp_DbgPrint(3, ("EvtDeviceFileCreate - begins\n"));
PAGED_CODE();
//
// initialize variables
//
pDevContext = GetDeviceContext(Device);
pFileContext = GetFileContext(FileObject);
fileName = WdfFileObjectGetFileName(FileObject);
if (0 == fileName->Length) {
//
// opening a device as opposed to pipe.
//
status = STATUS_SUCCESS;
}
else {
pipe = GetPipeFromName(pDevContext, fileName);
if (pipe != NULL) {
//
// found a match
//
pFileContext->Pipe = pipe;
WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(pipe);
status = STATUS_SUCCESS;
}
else {
status = STATUS_INVALID_DEVICE_REQUEST;
}
}
WdfRequestComplete(Request, status);
UsbSamp_DbgPrint(3, ("EvtDeviceFileCreate - ends\n"));
return;
}
VOID
UsbSamp_EvtIoDeviceControl(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t OutputBufferLength,
_In_ size_t InputBufferLength,
_In_ ULONG IoControlCode
)
/*++
Routine Description:
This event is called when the framework receives IRP_MJ_DEVICE_CONTROL
requests from the system.
Arguments:
Queue - Handle to the framework queue object that is associated
with the I/O request.
Request - Handle to a framework request object.
OutputBufferLength - length of the request's output buffer,
if an output buffer is available.
InputBufferLength - length of the request's input buffer,
if an input buffer is available.
IoControlCode - the driver-defined or system-defined I/O control code
(IOCTL) that is associated with the request.
Return Value:
VOID
--*/
{
WDFDEVICE device;
PVOID ioBuffer;
size_t bufLength;
NTSTATUS status;
PDEVICE_CONTEXT pDevContext;
PFILE_CONTEXT pFileContext;
ULONG length = 0;
UNREFERENCED_PARAMETER(OutputBufferLength);
UNREFERENCED_PARAMETER(InputBufferLength);
UsbSamp_DbgPrint(3, ("Entered UsbSamp_DispatchDevCtrl\n"));
//
// If your driver is at the top of its driver stack, EvtIoDeviceControl is called
// at IRQL = PASSIVE_LEVEL.
//
_IRQL_limited_to_(PASSIVE_LEVEL);
PAGED_CODE();
//
// initialize variables
//
device = WdfIoQueueGetDevice(Queue);
pDevContext = GetDeviceContext(device);
switch(IoControlCode) {
case IOCTL_USBSAMP_RESET_PIPE:
pFileContext = GetFileContext(WdfRequestGetFileObject(Request));
if (pFileContext->Pipe == NULL) {
status = STATUS_INVALID_PARAMETER;
}
else {
status = ResetPipe(pFileContext->Pipe);
}
break;
case IOCTL_USBSAMP_GET_CONFIG_DESCRIPTOR:
if (pDevContext->UsbConfigurationDescriptor) {
length = pDevContext->UsbConfigurationDescriptor->wTotalLength;
status = WdfRequestRetrieveOutputBuffer(Request, length, &ioBuffer, &bufLength);
if (!NT_SUCCESS(status)){
UsbSamp_DbgPrint(1, ("WdfRequestRetrieveInputBuffer failed\n"));
break;
}
RtlCopyMemory(ioBuffer,
pDevContext->UsbConfigurationDescriptor,
length);
status = STATUS_SUCCESS;
}
else {
status = STATUS_INVALID_DEVICE_STATE;
}
break;
case IOCTL_USBSAMP_RESET_DEVICE:
status = ResetDevice(device);
break;
default :
status = STATUS_INVALID_DEVICE_REQUEST;
break;
}
WdfRequestCompleteWithInformation(Request, status, length);
UsbSamp_DbgPrint(3, ("Exit UsbSamp_DispatchDevCtrl\n"));
return;
}
VOID
UsbSamp_EvtIoRead(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t Length
)
/*++
Routine Description:
Called by the framework when it receives Read requests.
Arguments:
Queue - Default queue handle
Request - Handle to the read/write request
Lenght - Length of the data buffer associated with the request.
The default property of the queue is to not dispatch
zero lenght read & write requests to the driver and
complete is with status success. So we will never get
a zero length request.
Return Value:
--*/
{
PFILE_CONTEXT fileContext = NULL;
WDFUSBPIPE pipe;
WDF_USB_PIPE_INFORMATION pipeInfo;
//
// If your driver is at the top of its driver stack, EvtIoRead is called
// at IRQL = PASSIVE_LEVEL.
//
_IRQL_limited_to_(PASSIVE_LEVEL);
PAGED_CODE();
//
// Get the pipe associate with this request.
//
fileContext = GetFileContext(WdfRequestGetFileObject(Request));
pipe = fileContext->Pipe;
if (pipe == NULL) {
UsbSamp_DbgPrint(1, ("pipe handle is NULL\n"));
WdfRequestCompleteWithInformation(Request, STATUS_INVALID_PARAMETER, 0);
return;
}
WDF_USB_PIPE_INFORMATION_INIT(&pipeInfo);
WdfUsbTargetPipeGetInformation(pipe, &pipeInfo);
if ((WdfUsbPipeTypeBulk == pipeInfo.PipeType) ||
(WdfUsbPipeTypeInterrupt == pipeInfo.PipeType)) {
ReadWriteBulkEndPoints(Queue, Request, (ULONG) Length, WdfRequestTypeRead);
return;
}
else if (WdfUsbPipeTypeIsochronous == pipeInfo.PipeType){
#if !defined(BUFFERED_READ_WRITE) // if doing DIRECT_IO
ReadWriteIsochEndPoints(Queue, Request, (ULONG) Length, WdfRequestTypeRead);
return;
#endif
}
UsbSamp_DbgPrint(1, ("ISO transfer is not supported for buffered I/O transfer\n"));
WdfRequestCompleteWithInformation(Request, STATUS_INVALID_DEVICE_REQUEST, 0);
return;
}
VOID
UsbSamp_EvtIoWrite(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t Length
)
/*++
Routine Description:
Called by the framework when it receives Write requests.
Arguments:
Queue - Default queue handle
Request - Handle to the read/write request
Lenght - Length of the data buffer associated with the request.
The default property of the queue is to not dispatch
zero lenght read & write requests to the driver and
complete is with status success. So we will never get
a zero length request.
Return Value:
--*/
{
PFILE_CONTEXT fileContext = NULL;
WDFUSBPIPE pipe;
WDF_USB_PIPE_INFORMATION pipeInfo;
//
// If your driver is at the top of its driver stack, EvtIoWrite is called
// at IRQL = PASSIVE_LEVEL.
//
_IRQL_limited_to_(PASSIVE_LEVEL);
PAGED_CODE();
//
// Get the pipe associate with this request.
//
fileContext = GetFileContext(WdfRequestGetFileObject(Request));
pipe = fileContext->Pipe;
if (pipe == NULL) {
UsbSamp_DbgPrint(1, ("pipe handle is NULL\n"));
WdfRequestCompleteWithInformation(Request, STATUS_INVALID_PARAMETER, 0);
return;
}
WDF_USB_PIPE_INFORMATION_INIT(&pipeInfo);
WdfUsbTargetPipeGetInformation(pipe, &pipeInfo);
if ((WdfUsbPipeTypeBulk == pipeInfo.PipeType) ||
(WdfUsbPipeTypeInterrupt == pipeInfo.PipeType)) {
ReadWriteBulkEndPoints(Queue, Request, (ULONG) Length, WdfRequestTypeWrite);
return;
}
else if (WdfUsbPipeTypeIsochronous == pipeInfo.PipeType){
#if !defined(BUFFERED_READ_WRITE) // if doing DIRECT_IO
ReadWriteIsochEndPoints(Queue, Request, (ULONG) Length, WdfRequestTypeWrite);
return;
#endif
}
UsbSamp_DbgPrint(1, ("ISO transfer is not supported for buffered I/O transfer\n"));
WdfRequestCompleteWithInformation(Request, STATUS_INVALID_DEVICE_REQUEST, 0);
return;
}
WDFUSBPIPE
GetPipeFromName(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ PUNICODE_STRING FileName
)
/*++
Routine Description:
This routine will pass the string pipe name and
fetch the pipe handle.
Arguments:
DeviceContext - pointer to Device Context
FileName - string pipe name
Return Value:
The device extension maintains a pipe context for
the pipes on 82930 board.
--*/
{
LONG ix;
ULONG uval;
ULONG nameLength;
ULONG umultiplier;
WDFUSBPIPE pipe = NULL;
PAGED_CODE();
//
// typedef WCHAR *PWSTR;
//
nameLength = (FileName->Length / sizeof(WCHAR));
UsbSamp_DbgPrint(3, ("UsbSamp_PipeWithName - begins\n"));
if (nameLength != 0) {
UsbSamp_DbgPrint(3, ("Filename = %wZ nameLength = %d\n", FileName, nameLength));
//
// Parse the pipe#
//
ix = nameLength - 1;
// if last char isn't digit, decrement it.
while((ix > -1) &&
((FileName->Buffer[ix] < (WCHAR) '0') ||
(FileName->Buffer[ix] > (WCHAR) '9'))) {
ix--;
}
if (ix > -1) {
uval = 0;
umultiplier = 1;
// traversing least to most significant digits.
while((ix > -1) &&
(FileName->Buffer[ix] >= (WCHAR) '0') &&
(FileName->Buffer[ix] <= (WCHAR) '9')) {
uval += (umultiplier *
(ULONG) (FileName->Buffer[ix] - (WCHAR) '0'));
ix--;
umultiplier *= 10;
}
pipe = WdfUsbInterfaceGetConfiguredPipe(
DeviceContext->UsbInterface,
(UCHAR)uval, //PipeIndex,
NULL
);
}
}
UsbSamp_DbgPrint(3, ("UsbSamp_PipeWithName - ends\n"));
return pipe;
}
NTSTATUS
ResetPipe(
_In_ WDFUSBPIPE Pipe
)
/*++
Routine Description:
This routine resets the pipe.
Arguments:
Pipe - framework pipe handle
Return Value:
NT status value
--*/
{
NTSTATUS status;
PAGED_CODE();
//
// This routine synchronously submits a URB_FUNCTION_RESET_PIPE
// request down the stack.
//
status = WdfUsbTargetPipeResetSynchronously(Pipe,
WDF_NO_HANDLE, // WDFREQUEST
NULL // PWDF_REQUEST_SEND_OPTIONS
);
if (NT_SUCCESS(status)) {
UsbSamp_DbgPrint(3, ("ResetPipe - success\n"));
status = STATUS_SUCCESS;
}
else {
UsbSamp_DbgPrint(1, ("ResetPipe - failed\n"));
}
return status;
}
VOID
StopAllPipes(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
UCHAR count,i;
count = DeviceContext->NumberConfiguredPipes;
for (i = 0; i < count; i++) {
WDFUSBPIPE pipe;
pipe = WdfUsbInterfaceGetConfiguredPipe(DeviceContext->UsbInterface,
i, //PipeIndex,
NULL
);
WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(pipe),
WdfIoTargetCancelSentIo);
}
}
VOID
StartAllPipes(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
NTSTATUS status;
UCHAR count,i;
count = DeviceContext->NumberConfiguredPipes;
for (i = 0; i < count; i++) {
WDFUSBPIPE pipe;
pipe = WdfUsbInterfaceGetConfiguredPipe(DeviceContext->UsbInterface,
i, //PipeIndex,
NULL
);
status = WdfIoTargetStart(WdfUsbTargetPipeGetIoTarget(pipe));
if (!NT_SUCCESS(status)) {
UsbSamp_DbgPrint(1, ("StartAllPipes - failed pipe #%d\n", i));
}
}
}
NTSTATUS
ResetDevice(
_In_ WDFDEVICE Device
)
/*++
Routine Description:
This routine calls WdfUsbTargetDeviceResetPortSynchronously to reset the device if it's still
connected.
Arguments:
Device - Handle to a framework device
Return Value:
NT status value
--*/
{
PDEVICE_CONTEXT pDeviceContext;
NTSTATUS status;
UsbSamp_DbgPrint(3, ("ResetDevice - begins\n"));
PAGED_CODE();
pDeviceContext = GetDeviceContext(Device);
//
// A reset-device
// request will be stuck in the USB until the pending transactions
// have been canceled. Similarly, if there are pending tranasfers on the BULK
// _In_/OUT pipe cancel them.
// To work around this issue, the driver should stop the continuous reader
// (by calling WdfIoTargetStop) before resetting the device, and restart the
// continuous reader (by calling WdfIoTargetStart) after the request completes.
//
StopAllPipes(pDeviceContext);
//
// It may not be necessary to check whether device is connected before
// resetting the port.
//
status = WdfUsbTargetDeviceIsConnectedSynchronous(pDeviceContext->WdfUsbTargetDevice);
if (NT_SUCCESS(status)) {
status = WdfUsbTargetDeviceResetPortSynchronously(pDeviceContext->WdfUsbTargetDevice);
}
StartAllPipes(pDeviceContext);
UsbSamp_DbgPrint(3, ("ResetDevice - ends\n"));
return status;
}
|