[SHELL32]
[reactos.git] / reactos / drivers / usb / usbhub / usbhub.c
1 /*
2 * ReactOS USB hub driver
3 * Copyright (C) 2004 Aleksey Bragin
4 * (C) 2005 Mark Tempel
5 * (C) 2005 Herv� Poussineau
6 * (C) 2010 Michael Martin
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24 /* INCLUDES *******************************************************************/
25 //#define NDEBUG
26 #include "usbhub.h"
27
28 /* PUBLIC AND PRIVATE FUNCTIONS ***********************************************/
29
30 NTSTATUS NTAPI
31 UsbhubAddDevice(
32 IN PDRIVER_OBJECT DriverObject,
33 IN PDEVICE_OBJECT Pdo)
34 {
35 PDEVICE_OBJECT Fdo;
36 PHUB_DEVICE_EXTENSION DeviceExtension;
37 NTSTATUS Status;
38
39 Status = IoCreateDevice(DriverObject,
40 sizeof(HUB_DEVICE_EXTENSION),
41 NULL, /* DeviceName */
42 FILE_DEVICE_BUS_EXTENDER,
43 FILE_AUTOGENERATED_DEVICE_NAME,
44 FALSE,
45 &Fdo);
46
47 if (!NT_SUCCESS(Status))
48 {
49 DPRINT1("Usbhub: IoCreateDevice() failed with status 0x%08lx\n", Status);
50 return Status;
51 }
52
53 // zerofill device extension
54 DeviceExtension = (PHUB_DEVICE_EXTENSION)Fdo->DeviceExtension;
55 RtlZeroMemory(DeviceExtension, sizeof(HUB_DEVICE_EXTENSION));
56
57 DeviceExtension->IsFDO = TRUE;
58 Fdo->Flags |= DO_POWER_PAGABLE;
59 //Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice);
60 DeviceExtension->LowerDevice = IoAttachDeviceToDeviceStack(Fdo, Pdo);
61 if (!NT_SUCCESS(Status))
62 {
63 DPRINT("Usbhub: IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status);
64 IoDeleteDevice(Fdo);
65 return Status;
66 }
67 Fdo->Flags |= DO_BUFFERED_IO;
68
69 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
70
71 return STATUS_SUCCESS;
72 }
73
74 static NTSTATUS NTAPI
75 IrpStub(
76 IN PDEVICE_OBJECT DeviceObject,
77 IN PIRP Irp)
78 {
79 NTSTATUS Status;
80
81 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
82 {
83 DPRINT1("Usbhub: FDO stub for major function 0x%lx\n",
84 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
85 return ForwardIrpAndForget(DeviceObject, Irp);
86 }
87 else
88 {
89 /* We can't forward request to the lower driver, because
90 * we are a Pdo, so we don't have lower driver...
91 */
92 DPRINT1("Usbhub: PDO stub for major function 0x%lx\n",
93 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
94 #ifndef NDEBUG
95 DbgBreakPoint();
96 #endif
97 }
98
99 Status = Irp->IoStatus.Status;
100 IoCompleteRequest(Irp, IO_NO_INCREMENT);
101 return Status;
102 }
103
104 static NTSTATUS NTAPI
105 DispatchDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
106 {
107 DPRINT1("Usbhub: DispatchDeviceControl\n");
108 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
109 return UsbhubDeviceControlFdo(DeviceObject, Irp);
110 else
111 return IrpStub(DeviceObject, Irp);
112 }
113
114 static NTSTATUS NTAPI
115 DispatchInternalDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
116 {
117 DPRINT1("Usbhub: DispatchInternalDeviceControl\n");
118 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
119 return IrpStub(DeviceObject, Irp);
120 else
121 return UsbhubInternalDeviceControlPdo(DeviceObject, Irp);
122 }
123
124 static NTSTATUS NTAPI
125 DispatchPnp(PDEVICE_OBJECT DeviceObject, PIRP Irp)
126 {
127 DPRINT1("Usbhub: DispatchPnp\n");
128 if (((PHUB_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
129 return UsbhubPnpFdo(DeviceObject, Irp);
130 else
131 return UsbhubPnpPdo(DeviceObject, Irp);
132 }
133
134 /*
135 * Standard DriverEntry method.
136 */
137 NTSTATUS NTAPI
138 DriverEntry(
139 IN PDRIVER_OBJECT DriverObject,
140 IN PUNICODE_STRING RegistryPath)
141 {
142 ULONG i;
143
144 DriverObject->DriverExtension->AddDevice = UsbhubAddDevice;
145 DPRINT1("Usbhub: DriverEntry\n");
146
147 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
148 DriverObject->MajorFunction[i] = IrpStub;
149
150 DriverObject->MajorFunction[IRP_MJ_CREATE] = UsbhubCreate;
151 DriverObject->MajorFunction[IRP_MJ_CLOSE] = UsbhubClose;
152 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = UsbhubCleanup;
153 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDeviceControl;
154 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = DispatchInternalDeviceControl;
155 DriverObject->MajorFunction[IRP_MJ_PNP] = DispatchPnp;
156
157 return STATUS_SUCCESS;
158 }
159