ReactOS  0.4.15-dev-3203-gacde1e0
fdo.c File Reference
#include "usbstor.h"
#include <debug.h>
Include dependency graph for fdo.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS USBSTOR_FdoHandleDeviceRelations (IN PFDO_DEVICE_EXTENSION DeviceExtension, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_FdoHandleRemoveDevice (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_FdoHandleStartDevice (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_FdoHandlePnp (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file fdo.c.

Function Documentation

◆ USBSTOR_FdoHandleDeviceRelations()

NTSTATUS USBSTOR_FdoHandleDeviceRelations ( IN PFDO_DEVICE_EXTENSION  DeviceExtension,
IN OUT PIRP  Irp 
)

Definition at line 41 of file fdo.c.

44 {
45  INT32 DeviceCount = 0;
46  LONG Index;
47  PDEVICE_RELATIONS DeviceRelations;
48  PIO_STACK_LOCATION IoStack;
49 
51 
52  // FDO always only handles bus relations
53  if (IoStack->Parameters.QueryDeviceRelations.Type == BusRelations)
54  {
55  // go through array and count device objects
56  for (Index = 0; Index < max(DeviceExtension->MaxLUN, 1); Index++)
57  {
58  if (DeviceExtension->ChildPDO[Index])
59  {
60  DeviceCount++;
61  }
62  }
63 
64  DeviceRelations = ExAllocatePoolWithTag(PagedPool, sizeof(DEVICE_RELATIONS) + (DeviceCount - 1) * sizeof(PDEVICE_OBJECT), USB_STOR_TAG);
65  if (!DeviceRelations)
66  {
67  Irp->IoStatus.Information = 0;
68  Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
71  }
72 
73  DeviceRelations->Count = 0;
74 
75  // add device objects
76  for (Index = 0; Index < max(DeviceExtension->MaxLUN, 1); Index++)
77  {
78  if (DeviceExtension->ChildPDO[Index])
79  {
80  // store child pdo
81  DeviceRelations->Objects[DeviceRelations->Count] = DeviceExtension->ChildPDO[Index];
82 
83  // add reference
84  ObReferenceObject(DeviceExtension->ChildPDO[Index]);
85 
86  DeviceRelations->Count++;
87  }
88  }
89 
90  Irp->IoStatus.Information = (ULONG_PTR)DeviceRelations;
91  Irp->IoStatus.Status = STATUS_SUCCESS;
92  }
93 
95 
96  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
97 }
#define max(a, b)
Definition: svc.c:63
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
PDEVICE_OBJECT Objects[1]
Definition: iotypes.h:2163
FORCEINLINE VOID IoCopyCurrentIrpStackLocationToNext(_Inout_ PIRP Irp)
Definition: iofuncs.h:2868
_In_ PIRP Irp
Definition: csq.h:116
long LONG
Definition: pedump.c:60
ULONG DeviceCount
Definition: mpu401.c:26
#define IoCompleteRequest
Definition: irp.c:1240
_In_ WDFCOLLECTION _In_ ULONG Index
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define USB_STOR_TAG
Definition: usbstor.h:10
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2793
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define ObReferenceObject
Definition: obfuncs.h:204
#define IO_NO_INCREMENT
Definition: iotypes.h:598
signed int INT32
#define ULONG_PTR
Definition: config.h:101
#define STATUS_SUCCESS
Definition: shellext.h:65
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:3128

Referenced by USBSTOR_FdoHandlePnp().

◆ USBSTOR_FdoHandlePnp()

NTSTATUS USBSTOR_FdoHandlePnp ( IN PDEVICE_OBJECT  DeviceObject,
IN OUT PIRP  Irp 
)

Definition at line 277 of file fdo.c.

280 {
281  PIO_STACK_LOCATION IoStack;
282  PFDO_DEVICE_EXTENSION DeviceExtension;
284 
286  DeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
287  ASSERT(DeviceExtension->Common.IsFDO);
288 
289  switch(IoStack->MinorFunction)
290  {
292  {
293  DPRINT("IRP_MN_SURPRISE_REMOVAL %p\n", DeviceObject);
294  Irp->IoStatus.Status = STATUS_SUCCESS;
295 
296  // forward irp to next device object
298  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
299  }
301  {
302  DPRINT("IRP_MN_QUERY_DEVICE_RELATIONS %p Type: %u\n", DeviceObject, IoStack->Parameters.QueryDeviceRelations.Type);
303  return USBSTOR_FdoHandleDeviceRelations(DeviceExtension, Irp);
304  }
305  case IRP_MN_STOP_DEVICE:
306  {
307  DPRINT1("USBSTOR_FdoHandlePnp: IRP_MN_STOP_DEVICE unimplemented\n");
309  Irp->IoStatus.Status = STATUS_SUCCESS;
310 
311  // forward irp to next device object
313  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
314  }
316  {
317  DPRINT("IRP_MN_REMOVE_DEVICE\n");
318 
319  return USBSTOR_FdoHandleRemoveDevice(DeviceObject, DeviceExtension, Irp);
320  }
322  {
323  // FIXME: set custom capabilities
325  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
326  }
329  {
330  if (DeviceExtension->IrpPendingCount != 0 || DeviceExtension->ActiveSrb != NULL)
331  {
332  /* We have pending requests */
333  DPRINT1("Failing removal/stop request due to pending requests present\n");
335  }
336  else
337  {
338  /* We're all clear */
339  Irp->IoStatus.Status = STATUS_SUCCESS;
340 
342  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
343  }
344  break;
345  }
346  case IRP_MN_START_DEVICE:
347  {
349  break;
350  }
351  default:
352  {
353  // forward irp to next device object
355  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
356  }
357  }
358 
359  if (Status != STATUS_PENDING)
360  {
361  Irp->IoStatus.Status = Status;
363  }
364 
365  return Status;
366 }
#define IRP_MN_REMOVE_DEVICE
NTSTATUS USBSTOR_FdoHandleDeviceRelations(IN PFDO_DEVICE_EXTENSION DeviceExtension, IN OUT PIRP Irp)
Definition: fdo.c:41
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS USBSTOR_FdoHandleStartDevice(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension, IN OUT PIRP Irp)
Definition: fdo.c:152
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
NTSTATUS USBSTOR_FdoHandleRemoveDevice(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension, IN OUT PIRP Irp)
Definition: fdo.c:100
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_In_ PIRP Irp
Definition: csq.h:116
#define IRP_MN_QUERY_REMOVE_DEVICE
VOID NTAPI IoStopTimer(PDEVICE_OBJECT DeviceObject)
Definition: iotimer.c:166
#define IoCompleteRequest
Definition: irp.c:1240
#define IRP_MN_QUERY_STOP_DEVICE
Status
Definition: gdiplustypes.h:24
#define IRP_MN_SURPRISE_REMOVAL
Definition: ntifs_ex.h:408
#define IRP_MN_STOP_DEVICE
#define ASSERT(a)
Definition: mode.c:44
#define STATUS_PENDING
Definition: ntstatus.h:82
#define IRP_MN_START_DEVICE
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:81
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2793
#define NULL
Definition: types.h:112
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define IRP_MN_QUERY_DEVICE_RELATIONS
#define IoSkipCurrentIrpStackLocation(Irp)
Definition: ntifs_ex.h:421
#define DPRINT1
Definition: precomp.h:8
#define IO_NO_INCREMENT
Definition: iotypes.h:598
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:71
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:3128
#define IRP_MN_QUERY_CAPABILITIES

Referenced by USBSTOR_DispatchPnp().

◆ USBSTOR_FdoHandleRemoveDevice()

NTSTATUS USBSTOR_FdoHandleRemoveDevice ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension,
IN OUT PIRP  Irp 
)

Definition at line 100 of file fdo.c.

104 {
106  ULONG Index;
107 
108  DPRINT("Handling FDO removal %p\n", DeviceObject);
109 
110  // FIXME: wait for devices finished processing
111  for (Index = 0; Index < USB_MAXCHILDREN; Index++)
112  {
113  if (DeviceExtension->ChildPDO[Index] != NULL)
114  {
115  DPRINT("Deleting PDO %p RefCount %x AttachedDevice %p \n", DeviceExtension->ChildPDO[Index], DeviceExtension->ChildPDO[Index]->ReferenceCount, DeviceExtension->ChildPDO[Index]->AttachedDevice);
116  IoDeleteDevice(DeviceExtension->ChildPDO[Index]);
117  }
118  }
119 
120  // Freeing everything in DeviceExtension
121  ASSERT(
122  DeviceExtension->DeviceDescriptor &&
123  DeviceExtension->ConfigurationDescriptor &&
124  DeviceExtension->InterfaceInformation &&
125  DeviceExtension->ResetDeviceWorkItem
126  );
127 
128  ExFreePoolWithTag(DeviceExtension->DeviceDescriptor, USB_STOR_TAG);
129  ExFreePoolWithTag(DeviceExtension->ConfigurationDescriptor, USB_STOR_TAG);
130  ExFreePoolWithTag(DeviceExtension->InterfaceInformation, USB_STOR_TAG);
131  IoFreeWorkItem(DeviceExtension->ResetDeviceWorkItem);
132 
133  if (DeviceExtension->SerialNumber)
134  {
135  ExFreePoolWithTag(DeviceExtension->SerialNumber, USB_STOR_TAG);
136  }
137 
138  // Send the IRP down the stack
140  Irp->IoStatus.Status = STATUS_SUCCESS;
141  Status = IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
142 
143  // Detach from the device stack
144  IoDetachDevice(DeviceExtension->LowerDeviceObject);
145 
147 
148  return Status;
149 }
LONG NTSTATUS
Definition: precomp.h:26
VOID NTAPI IoDetachDevice(IN PDEVICE_OBJECT TargetDevice)
Definition: device.c:1296
VOID NTAPI IoFreeWorkItem(IN PIO_WORKITEM IoWorkItem)
Definition: iowork.c:64
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_In_ PIRP Irp
Definition: csq.h:116
Status
Definition: gdiplustypes.h:24
#define USB_MAXCHILDREN
Definition: usbstor.h:60
#define ASSERT(a)
Definition: mode.c:44
_In_ WDFCOLLECTION _In_ ULONG Index
#define USB_STOR_TAG
Definition: usbstor.h:10
VOID NTAPI IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject)
Definition: device.c:1251
#define NULL
Definition: types.h:112
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define IoSkipCurrentIrpStackLocation(Irp)
Definition: ntifs_ex.h:421
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:71
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099

Referenced by USBSTOR_FdoHandlePnp().

◆ USBSTOR_FdoHandleStartDevice()

NTSTATUS USBSTOR_FdoHandleStartDevice ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension,
IN OUT PIRP  Irp 
)

Definition at line 152 of file fdo.c.

156 {
159  UCHAR Index = 0;
161 
162  // forward irp to lower device
163  Status = USBSTOR_SyncForwardIrp(DeviceExtension->LowerDeviceObject, Irp);
164  if (!NT_SUCCESS(Status))
165  {
166  DPRINT1("USBSTOR_FdoHandleStartDevice Lower device failed to start %x\n", Status);
167  return Status;
168  }
169 
170  if (!DeviceExtension->ResetDeviceWorkItem)
171  {
173  DeviceExtension->ResetDeviceWorkItem = WorkItem;
174 
175  if (!WorkItem)
176  {
178  }
179  }
180 
181  // initialize irp queue
182  USBSTOR_QueueInitialize(DeviceExtension);
183 
184  // first get device & configuration & string descriptor
186  if (!NT_SUCCESS(Status))
187  {
188  DPRINT1("USBSTOR_FdoHandleStartDevice failed to get device descriptor with %x\n", Status);
189  return Status;
190  }
191 
192 #if DBG
193  USBSTOR_DumpDeviceDescriptor(DeviceExtension->DeviceDescriptor);
194 #endif
195 
196  // Check that this device uses bulk transfers and is SCSI
197 
198  InterfaceDesc = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)DeviceExtension->ConfigurationDescriptor + sizeof(USB_CONFIGURATION_DESCRIPTOR));
200  ASSERT(InterfaceDesc->bLength == sizeof(USB_INTERFACE_DESCRIPTOR));
201 
202  DPRINT("bInterfaceSubClass %x\n", InterfaceDesc->bInterfaceSubClass);
203  if (InterfaceDesc->bInterfaceProtocol != USB_PROTOCOL_BULK)
204  {
205  DPRINT1("USB Device is not a bulk only device and is not currently supported\n");
206  return STATUS_NOT_SUPPORTED;
207  }
208 
209  if (InterfaceDesc->bInterfaceSubClass == USB_SUBCLASS_UFI)
210  {
211  DPRINT1("USB Floppy devices are not supported\n");
212  return STATUS_NOT_SUPPORTED;
213  }
214 
215  // now select an interface
217  if (!NT_SUCCESS(Status))
218  {
219  // failed to get device descriptor
220  DPRINT1("USBSTOR_FdoHandleStartDevice failed to select configuration / interface with %x\n", Status);
221  return Status;
222  }
223 
224  // check if we got a bulk in + bulk out endpoint
225  Status = USBSTOR_GetPipeHandles(DeviceExtension);
226  if (!NT_SUCCESS(Status))
227  {
228  DPRINT1("USBSTOR_FdoHandleStartDevice no pipe handles %x\n", Status);
229  return Status;
230  }
231 
232  Status = USBSTOR_GetMaxLUN(DeviceExtension->LowerDeviceObject, DeviceExtension);
233  if (!NT_SUCCESS(Status))
234  {
235  DPRINT1("USBSTOR_FdoHandleStartDevice failed to get max lun %x\n", Status);
236  return Status;
237  }
238 
239  // now create for each LUN a device object, 1 minimum
240  do
241  {
243 
244  if (!NT_SUCCESS(Status))
245  {
246  DPRINT1("USBSTOR_FdoHandleStartDevice USBSTOR_CreatePDO failed for Index %lu with Status %x\n", Index, Status);
247  return Status;
248  }
249 
250  Index++;
251  DeviceExtension->InstanceCount++;
252 
253  } while(Index < DeviceExtension->MaxLUN);
254 
255 #if 0
256  //
257  // finally get usb device interface
258  //
259  Status = USBSTOR_GetBusInterface(DeviceExtension->LowerDeviceObject, &DeviceExtension->BusInterface);
260  if (!NT_SUCCESS(Status))
261  {
262  //
263  // failed to device interface
264  //
265  DPRINT1("USBSTOR_FdoHandleStartDevice failed to get device interface %x\n", Status);
266  return Status;
267  }
268 #endif
269 
270  //IoStartTimer(DeviceObject);
271 
272  DPRINT("USBSTOR_FdoHandleStartDevice FDO is initialized\n");
273  return STATUS_SUCCESS;
274 }
return STATUS_NOT_SUPPORTED
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define USB_SUBCLASS_UFI
Definition: usbstor.h:25
NTSTATUS NTAPI USBSTOR_GetBusInterface(IN PDEVICE_OBJECT DeviceObject, OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface)
Definition: misc.c:57
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS USBSTOR_GetDescriptors(IN PDEVICE_OBJECT DeviceObject)
Definition: descriptor.c:75
struct _USB_INTERFACE_DESCRIPTOR * PUSB_INTERFACE_DESCRIPTOR
PIO_WORKITEM NTAPI IoAllocateWorkItem(IN PDEVICE_OBJECT DeviceObject)
Definition: iowork.c:75
uint32_t ULONG_PTR
Definition: typedefs.h:65
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_In_ PIRP Irp
Definition: csq.h:116
#define USB_INTERFACE_DESCRIPTOR_TYPE
Definition: usb100.h:52
NTSTATUS USBSTOR_SelectConfigurationAndInterface(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: descriptor.c:234
NTSTATUS NTAPI USBSTOR_SyncForwardIrp(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: misc.c:34
Status
Definition: gdiplustypes.h:24
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
_In_ WDFCOLLECTION _In_ ULONG Index
VOID USBSTOR_QueueInitialize(PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: queue.c:17
unsigned char UCHAR
Definition: xmlstorage.h:181
struct _USB_CONFIGURATION_DESCRIPTOR USB_CONFIGURATION_DESCRIPTOR
#define USB_PROTOCOL_BULK
Definition: usbstor.h:38
#define DPRINT1
Definition: precomp.h:8
VOID USBSTOR_DumpDeviceDescriptor(PUSB_DEVICE_DESCRIPTOR DeviceDescriptor)
Definition: fdo.c:18
NTSTATUS USBSTOR_CreatePDO(IN PDEVICE_OBJECT DeviceObject, IN UCHAR LUN)
Definition: pdo.c:836
static const struct @250 InterfaceDesc[]
NTSTATUS USBSTOR_GetPipeHandles(IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: descriptor.c:319
#define STATUS_SUCCESS
Definition: shellext.h:65
_Must_inspect_result_ _In_ PWDF_WORKITEM_CONFIG _In_ PWDF_OBJECT_ATTRIBUTES _Out_ WDFWORKITEM * WorkItem
Definition: wdfworkitem.h:110
#define DPRINT
Definition: sndvol32.h:71
NTSTATUS USBSTOR_GetMaxLUN(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:204

Referenced by USBSTOR_FdoHandlePnp().