[USBCCGP]
[reactos.git] / reactos / drivers / usb / usbccgp / usbccgp.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/usbccgp.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 DRIVER_ADD_DEVICE USBCCGP_AddDevice;
18
19 NTSTATUS
20 NTAPI
21 USBCCGP_AddDevice(
22 PDRIVER_OBJECT DriverObject,
23 PDEVICE_OBJECT PhysicalDeviceObject)
24 {
25 NTSTATUS Status;
26 PDEVICE_OBJECT DeviceObject;
27 PFDO_DEVICE_EXTENSION FDODeviceExtension;
28
29 // lets create the device
30 Status = IoCreateDevice(DriverObject, sizeof(FDO_DEVICE_EXTENSION), NULL, FILE_DEVICE_USB, FILE_AUTOGENERATED_DEVICE_NAME, FALSE, &DeviceObject);
31 if (!NT_SUCCESS(Status))
32 {
33 // failed to create device
34 DPRINT1("USBCCGP_AddDevice failed to create device with %x\n", Status);
35 return Status;
36 }
37
38 // get device extension
39 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
40
41 // init device extension
42 RtlZeroMemory(FDODeviceExtension, sizeof(FDO_DEVICE_EXTENSION));
43 FDODeviceExtension->Common.IsFDO = TRUE;
44 FDODeviceExtension->DriverObject = DriverObject;
45 FDODeviceExtension->PhysicalDeviceObject = PhysicalDeviceObject;
46 InitializeListHead(&FDODeviceExtension->ResetPortListHead);
47 InitializeListHead(&FDODeviceExtension->CyclePortListHead);
48 KeInitializeSpinLock(&FDODeviceExtension->Lock);
49
50 FDODeviceExtension->NextDeviceObject = IoAttachDeviceToDeviceStack(DeviceObject, PhysicalDeviceObject);
51 if (!FDODeviceExtension->NextDeviceObject)
52 {
53 // failed to attach
54 DPRINT1("USBCCGP_AddDevice failed to attach device\n");
55 IoDeleteDevice(DeviceObject);
56 return STATUS_DEVICE_REMOVED;
57 }
58
59 // set device flags
60 DeviceObject->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE;
61
62 // device is initialized
63 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
64
65 // device initialized
66 return Status;
67 }
68
69 NTSTATUS
70 NTAPI
71 USBCCGP_CreateClose(
72 PDEVICE_OBJECT DeviceObject,
73 PIRP Irp)
74 {
75 PCOMMON_DEVICE_EXTENSION DeviceExtension;
76 PFDO_DEVICE_EXTENSION FDODeviceExtension;
77
78 // get common device extension
79 DeviceExtension = (PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
80
81 // is it a fdo
82 if (DeviceExtension->IsFDO)
83 {
84 // forward and forget
85 IoSkipCurrentIrpStackLocation(Irp);
86
87 // get fdo
88 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
89
90 // call lower driver
91 return IoCallDriver(FDODeviceExtension->NextDeviceObject, Irp);
92 }
93 else
94 {
95 // pdo not supported
96 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
97 IoCompleteRequest(Irp, IO_NO_INCREMENT);
98 return STATUS_NOT_SUPPORTED;
99 }
100 }
101
102 NTSTATUS
103 NTAPI
104 USBCCGP_Dispatch(
105 PDEVICE_OBJECT DeviceObject,
106 PIRP Irp)
107 {
108 PCOMMON_DEVICE_EXTENSION DeviceExtension;
109 PIO_STACK_LOCATION IoStack;
110
111 // get common device extension
112 DeviceExtension = (PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
113
114 // get current stack location
115 IoStack = IoGetCurrentIrpStackLocation(Irp);
116
117 if (IoStack->MajorFunction == IRP_MJ_CREATE || IoStack->MajorFunction == IRP_MJ_CLOSE)
118 {
119 // dispatch to default handler
120 return USBCCGP_CreateClose(DeviceObject, Irp);
121 }
122
123 if (DeviceExtension->IsFDO)
124 {
125 // handle request for FDO
126 return FDO_Dispatch(DeviceObject, Irp);
127 }
128 else
129 {
130 // handle request for PDO
131 return PDO_Dispatch(DeviceObject, Irp);
132 }
133 }
134
135 NTSTATUS
136 NTAPI
137 DriverEntry(
138 PDRIVER_OBJECT DriverObject,
139 PUNICODE_STRING RegistryPath)
140 {
141
142 // initialize driver object
143 DPRINT("[USBCCGP] DriverEntry\n");
144 DriverObject->DriverExtension->AddDevice = USBCCGP_AddDevice;
145 DriverObject->MajorFunction[IRP_MJ_CREATE] = USBCCGP_Dispatch;
146 DriverObject->MajorFunction[IRP_MJ_CLOSE] = USBCCGP_Dispatch;
147 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = USBCCGP_Dispatch;
148 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = USBCCGP_Dispatch;
149 DriverObject->MajorFunction[IRP_MJ_POWER] = USBCCGP_Dispatch;
150 DriverObject->MajorFunction[IRP_MJ_PNP] = USBCCGP_Dispatch;
151
152 // FIMXE query GenericCompositeUSBDeviceString
153
154 return STATUS_SUCCESS;
155 }