fe788e767fe3f968aef326c42cbed00e9866ac10
[reactos.git] / reactos / drivers / usb / usbccgp / misc.c
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 /* Driver verifier */
15 IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine;
16
17 NTSTATUS
18 NTAPI
19 USBSTOR_SyncForwardIrpCompletionRoutine(
20 PDEVICE_OBJECT DeviceObject,
21 PIRP Irp,
22 PVOID Context)
23 {
24 if (Irp->PendingReturned)
25 {
26 KeSetEvent((PKEVENT)Context, IO_NO_INCREMENT, FALSE);
27 }
28 return STATUS_MORE_PROCESSING_REQUIRED;
29 }
30
31 NTSTATUS
32 NTAPI
33 USBCCGP_SyncForwardIrp(
34 PDEVICE_OBJECT DeviceObject,
35 PIRP Irp)
36 {
37 KEVENT Event;
38 NTSTATUS Status;
39
40 /* Initialize event */
41 KeInitializeEvent(&Event, NotificationEvent, FALSE);
42
43 /* Copy irp stack location */
44 IoCopyCurrentIrpStackLocationToNext(Irp);
45
46 /* Set completion routine */
47 IoSetCompletionRoutine(Irp,
48 USBSTOR_SyncForwardIrpCompletionRoutine,
49 &Event,
50 TRUE,
51 TRUE,
52 TRUE);
53
54 /* Call driver */
55 Status = IoCallDriver(DeviceObject, Irp);
56
57 /* Check if pending */
58 if (Status == STATUS_PENDING)
59 {
60 /* Wait for the request to finish */
61 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
62
63 /* Copy status code */
64 Status = Irp->IoStatus.Status;
65 }
66
67 /* Done */
68 return Status;
69 }
70
71 NTSTATUS
72 USBCCGP_SyncUrbRequest(
73 IN PDEVICE_OBJECT DeviceObject,
74 OUT PURB UrbRequest)
75 {
76 PIRP Irp;
77 PIO_STACK_LOCATION IoStack;
78 KEVENT Event;
79 NTSTATUS Status;
80
81 /* Allocate irp */
82 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
83 if (!Irp)
84 {
85 /* No memory */
86 return STATUS_INSUFFICIENT_RESOURCES;
87 }
88
89 /* Initialize event */
90 KeInitializeEvent(&Event, NotificationEvent, FALSE);
91
92 /* Get next stack location */
93 IoStack = IoGetNextIrpStackLocation(Irp);
94
95 /* Initialize stack location */
96 IoStack->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
97 IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
98 IoStack->Parameters.Others.Argument1 = (PVOID)UrbRequest;
99 IoStack->Parameters.DeviceIoControl.InputBufferLength = UrbRequest->UrbHeader.Length;
100 Irp->IoStatus.Status = STATUS_SUCCESS;
101
102 /* Setup completion routine */
103 IoSetCompletionRoutine(Irp,
104 USBSTOR_SyncForwardIrpCompletionRoutine,
105 &Event,
106 TRUE,
107 TRUE,
108 TRUE);
109
110 /* Call driver */
111 Status = IoCallDriver(DeviceObject, Irp);
112
113 /* Check if request is pending */
114 if (Status == STATUS_PENDING)
115 {
116 /* Wait for completion */
117 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
118
119 /* Update status */
120 Status = Irp->IoStatus.Status;
121 }
122
123 /* Free irp */
124 IoFreeIrp(Irp);
125
126 /* Done */
127 return Status;
128 }
129
130 PVOID
131 AllocateItem(
132 IN POOL_TYPE PoolType,
133 IN ULONG ItemSize)
134 {
135 /* Allocate item */
136 PVOID Item = ExAllocatePoolWithTag(PoolType, ItemSize, USBCCPG_TAG);
137
138 if (Item)
139 {
140 /* Zero item */
141 RtlZeroMemory(Item, ItemSize);
142 }
143
144 /* Return element */
145 return Item;
146 }
147
148 VOID
149 FreeItem(
150 IN PVOID Item)
151 {
152 /* Free item */
153 ExFreePoolWithTag(Item, USBCCPG_TAG);
154 }
155
156 VOID
157 DumpFunctionDescriptor(
158 IN PUSBC_FUNCTION_DESCRIPTOR FunctionDescriptor,
159 IN ULONG FunctionDescriptorCount)
160 {
161 ULONG Index, SubIndex;
162
163
164 DPRINT1("FunctionCount %lu\n", FunctionDescriptorCount);
165 for(Index = 0; Index < FunctionDescriptorCount; Index++)
166 {
167 DPRINT1("Function %lu\n", Index);
168 DPRINT1("FunctionNumber %lu\n", FunctionDescriptor[Index].FunctionNumber);
169 DPRINT1("HardwareId %S\n", FunctionDescriptor[Index].HardwareId.Buffer);
170 DPRINT1("CompatibleId %S\n", FunctionDescriptor[Index].CompatibleId.Buffer);
171 DPRINT1("FunctionDescription %wZ\n", &FunctionDescriptor[Index].FunctionDescription);
172 DPRINT1("NumInterfaces %lu\n", FunctionDescriptor[Index].NumberOfInterfaces);
173
174 for(SubIndex = 0; SubIndex < FunctionDescriptor[Index].NumberOfInterfaces; SubIndex++)
175 {
176 DPRINT1(" Index %lu Interface %p\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]);
177 DPRINT1(" Index %lu Interface InterfaceNumber %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceNumber);
178 DPRINT1(" Index %lu Interface Alternate %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bAlternateSetting );
179 DPRINT1(" Index %lu bLength %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bLength);
180 DPRINT1(" Index %lu bDescriptorType %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bDescriptorType);
181 DPRINT1(" Index %lu bInterfaceNumber %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceNumber);
182 DPRINT1(" Index %lu bAlternateSetting %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bAlternateSetting);
183 DPRINT1(" Index %lu bNumEndpoints %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bNumEndpoints);
184 DPRINT1(" Index %lu bInterfaceClass %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceClass);
185 DPRINT1(" Index %lu bInterfaceSubClass %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceSubClass);
186 DPRINT1(" Index %lu bInterfaceProtocol %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->bInterfaceProtocol);
187 DPRINT1(" Index %lu iInterface %x\n", SubIndex, FunctionDescriptor[Index].InterfaceDescriptorList[SubIndex]->iInterface);
188 }
189 }
190 }