[LIBUSB] Add additional operator new/delete
[reactos.git] / sdk / lib / drivers / libusb / libusb.cpp
1 /*
2 * PROJECT: ReactOS Universal Serial Bus Bulk Driver Library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/drivers/libusb/libusb.cpp
5 * PURPOSE: USB Common Driver Library.
6 * PROGRAMMERS:
7 * Michael Martin (michael.martin@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
9 */
10
11 #include "libusb.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 //
17 // driver verifier
18 //
19 DRIVER_ADD_DEVICE USBLIB_AddDevice;
20
21 PVOID
22 __cdecl
23 operator new(
24 size_t iSize,
25 POOL_TYPE poolType,
26 ULONG tag)
27 {
28 PVOID result = ExAllocatePoolWithTag(poolType, iSize, tag);
29 if (result) {
30 RtlZeroMemory(result, iSize);
31 }
32 return result;
33 }
34
35 void
36 __cdecl
37 operator delete(
38 PVOID pVoid)
39 {
40 if (pVoid) ExFreePool(pVoid);
41 }
42
43 void
44 __cdecl
45 operator delete(
46 PVOID pVoid, UINT_PTR)
47 {
48 if (pVoid) ExFreePool(pVoid);
49 }
50
51 extern
52 "C"
53 {
54 NTSTATUS
55 NTAPI
56 USBLIB_AddDevice(
57 PDRIVER_OBJECT DriverObject,
58 PDEVICE_OBJECT PhysicalDeviceObject)
59 {
60 NTSTATUS Status;
61 PHCDCONTROLLER HcdController;
62
63 DPRINT("USBLIB_AddDevice\n");
64
65 /* first create the controller object */
66 Status = CreateHCDController(&HcdController);
67 if (!NT_SUCCESS(Status))
68 {
69 /* failed to create hcd */
70 DPRINT1("AddDevice: Failed to create hcd with %x\n", Status);
71 return Status;
72 }
73
74 /* initialize the hcd */
75 Status = HcdController->Initialize(NULL, // FIXME
76 DriverObject,
77 PhysicalDeviceObject);
78
79 /* check for success */
80 if (!NT_SUCCESS(Status))
81 {
82 /* failed to initialize device */
83 DPRINT1("AddDevice: failed to initialize\n");
84
85 /* release object */
86 HcdController->Release();
87 }
88
89 return Status;
90
91 }
92 }
93
94 extern
95 "C"
96 {
97 NTSTATUS
98 NTAPI
99 USBLIB_Dispatch(
100 PDEVICE_OBJECT DeviceObject,
101 PIRP Irp)
102 {
103 PCOMMON_DEVICE_EXTENSION DeviceExtension;
104 PIO_STACK_LOCATION IoStack;
105 NTSTATUS Status;
106
107 //
108 // get common device extension
109 //
110 DeviceExtension = (PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
111
112 //
113 // get current stack location
114 //
115 IoStack = IoGetCurrentIrpStackLocation(Irp);
116
117 //
118 // sanity checks
119 //
120 PC_ASSERT(DeviceExtension->Dispatcher);
121
122 switch(IoStack->MajorFunction)
123 {
124 case IRP_MJ_PNP:
125 {
126 //
127 // dispatch pnp
128 //
129 return DeviceExtension->Dispatcher->HandlePnp(DeviceObject, Irp);
130 }
131
132 case IRP_MJ_POWER:
133 {
134 //
135 // dispatch power
136 //
137 return DeviceExtension->Dispatcher->HandlePower(DeviceObject, Irp);
138 }
139 case IRP_MJ_INTERNAL_DEVICE_CONTROL:
140 case IRP_MJ_DEVICE_CONTROL:
141 {
142 //
143 // dispatch io control
144 //
145 return DeviceExtension->Dispatcher->HandleDeviceControl(DeviceObject, Irp);
146 }
147 case IRP_MJ_SYSTEM_CONTROL:
148 {
149 //
150 // dispatch system control
151 //
152 return DeviceExtension->Dispatcher->HandleSystemControl(DeviceObject, Irp);
153 }
154 default:
155 {
156 DPRINT1("USBLIB_Dispatch> Major %lu Minor %lu unhandeled\n", IoStack->MajorFunction, IoStack->MinorFunction);
157 Status = STATUS_SUCCESS;
158 }
159 }
160
161 //
162 // complete request
163 //
164 Irp->IoStatus.Information = 0;
165 Irp->IoStatus.Status = Status;
166 IoCompleteRequest(Irp, IO_NO_INCREMENT);
167
168 return Status;
169 }
170 }