2 * PROJECT: ReactOS Universal Serial Bus Bulk Storage Driver
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: USB block storage device driver.
5 * COPYRIGHT: 2005-2006 James Tabor
6 * 2011-2012 Michael Martin (michael.martin@reactos.org)
7 * 2011-2013 Johannes Anderwald (johannes.anderwald@reactos.org)
19 IN PDRIVER_OBJECT DriverObject
,
20 IN PDEVICE_OBJECT PhysicalDeviceObject
)
23 PDEVICE_OBJECT DeviceObject
;
24 PFDO_DEVICE_EXTENSION DeviceExtension
;
26 Status
= IoCreateDevice(DriverObject
, sizeof(FDO_DEVICE_EXTENSION
), 0, FILE_DEVICE_BUS_EXTENDER
, FILE_AUTOGENERATED_DEVICE_NAME
| FILE_DEVICE_SECURE_OPEN
, FALSE
, &DeviceObject
);
27 if (!NT_SUCCESS(Status
))
29 DPRINT1("USBSTOR_AddDevice: Failed to create FDO Status %x\n", Status
);
33 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
34 ASSERT(DeviceExtension
);
35 RtlZeroMemory(DeviceExtension
, sizeof(FDO_DEVICE_EXTENSION
));
37 // initialize device extension
38 DeviceExtension
->Common
.IsFDO
= TRUE
;
39 DeviceExtension
->FunctionalDeviceObject
= DeviceObject
;
40 DeviceExtension
->PhysicalDeviceObject
= PhysicalDeviceObject
;
41 DeviceExtension
->LowerDeviceObject
= IoAttachDeviceToDeviceStack(DeviceObject
, PhysicalDeviceObject
);
43 KeInitializeSpinLock(&DeviceExtension
->CommonLock
);
45 IoInitializeTimer(DeviceObject
, USBSTOR_TimerRoutine
, (PVOID
)DeviceExtension
);
48 if (!DeviceExtension
->LowerDeviceObject
)
50 IoDeleteDevice(DeviceObject
);
52 return STATUS_DEVICE_REMOVED
;
55 DeviceObject
->Flags
|= DO_BUFFERED_IO
| DO_POWER_PAGABLE
;
57 // device is initialized
58 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
60 return STATUS_SUCCESS
;
66 PDRIVER_OBJECT DriverObject
)
73 USBSTOR_DispatchClose(
74 PDEVICE_OBJECT DeviceObject
,
77 // function always succeeds ;)
78 DPRINT("USBSTOR_DispatchClose\n");
79 Irp
->IoStatus
.Information
= 0;
80 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
81 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
82 return STATUS_SUCCESS
;
87 USBSTOR_DispatchDeviceControl(
88 PDEVICE_OBJECT DeviceObject
,
93 Status
= USBSTOR_HandleDeviceControl(DeviceObject
, Irp
);
94 Irp
->IoStatus
.Status
= Status
;
95 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
101 USBSTOR_DispatchScsi(
102 PDEVICE_OBJECT DeviceObject
,
105 return USBSTOR_HandleInternalDeviceControl(DeviceObject
, Irp
);
110 USBSTOR_DispatchReadWrite(
111 PDEVICE_OBJECT DeviceObject
,
114 // read write ioctl is not supported
115 Irp
->IoStatus
.Information
= 0;
116 Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
;
117 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
118 return STATUS_INVALID_PARAMETER
;
124 PDEVICE_OBJECT DeviceObject
,
127 PUSBSTOR_COMMON_DEVICE_EXTENSION DeviceExtension
;
129 DeviceExtension
= (PUSBSTOR_COMMON_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
131 if (DeviceExtension
->IsFDO
)
133 return USBSTOR_FdoHandlePnp(DeviceObject
, Irp
);
137 return USBSTOR_PdoHandlePnp(DeviceObject
, Irp
);
143 USBSTOR_DispatchPower(
144 PDEVICE_OBJECT DeviceObject
,
147 PFDO_DEVICE_EXTENSION DeviceExtension
;
149 // get common device extension
150 DeviceExtension
= (PFDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
152 if (DeviceExtension
->Common
.IsFDO
)
154 PoStartNextPowerIrp(Irp
);
155 IoSkipCurrentIrpStackLocation(Irp
);
156 return PoCallDriver(DeviceExtension
->LowerDeviceObject
, Irp
);
160 PoStartNextPowerIrp(Irp
);
161 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
162 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
163 return STATUS_SUCCESS
;
170 IN PDRIVER_OBJECT DriverObject
,
171 IN PUNICODE_STRING RegPath
)
174 DPRINT("********* USB Storage *********\n");
176 DriverObject
->DriverUnload
= USBSTOR_Unload
;
177 DriverObject
->DriverExtension
->AddDevice
= USBSTOR_AddDevice
;
178 DriverObject
->DriverStartIo
= USBSTOR_StartIo
;
179 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = USBSTOR_DispatchClose
;
180 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = USBSTOR_DispatchClose
;
181 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = USBSTOR_DispatchDeviceControl
; // scsi pass through requests
182 DriverObject
->MajorFunction
[IRP_MJ_READ
] = USBSTOR_DispatchReadWrite
;
183 DriverObject
->MajorFunction
[IRP_MJ_WRITE
] = USBSTOR_DispatchReadWrite
;
184 DriverObject
->MajorFunction
[IRP_MJ_SCSI
] = USBSTOR_DispatchScsi
;
185 DriverObject
->MajorFunction
[IRP_MJ_PNP
] = USBSTOR_DispatchPnp
;
186 DriverObject
->MajorFunction
[IRP_MJ_POWER
] = USBSTOR_DispatchPower
;
188 return STATUS_SUCCESS
;