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