a5ee0be59e7ed2da7cc5b3ad1241e91ba92e1707
[reactos.git] / reactos / drivers / bus / pci / pci.c
1 /* $Id: pci.c,v 1.6 2004/02/10 16:22:55 navaraf Exp $
2 *
3 * PROJECT: ReactOS PCI Bus driver
4 * FILE: pci.c
5 * PURPOSE: Driver entry
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * UPDATE HISTORY:
8 * 10-09-2001 CSH Created
9 */
10
11 #include <ddk/ntddk.h>
12
13 #include "pcidef.h"
14 #include "pci.h"
15
16 #define NDEBUG
17 #include <debug.h>
18
19
20 #ifdef ALLOC_PRAGMA
21
22 // Make the initialization routines discardable, so that they
23 // don't waste space
24
25 #pragma alloc_text(init, DriverEntry)
26
27 #endif /* ALLOC_PRAGMA */
28
29 /*** PUBLIC ******************************************************************/
30
31
32 /*** PRIVATE *****************************************************************/
33
34 NTSTATUS
35 STDCALL
36 PciDispatchDeviceControl(
37 IN PDEVICE_OBJECT DeviceObject,
38 IN PIRP Irp)
39 {
40 PIO_STACK_LOCATION IrpSp;
41 NTSTATUS Status;
42
43 DPRINT("Called. IRP is at (0x%X)\n", Irp);
44
45 Irp->IoStatus.Information = 0;
46
47 IrpSp = IoGetCurrentIrpStackLocation(Irp);
48 switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
49 default:
50 DPRINT("Unknown IOCTL 0x%X\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
51 Status = STATUS_NOT_IMPLEMENTED;
52 break;
53 }
54
55 if (Status != STATUS_PENDING) {
56 Irp->IoStatus.Status = Status;
57
58 DPRINT("Completing IRP at 0x%X\n", Irp);
59
60 IoCompleteRequest(Irp, IO_NO_INCREMENT);
61 }
62
63 DPRINT("Leaving. Status 0x%X\n", Status);
64
65 return Status;
66 }
67
68
69 NTSTATUS
70 STDCALL
71 PciPnpControl(
72 IN PDEVICE_OBJECT DeviceObject,
73 IN PIRP Irp)
74 /*
75 * FUNCTION: Handle Plug and Play IRPs
76 * ARGUMENTS:
77 * DeviceObject = Pointer to PDO or FDO
78 * Irp = Pointer to IRP that should be handled
79 * RETURNS:
80 * Status
81 */
82 {
83 PCOMMON_DEVICE_EXTENSION DeviceExtension;
84 NTSTATUS Status;
85
86 DeviceExtension = (PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
87
88 DPRINT("IsFDO %d\n", DeviceExtension->IsFDO);
89
90 if (DeviceExtension->IsFDO) {
91 Status = FdoPnpControl(DeviceObject, Irp);
92 } else {
93 Status = PdoPnpControl(DeviceObject, Irp);
94 }
95
96 return Status;
97 }
98
99
100 NTSTATUS
101 STDCALL
102 PciPowerControl(
103 IN PDEVICE_OBJECT DeviceObject,
104 IN PIRP Irp)
105 /*
106 * FUNCTION: Handle power management IRPs
107 * ARGUMENTS:
108 * DeviceObject = Pointer to PDO or FDO
109 * Irp = Pointer to IRP that should be handled
110 * RETURNS:
111 * Status
112 */
113 {
114 PCOMMON_DEVICE_EXTENSION DeviceExtension;
115 NTSTATUS Status;
116
117 DeviceExtension = (PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
118
119 if (DeviceExtension->IsFDO) {
120 Status = FdoPowerControl(DeviceObject, Irp);
121 } else {
122 Status = PdoPowerControl(DeviceObject, Irp);
123 }
124
125 return Status;
126 }
127
128
129 NTSTATUS
130 STDCALL
131 PciAddDevice(
132 IN PDRIVER_OBJECT DriverObject,
133 IN PDEVICE_OBJECT PhysicalDeviceObject)
134 {
135 PFDO_DEVICE_EXTENSION DeviceExtension;
136 PDEVICE_OBJECT Fdo;
137 NTSTATUS Status;
138
139 DPRINT("Called\n");
140
141 Status = IoCreateDevice(DriverObject, sizeof(FDO_DEVICE_EXTENSION),
142 NULL, FILE_DEVICE_BUS_EXTENDER, FILE_DEVICE_SECURE_OPEN, TRUE, &Fdo);
143 if (!NT_SUCCESS(Status)) {
144 DPRINT("IoCreateDevice() failed with status 0x%X\n", Status);
145 return Status;
146 }
147
148 DeviceExtension = (PFDO_DEVICE_EXTENSION)Fdo->DeviceExtension;
149
150 RtlZeroMemory(DeviceExtension, sizeof(FDO_DEVICE_EXTENSION));
151
152 DeviceExtension->Common.IsFDO = TRUE;
153
154 DeviceExtension->Ldo =
155 IoAttachDeviceToDeviceStack(Fdo, PhysicalDeviceObject);
156
157 DeviceExtension->State = dsStopped;
158
159 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
160
161 //Fdo->Flags |= DO_POWER_PAGABLE;
162
163 DPRINT("Done AddDevice\n");
164
165 return STATUS_SUCCESS;
166 }
167
168
169 NTSTATUS
170 STDCALL
171 DriverEntry(
172 IN PDRIVER_OBJECT DriverObject,
173 IN PUNICODE_STRING RegistryPath)
174 {
175 DbgPrint("Peripheral Component Interconnect Bus Driver\n");
176
177 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = PciDispatchDeviceControl;
178 DriverObject->MajorFunction[IRP_MJ_PNP] = PciPnpControl;
179 DriverObject->MajorFunction[IRP_MJ_POWER] = PciPowerControl;
180 DriverObject->DriverExtension->AddDevice = PciAddDevice;
181
182 return STATUS_SUCCESS;
183 }
184
185
186 BOOLEAN
187 PciCreateUnicodeString(
188 PUNICODE_STRING Destination,
189 PWSTR Source,
190 POOL_TYPE PoolType)
191 {
192 ULONG Length;
193
194 if (!Source)
195 {
196 RtlInitUnicodeString(Destination, NULL);
197 return TRUE;
198 }
199
200 Length = (wcslen(Source) + 1) * sizeof(WCHAR);
201
202 Destination->Buffer = ExAllocatePool(PoolType, Length);
203
204 if (Destination->Buffer == NULL)
205 {
206 return FALSE;
207 }
208
209 RtlCopyMemory(Destination->Buffer, Source, Length);
210
211 Destination->MaximumLength = Length;
212
213 Destination->Length = Length - sizeof(WCHAR);
214
215 return TRUE;
216 }
217
218 /* EOF */