174612d94bcc2d641222340111e43aaefb11569f
[reactos.git] / 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 FDODeviceExtension->NextDeviceObject = IoAttachDeviceToDeviceStack(DeviceObject, PhysicalDeviceObject);
47 if (!FDODeviceExtension->NextDeviceObject)
48 {
49 // failed to attach
50 DPRINT1("USBCCGP_AddDevice failed to attach device\n");
51 IoDeleteDevice(DeviceObject);
52 return STATUS_DEVICE_REMOVED;
53 }
54
55 // set device flags
56 DeviceObject->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE;
57
58 // device is initialized
59 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
60
61 // device initialized
62 return Status;
63 }
64
65 NTSTATUS
66 NTAPI
67 USBCCGP_CreateClose(
68 PDEVICE_OBJECT DeviceObject,
69 PIRP Irp)
70 {
71 PCOMMON_DEVICE_EXTENSION DeviceExtension;
72 PFDO_DEVICE_EXTENSION FDODeviceExtension;
73
74 // get common device extension
75 DeviceExtension = (PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
76
77 // is it a fdo
78 if (DeviceExtension->IsFDO)
79 {
80 // forward and forget
81 IoSkipCurrentIrpStackLocation(Irp);
82
83 // get fdo
84 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
85
86 // call lower driver
87 return IoCallDriver(FDODeviceExtension->NextDeviceObject, Irp);
88 }
89 else
90 {
91 // pdo not supported
92 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
93 IoCompleteRequest(Irp, IO_NO_INCREMENT);
94 return STATUS_NOT_SUPPORTED;
95 }
96 }
97
98 NTSTATUS
99 NTAPI
100 USBCCGP_Dispatch(
101 PDEVICE_OBJECT DeviceObject,
102 PIRP Irp)
103 {
104 PCOMMON_DEVICE_EXTENSION DeviceExtension;
105 PIO_STACK_LOCATION IoStack;
106
107 // get common device extension
108 DeviceExtension = (PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
109
110 // get current stack location
111 IoStack = IoGetCurrentIrpStackLocation(Irp);
112
113 if (IoStack->MajorFunction == IRP_MJ_CREATE || IoStack->MajorFunction == IRP_MJ_CLOSE)
114 {
115 // dispatch to default handler
116 return USBCCGP_CreateClose(DeviceObject, Irp);
117 }
118
119 if (DeviceExtension->IsFDO)
120 {
121 // handle request for FDO
122 return FDO_Dispatch(DeviceObject, Irp);
123 }
124 else
125 {
126 // handle request for PDO
127 return PDO_Dispatch(DeviceObject, Irp);
128 }
129 }
130
131 NTSTATUS
132 NTAPI
133 DriverEntry(
134 PDRIVER_OBJECT DriverObject,
135 PUNICODE_STRING RegistryPath)
136 {
137
138 // initialize driver object
139 DPRINT1("[USBCCGP] DriverEntry\n");
140 DriverObject->DriverExtension->AddDevice = USBCCGP_AddDevice;
141 DriverObject->MajorFunction[IRP_MJ_CREATE] = USBCCGP_Dispatch;
142 DriverObject->MajorFunction[IRP_MJ_CLOSE] = USBCCGP_Dispatch;
143 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = USBCCGP_Dispatch;
144 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = USBCCGP_Dispatch;
145 DriverObject->MajorFunction[IRP_MJ_POWER] = USBCCGP_Dispatch;
146 DriverObject->MajorFunction[IRP_MJ_PNP] = USBCCGP_Dispatch;
147
148 // FIMXE query GenericCompositeUSBDeviceString
149
150 return STATUS_SUCCESS;
151 }