ReactOS  0.4.14-dev-384-g5b37caa
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  ULONG 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 = (PDEVICE_RELATIONS)AllocateItem(PagedPool, sizeof(DEVICE_RELATIONS) + (DeviceCount > 1 ? (DeviceCount-1) * sizeof(PDEVICE_OBJECT) : 0));
65  if (!DeviceRelations)
66  {
67  Irp->IoStatus.Information = 0;
68  Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
71  }
72 
73  // add device objects
74  for (Index = 0; Index < max(DeviceExtension->MaxLUN, 1); Index++)
75  {
76  if (DeviceExtension->ChildPDO[Index])
77  {
78  // store child pdo
79  DeviceRelations->Objects[DeviceRelations->Count] = DeviceExtension->ChildPDO[Index];
80 
81  // add reference
82  ObReferenceObject(DeviceExtension->ChildPDO[Index]);
83 
84  DeviceRelations->Count++;
85  }
86  }
87 
88  Irp->IoStatus.Information = (ULONG_PTR)DeviceRelations;
89  Irp->IoStatus.Status = STATUS_SUCCESS;
90  }
91 
93 
94  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
95 }
#define max(a, b)
Definition: svc.c:63
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ PIRP Irp
Definition: csq.h:116
PDEVICE_OBJECT Objects[1]
Definition: iotypes.h:2055
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
FORCEINLINE VOID IoCopyCurrentIrpStackLocationToNext(_Inout_ PIRP Irp)
Definition: iofuncs.h:2820
long LONG
Definition: pedump.c:60
ULONG DeviceCount
Definition: mpu401.c:26
struct _DEVICE_RELATIONS * PDEVICE_RELATIONS
#define IoCompleteRequest
Definition: irp.c:1240
static const UCHAR Index[8]
Definition: usbohci.c:18
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define ObReferenceObject
Definition: obfuncs.h:204
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:566
#define ULONG_PTR
Definition: config.h:101
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
return STATUS_SUCCESS
Definition: btrfs.c:2938

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 
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 0
331  //
332  // we can if nothing is pending
333  //
334  if (DeviceExtension->IrpPendingCount != 0 ||
335  DeviceExtension->ActiveSrb != NULL)
336 #else
337  if (TRUE)
338 #endif
339  {
340  /* We have pending requests */
341  DPRINT1("Failing removal/stop request due to pending requests present\n");
343  }
344  else
345  {
346  /* We're all clear */
347  Irp->IoStatus.Status = STATUS_SUCCESS;
348 
350  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
351  }
352  break;
353  }
354  case IRP_MN_START_DEVICE:
355  {
357  break;
358  }
359  default:
360  {
361  // forward irp to next device object
363  return IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
364  }
365  }
366 
367  if (Status != STATUS_PENDING)
368  {
369  Irp->IoStatus.Status = Status;
371  }
372 
373  return Status;
374 }
#define TRUE
Definition: types.h:120
#define IRP_MN_REMOVE_DEVICE
NTSTATUS USBSTOR_FdoHandleDeviceRelations(IN PFDO_DEVICE_EXTENSION DeviceExtension, IN OUT PIRP Irp)
Definition: fdo.c:41
_In_ PIRP Irp
Definition: csq.h:116
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:150
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:98
#define IRP_MN_QUERY_REMOVE_DEVICE
VOID NTAPI IoStopTimer(PDEVICE_OBJECT DeviceObject)
Definition: iotimer.c:166
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
#define IoCompleteRequest
Definition: irp.c:1240
void DPRINT(...)
Definition: polytest.cpp:61
#define IRP_MN_QUERY_STOP_DEVICE
#define IRP_MN_SURPRISE_REMOVAL
Definition: ntifs_ex.h:408
#define IRP_MN_STOP_DEVICE
#define STATUS_PENDING
Definition: ntstatus.h:82
#define IRP_MN_START_DEVICE
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
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:566
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
return STATUS_SUCCESS
Definition: btrfs.c:2938
#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 98 of file fdo.c.

102 {
104  ULONG Index;
105 
106  DPRINT("Handling FDO removal %p\n", DeviceObject);
107 
108  // FIXME: wait for devices finished processing
109  for (Index = 0; Index < 16; Index++)
110  {
111  if (DeviceExtension->ChildPDO[Index] != NULL)
112  {
113  DPRINT("Deleting PDO %p RefCount %x AttachedDevice %p \n", DeviceExtension->ChildPDO[Index], DeviceExtension->ChildPDO[Index]->ReferenceCount, DeviceExtension->ChildPDO[Index]->AttachedDevice);
114  IoDeleteDevice(DeviceExtension->ChildPDO[Index]);
115  }
116  }
117 
118  // Freeing everything in DeviceExtension
119  ASSERT(
120  DeviceExtension->DeviceDescriptor &&
121  DeviceExtension->ConfigurationDescriptor &&
122  DeviceExtension->InterfaceInformation &&
123  DeviceExtension->ResetDeviceWorkItem
124  );
125 
126  ExFreePoolWithTag(DeviceExtension->DeviceDescriptor, USB_STOR_TAG);
127  ExFreePoolWithTag(DeviceExtension->ConfigurationDescriptor, USB_STOR_TAG);
128  ExFreePoolWithTag(DeviceExtension->InterfaceInformation, USB_STOR_TAG);
129  IoFreeWorkItem(DeviceExtension->ResetDeviceWorkItem);
130 
131  if (DeviceExtension->SerialNumber)
132  {
133  ExFreePoolWithTag(DeviceExtension->SerialNumber, USB_STOR_TAG);
134  }
135 
136  // Send the IRP down the stack
138  Irp->IoStatus.Status = STATUS_SUCCESS;
139  Status = IoCallDriver(DeviceExtension->LowerDeviceObject, Irp);
140 
141  // Detach from the device stack
142  IoDetachDevice(DeviceExtension->LowerDeviceObject);
143 
145 
146  return Status;
147 }
_In_ PIRP Irp
Definition: csq.h:116
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
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
static const UCHAR Index[8]
Definition: usbohci.c:18
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define USB_STOR_TAG
Definition: usbstor.h:10
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
VOID NTAPI IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject)
Definition: device.c:1251
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 ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:2938

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 150 of file fdo.c.

154 {
157  UCHAR Index = 0;
158  PIO_WORKITEM WorkItem;
159 
160  // forward irp to lower device
161  Status = USBSTOR_SyncForwardIrp(DeviceExtension->LowerDeviceObject, Irp);
162  if (!NT_SUCCESS(Status))
163  {
164  DPRINT1("USBSTOR_FdoHandleStartDevice Lower device failed to start %x\n", Status);
165  return Status;
166  }
167 
168  if (!DeviceExtension->ResetDeviceWorkItem)
169  {
170  WorkItem = IoAllocateWorkItem(DeviceObject);
171  DeviceExtension->ResetDeviceWorkItem = WorkItem;
172 
173  if (!WorkItem)
174  {
176  }
177  }
178 
179  // initialize irp queue
180  USBSTOR_QueueInitialize(DeviceExtension);
181 
182  // first get device & configuration & string descriptor
184  if (!NT_SUCCESS(Status))
185  {
186  DPRINT1("USBSTOR_FdoHandleStartDevice failed to get device descriptor with %x\n", Status);
187  return Status;
188  }
189 
190 #if DBG
191  USBSTOR_DumpDeviceDescriptor(DeviceExtension->DeviceDescriptor);
192 #endif
193 
194  // Check that this device uses bulk transfers and is SCSI
195 
196  InterfaceDesc = (PUSB_INTERFACE_DESCRIPTOR)((ULONG_PTR)DeviceExtension->ConfigurationDescriptor + sizeof(USB_CONFIGURATION_DESCRIPTOR));
198  ASSERT(InterfaceDesc->bLength == sizeof(USB_INTERFACE_DESCRIPTOR));
199 
200  DPRINT("bInterfaceSubClass %x\n", InterfaceDesc->bInterfaceSubClass);
201  if (InterfaceDesc->bInterfaceProtocol != 0x50)
202  {
203  DPRINT1("USB Device is not a bulk only device and is not currently supported\n");
204  return STATUS_NOT_SUPPORTED;
205  }
206 
207  if (InterfaceDesc->bInterfaceSubClass == 0x04) // UFI subclass
208  {
209  // FIXME: need to pad CDBs to 12 byte
210  // mode select commands must be translated from 1AH / 15h to 5AH / 55h
211  DPRINT1("[USBSTOR] Error: need to pad CDBs\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 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ PIRP Irp
Definition: csq.h:116
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:63
#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
void DPRINT(...)
Definition: polytest.cpp:61
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const UCHAR Index[8]
Definition: usbohci.c:18
VOID USBSTOR_QueueInitialize(PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: queue.c:17
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
unsigned char UCHAR
Definition: xmlstorage.h:181
struct _USB_CONFIGURATION_DESCRIPTOR USB_CONFIGURATION_DESCRIPTOR
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
static const struct @257 InterfaceDesc[]
#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:896
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
NTSTATUS USBSTOR_GetPipeHandles(IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: descriptor.c:319
return STATUS_SUCCESS
Definition: btrfs.c:2938
NTSTATUS USBSTOR_GetMaxLUN(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:204

Referenced by USBSTOR_FdoHandlePnp().