xref: /reactos/drivers/usb/usbccgp/misc.c (revision 9d3c3a75)
1 /*
2  * PROJECT:     ReactOS Universal Serial Bus Bulk Enhanced Host Controller Interface
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        drivers/usb/usbccgp/misc.c
5  * PURPOSE:     USB  device driver.
6  * PROGRAMMERS:
7  *              Michael Martin (michael.martin@reactos.org)
8  *              Johannes Anderwald (johannes.anderwald@reactos.org)
9  *              Cameron Gutman
10  */
11 
12 #include "usbccgp.h"
13 
14 #define NDEBUG
15 #include <debug.h>
16 
17 /* Driver verifier */
18 IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine;
19 
20 NTSTATUS
21 NTAPI
22 USBSTOR_SyncForwardIrpCompletionRoutine(
23     PDEVICE_OBJECT DeviceObject,
24     PIRP Irp,
25     PVOID Context)
26 {
27     if (Irp->PendingReturned)
28     {
29         KeSetEvent((PKEVENT)Context, IO_NO_INCREMENT, FALSE);
30     }
31     return STATUS_MORE_PROCESSING_REQUIRED;
32 }
33 
34 NTSTATUS
35 USBCCGP_SyncUrbRequest(
36     IN PDEVICE_OBJECT DeviceObject,
37     OUT PURB UrbRequest)
38 {
39     PIRP Irp;
40     PIO_STACK_LOCATION IoStack;
41     KEVENT Event;
42     NTSTATUS Status;
43 
44     /* Allocate irp */
45     Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
46     if (!Irp)
47     {
48         /* No memory */
49         return STATUS_INSUFFICIENT_RESOURCES;
50     }
51 
52     /* Initialize event */
53     KeInitializeEvent(&Event, NotificationEvent, FALSE);
54 
55     /* Get next stack location */
56     IoStack = IoGetNextIrpStackLocation(Irp);
57 
58     /* Initialize stack location */
59     IoStack->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
60     IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
61     IoStack->Parameters.Others.Argument1 = (PVOID)UrbRequest;
62     IoStack->Parameters.DeviceIoControl.InputBufferLength = UrbRequest->UrbHeader.Length;
63     Irp->IoStatus.Status = STATUS_SUCCESS;
64 
65     /* Setup completion routine */
66     IoSetCompletionRoutine(Irp,
67                            USBSTOR_SyncForwardIrpCompletionRoutine,
68                            &Event,
69                            TRUE,
70                            TRUE,
71                            TRUE);
72 
73     /* Call driver */
74     Status = IoCallDriver(DeviceObject, Irp);
75 
76     /* Check if request is pending */
77     if (Status == STATUS_PENDING)
78     {
79         /* Wait for completion */
80         KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
81 
82         /* Update status */
83         Status = Irp->IoStatus.Status;
84     }
85 
86     /* Free irp */
87     IoFreeIrp(Irp);
88 
89     /* Done */
90     return Status;
91 }
92 
93 PVOID
94 AllocateItem(
95     IN POOL_TYPE PoolType,
96     IN ULONG ItemSize)
97 {
98     /* Allocate item */
99     PVOID Item = ExAllocatePoolWithTag(PoolType, ItemSize, USBCCPG_TAG);
100 
101     if (Item)
102     {
103         /* Zero item */
104         RtlZeroMemory(Item, ItemSize);
105     }
106 
107     /* Return element */
108     return Item;
109 }
110 
111 VOID
112 FreeItem(
113     IN PVOID Item)
114 {
115     /* Free item */
116     ExFreePoolWithTag(Item, USBCCPG_TAG);
117 }
118 
119 VOID
120 DumpFunctionDescriptor(
121     IN PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor,
122     IN ULONG FunctionDescriptorCount)
123 {
124     ULONG Index, SubIndex;
125 
126 
127     DPRINT("FunctionCount %lu\n", FunctionDescriptorCount);
128     for (Index = 0; Index < FunctionDescriptorCount; Index++)
129     {
130         DPRINT("Function %lu\n", Index);
131         DPRINT("FunctionNumber %lu\n", FunctionDescriptor[Index].FunctionNumber);
132         DPRINT("HardwareId %S\n", FunctionDescriptor[Index].HardwareId.Buffer);
133         DPRINT("CompatibleId %S\n", FunctionDescriptor[Index].CompatibleId.Buffer);
134         DPRINT("FunctionDescription %wZ\n", &FunctionDescriptor[Index].FunctionDescription);
135         DPRINT("NumInterfaces %lu\n", FunctionDescriptor[Index].NumberOfInterfaces);
136 
137         for(SubIndex = 0; SubIndex < FunctionDescriptor[Index].NumberOfInterfaces; SubIndex++)
138         {
139             DPRINT(" Index %lu Interface %p\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]);
140             DPRINT(" Index %lu Interface InterfaceNumber %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceNumber);
141             DPRINT(" Index %lu Interface Alternate %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bAlternateSetting );
142             DPRINT(" Index %lu bLength %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bLength);
143             DPRINT(" Index %lu bDescriptorType %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bDescriptorType);
144             DPRINT(" Index %lu bInterfaceNumber %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceNumber);
145             DPRINT(" Index %lu bAlternateSetting %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bAlternateSetting);
146             DPRINT(" Index %lu bNumEndpoints %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bNumEndpoints);
147             DPRINT(" Index %lu bInterfaceClass %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceClass);
148             DPRINT(" Index %lu bInterfaceSubClass %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceSubClass);
149             DPRINT(" Index %lu bInterfaceProtocol %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceProtocol);
150             DPRINT(" Index %lu iInterface %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->iInterface);
151         }
152     }
153 }
154