[USBCCGP]
[reactos.git] / 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 //
15 // driver verifier
16 //
17 IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine;
18
19 NTSTATUS
20 NTAPI
21 USBSTOR_SyncForwardIrpCompletionRoutine(
22 PDEVICE_OBJECT DeviceObject,
23 PIRP Irp,
24 PVOID Context)
25 {
26 if (Irp->PendingReturned)
27 {
28 KeSetEvent((PKEVENT)Context, IO_NO_INCREMENT, FALSE);
29 }
30 return STATUS_MORE_PROCESSING_REQUIRED;
31 }
32
33 NTSTATUS
34 NTAPI
35 USBCCGP_SyncForwardIrp(
36 PDEVICE_OBJECT DeviceObject,
37 PIRP Irp)
38 {
39 KEVENT Event;
40 NTSTATUS Status;
41
42 //
43 // initialize event
44 //
45 KeInitializeEvent(&Event, NotificationEvent, FALSE);
46
47 //
48 // copy irp stack location
49 //
50 IoCopyCurrentIrpStackLocationToNext(Irp);
51
52 //
53 // set completion routine
54 //
55 IoSetCompletionRoutine(Irp, USBSTOR_SyncForwardIrpCompletionRoutine, &Event, TRUE, TRUE, TRUE);
56
57
58 //
59 // call driver
60 //
61 Status = IoCallDriver(DeviceObject, Irp);
62
63 //
64 // check if pending
65 //
66 if (Status == STATUS_PENDING)
67 {
68 //
69 // wait for the request to finish
70 //
71 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
72
73 //
74 // copy status code
75 //
76 Status = Irp->IoStatus.Status;
77 }
78
79 //
80 // done
81 //
82 return Status;
83 }
84
85 NTSTATUS
86 USBCCGP_SyncUrbRequest(
87 IN PDEVICE_OBJECT DeviceObject,
88 OUT PURB UrbRequest)
89 {
90 PIRP Irp;
91 PIO_STACK_LOCATION IoStack;
92 KEVENT Event;
93 NTSTATUS Status;
94
95 //
96 // allocate irp
97 //
98 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
99 if (!Irp)
100 {
101 //
102 // no memory
103 //
104 return STATUS_INSUFFICIENT_RESOURCES;
105 }
106
107 //
108 // initialize event
109 //
110 KeInitializeEvent(&Event, NotificationEvent, FALSE);
111
112
113 //
114 // get next stack location
115 //
116 IoStack = IoGetNextIrpStackLocation(Irp);
117
118 //
119 // initialize stack location
120 //
121 IoStack->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
122 IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
123 IoStack->Parameters.Others.Argument1 = (PVOID)UrbRequest;
124 IoStack->Parameters.DeviceIoControl.InputBufferLength = UrbRequest->UrbHeader.Length;
125 Irp->IoStatus.Status = STATUS_SUCCESS;
126
127 //
128 // setup completion routine
129 //
130 IoSetCompletionRoutine(Irp, USBSTOR_SyncForwardIrpCompletionRoutine, &Event, TRUE, TRUE, TRUE);
131
132 //
133 // call driver
134 //
135 Status = IoCallDriver(DeviceObject, Irp);
136
137 //
138 // check if request is pending
139 //
140 if (Status == STATUS_PENDING)
141 {
142 //
143 // wait for completion
144 //
145 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
146
147 //
148 // update status
149 //
150 Status = Irp->IoStatus.Status;
151 }
152
153 //
154 // free irp
155 //
156 IoFreeIrp(Irp);
157
158 //
159 // done
160 //
161 return Status;
162 }
163
164 PVOID
165 AllocateItem(
166 IN POOL_TYPE PoolType,
167 IN ULONG ItemSize)
168 {
169 //
170 // allocate item
171 //
172 PVOID Item = ExAllocatePoolWithTag(PoolType, ItemSize, USBCCPG_TAG);
173
174 if (Item)
175 {
176 //
177 // zero item
178 //
179 RtlZeroMemory(Item, ItemSize);
180 }
181
182 //
183 // return element
184 //
185 return Item;
186 }
187
188 VOID
189 FreeItem(
190 IN PVOID Item)
191 {
192 //
193 // free item
194 //
195 ExFreePoolWithTag(Item, USBCCPG_TAG);
196 }
197