xref: /reactos/drivers/usb/usbstor/usbstor.c (revision a5d0b5fd)
1 /*
2  * PROJECT:     ReactOS Universal Serial Bus Bulk Storage Driver
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        drivers/usb/usbstor/usbstor.c
5  * PURPOSE:     USB block storage device driver.
6  * PROGRAMMERS:
7  *              James Tabor
8                 Johannes Anderwald
9  */
10 
11 /* INCLUDES ******************************************************************/
12 
13 #define NDEBUG
14 #define INITGUID
15 #include "usbstor.h"
16 
17 /* PUBLIC AND PRIVATE FUNCTIONS **********************************************/
18 
19 NTSTATUS
20 NTAPI
21 USBSTOR_AddDevice(
22     IN PDRIVER_OBJECT DriverObject,
23     IN PDEVICE_OBJECT PhysicalDeviceObject)
24 {
25     NTSTATUS Status;
26     PDEVICE_OBJECT DeviceObject;
27     PFDO_DEVICE_EXTENSION DeviceExtension;
28 
29     //
30     // lets create the device
31     //
32     Status = IoCreateDevice(DriverObject, sizeof(FDO_DEVICE_EXTENSION), 0, FILE_DEVICE_BUS_EXTENDER, FILE_AUTOGENERATED_DEVICE_NAME | FILE_DEVICE_SECURE_OPEN, FALSE, &DeviceObject);
33 
34     //
35     // check for success
36     //
37     if (!NT_SUCCESS(Status))
38     {
39         DPRINT1("USBSTOR_AddDevice: Failed to create FDO Status %x\n", Status);
40         return Status;
41     }
42 
43     //
44     // get device extension
45     //
46     DeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
47     ASSERT(DeviceExtension);
48 
49     //
50     // zero device extension
51     //
52     RtlZeroMemory(DeviceExtension, sizeof(FDO_DEVICE_EXTENSION));
53 
54     //
55     // initialize device extension
56     //
57     DeviceExtension->Common.IsFDO = TRUE;
58     DeviceExtension->FunctionalDeviceObject = DeviceObject;
59     DeviceExtension->PhysicalDeviceObject = PhysicalDeviceObject;
60     DeviceExtension->LowerDeviceObject = IoAttachDeviceToDeviceStack(DeviceObject, PhysicalDeviceObject);
61 
62     //
63     // did attaching fail
64     //
65     if (!DeviceExtension->LowerDeviceObject)
66     {
67         //
68         // device removed
69         //
70         IoDeleteDevice(DeviceObject);
71 
72         return STATUS_DEVICE_REMOVED;
73     }
74 
75     //
76     // set device flags
77     //
78     DeviceObject->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE;
79 
80     //
81     // device is initialized
82     //
83     DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
84 
85 
86     //
87     // done
88     //
89     return STATUS_SUCCESS;
90 }
91 
92 VOID
93 NTAPI
94 USBSTOR_Unload(
95     PDRIVER_OBJECT DriverObject)
96 {
97     //
98     // no-op
99     //
100 }
101 
102 NTSTATUS
103 NTAPI
104 USBSTOR_DispatchClose(
105     PDEVICE_OBJECT DeviceObject,
106     PIRP Irp)
107 {
108     //
109     // function always succeeds ;)
110     //
111     DPRINT("USBSTOR_DispatchClose\n");
112     Irp->IoStatus.Information = 0;
113     Irp->IoStatus.Status = STATUS_SUCCESS;
114     IoCompleteRequest(Irp, IO_NO_INCREMENT);
115     return STATUS_SUCCESS;
116 }
117 
118 
119 NTSTATUS
120 NTAPI
121 USBSTOR_DispatchDeviceControl(
122     PDEVICE_OBJECT DeviceObject,
123     PIRP Irp)
124 {
125     NTSTATUS Status;
126 
127     //
128     // handle requests
129     //
130     Status = USBSTOR_HandleDeviceControl(DeviceObject, Irp);
131 
132     //
133     // complete request
134     //
135     Irp->IoStatus.Status = Status;
136     IoCompleteRequest(Irp, IO_NO_INCREMENT);
137 
138     //
139     // done
140     //
141     return Status;
142 }
143 
144 
145 NTSTATUS
146 NTAPI
147 USBSTOR_DispatchScsi(
148     PDEVICE_OBJECT DeviceObject,
149     PIRP Irp)
150 {
151     //
152     // handle requests
153     //
154     return USBSTOR_HandleInternalDeviceControl(DeviceObject, Irp);
155 }
156 
157 NTSTATUS
158 NTAPI
159 USBSTOR_DispatchReadWrite(
160     PDEVICE_OBJECT DeviceObject,
161     PIRP Irp)
162 {
163     //
164     // read write ioctl is not supported
165     //
166     Irp->IoStatus.Information = 0;
167     Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
168     IoCompleteRequest(Irp, IO_NO_INCREMENT);
169     return STATUS_INVALID_PARAMETER;
170 }
171 
172 NTSTATUS
173 NTAPI
174 USBSTOR_DispatchPnp(
175     PDEVICE_OBJECT DeviceObject,
176     PIRP Irp)
177 {
178     PUSBSTOR_COMMON_DEVICE_EXTENSION DeviceExtension;
179 
180     //
181     // get common device extension
182     //
183     DeviceExtension = (PUSBSTOR_COMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
184 
185     //
186     // is it for the FDO
187     //
188     if (DeviceExtension->IsFDO)
189     {
190         //
191         // dispatch pnp request to fdo pnp handler
192         //
193         return USBSTOR_FdoHandlePnp(DeviceObject, Irp);
194     }
195     else
196     {
197         //
198         // dispatch request to pdo pnp handler
199         //
200         return USBSTOR_PdoHandlePnp(DeviceObject, Irp);
201     }
202 }
203 
204 NTSTATUS
205 NTAPI
206 USBSTOR_DispatchPower(
207     PDEVICE_OBJECT DeviceObject,
208     PIRP Irp)
209 {
210     UNIMPLEMENTED
211 
212     Irp->IoStatus.Information = 0;
213     Irp->IoStatus.Status = STATUS_SUCCESS;
214     IoCompleteRequest(Irp, IO_NO_INCREMENT);
215     return STATUS_SUCCESS;
216 }
217 
218 
219 
220 NTSTATUS
221 NTAPI
222 DriverEntry(
223     IN PDRIVER_OBJECT DriverObject,
224     IN PUNICODE_STRING RegPath)
225 {
226 
227     DPRINT1("********* USB Storage *********\n");
228 
229     //
230     // driver unload routine
231     //
232     DriverObject->DriverUnload = USBSTOR_Unload;
233 
234     //
235     // add device function
236     //
237     DriverObject->DriverExtension->AddDevice = USBSTOR_AddDevice;
238 
239     //
240     // driver start i/o routine
241     //
242     DriverObject->DriverStartIo = USBSTOR_StartIo;
243 
244     //
245     // create / close
246     //
247     DriverObject->MajorFunction[IRP_MJ_CREATE] = USBSTOR_DispatchClose;
248     DriverObject->MajorFunction[IRP_MJ_CLOSE] = USBSTOR_DispatchClose;
249 
250     //
251     // scsi pass through requests
252     //
253     DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = USBSTOR_DispatchDeviceControl;
254 
255     //
256     // irp dispatch read / write
257     //
258     DriverObject->MajorFunction[IRP_MJ_READ] = USBSTOR_DispatchReadWrite;
259     DriverObject->MajorFunction[IRP_MJ_WRITE] = USBSTOR_DispatchReadWrite;
260 
261     //
262     // scsi queue ioctl
263     //
264     DriverObject->MajorFunction[IRP_MJ_SCSI] = USBSTOR_DispatchScsi;
265 
266     //
267     // pnp processing
268     //
269     DriverObject->MajorFunction[IRP_MJ_PNP] = USBSTOR_DispatchPnp;
270 
271     //
272     // power processing
273     //
274     DriverObject->MajorFunction[IRP_MJ_POWER] = USBSTOR_DispatchPower;
275 
276     return STATUS_SUCCESS;
277 }
278 
279