ReactOS  0.4.14-dev-297-g23e575c
usbstor.h File Reference
#include <wdm.h>
#include <usbdi.h>
#include <usbbusif.h>
#include <usbdlib.h>
#include <classpnp.h>
#include <pshpack1.h>
Include dependency graph for usbstor.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  __COMMON_DEVICE_EXTENSION__
 
struct  FDO_DEVICE_EXTENSION
 
struct  PDO_DEVICE_EXTENSION
 
struct  CBW
 
struct  CSW
 
struct  UFI_READ_WRITE_CMD
 
struct  UFI_CAPACITY_CMD
 
struct  UFI_CAPACITY_RESPONSE
 
struct  UFI_SENSE_CMD
 
struct  UFI_MODE_PARAMETER_HEADER
 
struct  UFI_TIMER_PROTECT_PAGE
 
struct  UFI_READ_FORMAT_CAPACITY
 
struct  UFI_CAPACITY_FORMAT_HEADER
 
struct  UFI_CAPACITY_DESCRIPTOR
 
struct  UFI_TEST_UNIT_CMD
 
struct  UFI_UNKNOWN_CMD
 
struct  IRP_CONTEXT
 
struct  _ERRORHANDLER_WORKITEM_DATA
 

Macros

#define USB_STOR_TAG   'sbsu'
 
#define USB_MAXCHILDREN   (16)
 
#define USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH   0x10000
 
#define HTONS(n)   (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
 
#define NTOHS(n)   (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
 
#define HTONL(n)
 
#define NTOHL(n)
 
#define BooleanFlagOn(Flags, SingleFlag)   ((BOOLEAN)((((Flags) & (SingleFlag)) != 0)))
 
#define SrbGetCdb(srb)   ((PCDB)(srb->Cdb))
 
#define USB_RECOVERABLE_ERRORS
 
#define USBSTOR_FDO_FLAGS_DEVICE_RESETTING   0x00000001
 
#define USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE   0x00000002
 
#define USB_BULK_GET_MAX_LUN   0xFE
 
#define USB_BULK_RESET_DEVICE   0xFF
 
#define CBW_SIGNATURE   0x43425355
 
#define CSW_SIGNATURE   0x53425355
 
#define MAX_LUN   0xF
 
#define CSW_STATUS_COMMAND_PASSED   0x00
 
#define CSW_STATUS_COMMAND_FAILED   0x01
 
#define CSW_STATUS_PHASE_ERROR   0x02
 
#define UFI_READ_WRITE_CMD_LEN   (0xA)
 
#define UFI_CAPACITY_CMD_LEN   0xA
 
#define UFI_READ_CAPACITY_CMD_LEN   0xA
 
#define UFI_SENSE_CMD_LEN   (6)
 
#define UFI_READ_FORMAT_CAPACITY_CMD_LEN   (10)
 
#define UNFORMATTED_MEDIA_CODE_DESCRIPTORY_TYPE   (1)
 
#define FORMAT_MEDIA_CODE_DESCRIPTOR_TYPE   (2)
 
#define CARTRIDGE_MEDIA_CODE_DESCRIPTOR_TYPE   (3)
 
#define UFI_TEST_UNIT_CMD_LEN   (6)
 

Typedefs

typedef struct __COMMON_DEVICE_EXTENSION__ USBSTOR_COMMON_DEVICE_EXTENSION
 
typedef struct __COMMON_DEVICE_EXTENSION__PUSBSTOR_COMMON_DEVICE_EXTENSION
 
typedef struct FDO_DEVICE_EXTENSIONPFDO_DEVICE_EXTENSION
 
typedef struct PDO_DEVICE_EXTENSIONPPDO_DEVICE_EXTENSION
 
typedef struct CBWPCBW
 
typedef struct CSWPCSW
 
typedef struct UFI_CAPACITY_CMDPUFI_CAPACITY_CMD
 
typedef struct UFI_CAPACITY_RESPONSEPUFI_CAPACITY_RESPONSE
 
typedef struct UFI_SENSE_CMDPUFI_SENSE_CMD
 
typedef struct UFI_MODE_PARAMETER_HEADERPUFI_MODE_PARAMETER_HEADER
 
typedef struct UFI_TIMER_PROTECT_PAGEPUFI_TIMER_PROTECT_PAGE
 
typedef struct UFI_READ_FORMAT_CAPACITYPUFI_READ_FORMAT_CAPACITY
 
typedef struct UFI_CAPACITY_FORMAT_HEADERPUFI_CAPACITY_FORMAT_HEADER
 
typedef struct UFI_CAPACITY_DESCRIPTORPUFI_CAPACITY_DESCRIPTOR
 
typedef struct UFI_TEST_UNIT_CMDPUFI_TEST_UNIT_CMD
 
typedef struct UFI_UNKNOWN_CMDPUFI_UNKNOWN_CMD
 
typedef struct IRP_CONTEXTPIRP_CONTEXT
 
typedef struct _ERRORHANDLER_WORKITEM_DATA ERRORHANDLER_WORKITEM_DATA
 
typedef struct _ERRORHANDLER_WORKITEM_DATAPERRORHANDLER_WORKITEM_DATA
 

Functions

 C_ASSERT (sizeof(CBW)==31)
 
 C_ASSERT (sizeof(UFI_READ_WRITE_CMD)==12)
 
 C_ASSERT (sizeof(UFI_CAPACITY_CMD)==12)
 
 C_ASSERT (sizeof(UFI_CAPACITY_RESPONSE)==8)
 
 C_ASSERT (sizeof(UFI_SENSE_CMD)==12)
 
 C_ASSERT (sizeof(UFI_MODE_PARAMETER_HEADER)==8)
 
 C_ASSERT (sizeof(UFI_TIMER_PROTECT_PAGE)==8)
 
 C_ASSERT (sizeof(UFI_READ_FORMAT_CAPACITY)==12)
 
 C_ASSERT (sizeof(UFI_CAPACITY_FORMAT_HEADER)==4)
 
 C_ASSERT (sizeof(UFI_TEST_UNIT_CMD)==12)
 
NTSTATUS USBSTOR_FdoHandlePnp (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_PdoHandlePnp (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_CreatePDO (IN PDEVICE_OBJECT DeviceObject, IN UCHAR LUN)
 
NTSTATUS NTAPI USBSTOR_SyncForwardIrp (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS NTAPI USBSTOR_GetBusInterface (IN PDEVICE_OBJECT DeviceObject, OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface)
 
PVOID AllocateItem (IN POOL_TYPE PoolType, IN ULONG ItemSize)
 
VOID FreeItem (IN PVOID Item)
 
NTSTATUS USBSTOR_SyncUrbRequest (IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
 
NTSTATUS USBSTOR_GetMaxLUN (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
 
NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
 
NTSTATUS USBSTOR_ResetDevice (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
 
BOOLEAN USBSTOR_IsFloppy (IN PUCHAR Buffer, IN ULONG BufferLength, OUT PUCHAR MediumTypeCode)
 
NTSTATUS USBSTOR_GetDescriptors (IN PDEVICE_OBJECT DeviceObject)
 
NTSTATUS USBSTOR_SelectConfigurationAndInterface (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
 
NTSTATUS USBSTOR_GetPipeHandles (IN PFDO_DEVICE_EXTENSION DeviceExtension)
 
NTSTATUS USBSTOR_HandleExecuteSCSI (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
NTSTATUS USBSTOR_SendCSWRequest (PIRP_CONTEXT Context, PIRP Irp)
 
NTSTATUS USBSTOR_HandleInternalDeviceControl (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
NTSTATUS USBSTOR_HandleDeviceControl (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
VOID NTAPI USBSTOR_StartIo (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 
VOID USBSTOR_QueueWaitForPendingRequests (IN PDEVICE_OBJECT DeviceObject)
 
VOID USBSTOR_QueueRelease (IN PDEVICE_OBJECT DeviceObject)
 
BOOLEAN USBSTOR_QueueAddIrp (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
VOID NTAPI USBSTOR_CancelIo (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
VOID USBSTOR_QueueInitialize (PFDO_DEVICE_EXTENSION FDODeviceExtension)
 
VOID USBSTOR_QueueNextRequest (IN PDEVICE_OBJECT DeviceObject)
 
VOID USBSTOR_QueueTerminateRequest (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
NTSTATUS USBSTOR_GetEndpointStatus (IN PDEVICE_OBJECT DeviceObject, IN UCHAR bEndpointAddress, OUT PUSHORT Value)
 
NTSTATUS USBSTOR_ResetPipeWithHandle (IN PDEVICE_OBJECT DeviceObject, IN USBD_PIPE_HANDLE PipeHandle)
 
VOID NTAPI USBSTOR_TimerRoutine (PDEVICE_OBJECT DeviceObject, PVOID Context)
 
VOID NTAPI USBSTOR_QueueResetPipe (IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP_CONTEXT Context)
 
VOID NTAPI USBSTOR_QueueResetDevice (IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
 

Macro Definition Documentation

◆ BooleanFlagOn

#define BooleanFlagOn (   Flags,
  SingleFlag 
)    ((BOOLEAN)((((Flags) & (SingleFlag)) != 0)))

Definition at line 29 of file usbstor.h.

◆ CARTRIDGE_MEDIA_CODE_DESCRIPTOR_TYPE

#define CARTRIDGE_MEDIA_CODE_DESCRIPTOR_TYPE   (3)

Definition at line 269 of file usbstor.h.

◆ CBW_SIGNATURE

#define CBW_SIGNATURE   0x43425355

Definition at line 116 of file usbstor.h.

◆ CSW_SIGNATURE

#define CSW_SIGNATURE   0x53425355

Definition at line 117 of file usbstor.h.

◆ CSW_STATUS_COMMAND_FAILED

#define CSW_STATUS_COMMAND_FAILED   0x01

Definition at line 122 of file usbstor.h.

◆ CSW_STATUS_COMMAND_PASSED

#define CSW_STATUS_COMMAND_PASSED   0x00

Definition at line 121 of file usbstor.h.

◆ CSW_STATUS_PHASE_ERROR

#define CSW_STATUS_PHASE_ERROR   0x02

Definition at line 123 of file usbstor.h.

◆ FORMAT_MEDIA_CODE_DESCRIPTOR_TYPE

#define FORMAT_MEDIA_CODE_DESCRIPTOR_TYPE   (2)

Definition at line 268 of file usbstor.h.

◆ HTONL

#define HTONL (   n)
Value:
(((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
GLdouble n
Definition: glext.h:7729
#define long
Definition: qsort.c:33

Definition at line 17 of file usbstor.h.

◆ HTONS

#define HTONS (   n)    (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))

Definition at line 14 of file usbstor.h.

◆ MAX_LUN

#define MAX_LUN   0xF

Definition at line 119 of file usbstor.h.

◆ NTOHL

#define NTOHL (   n)
Value:
(((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
GLdouble n
Definition: glext.h:7729
#define long
Definition: qsort.c:33

Definition at line 23 of file usbstor.h.

◆ NTOHS

#define NTOHS (   n)    (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))

Definition at line 15 of file usbstor.h.

◆ SrbGetCdb

#define SrbGetCdb (   srb)    ((PCDB)(srb->Cdb))

Definition at line 33 of file usbstor.h.

◆ UFI_CAPACITY_CMD_LEN

#define UFI_CAPACITY_CMD_LEN   0xA

Definition at line 171 of file usbstor.h.

◆ UFI_READ_CAPACITY_CMD_LEN

#define UFI_READ_CAPACITY_CMD_LEN   0xA

Definition at line 182 of file usbstor.h.

◆ UFI_READ_FORMAT_CAPACITY_CMD_LEN

#define UFI_READ_FORMAT_CAPACITY_CMD_LEN   (10)

Definition at line 246 of file usbstor.h.

◆ UFI_READ_WRITE_CMD_LEN

#define UFI_READ_WRITE_CMD_LEN   (0xA)

Definition at line 153 of file usbstor.h.

◆ UFI_SENSE_CMD_LEN

#define UFI_SENSE_CMD_LEN   (6)

Definition at line 202 of file usbstor.h.

◆ UFI_TEST_UNIT_CMD_LEN

#define UFI_TEST_UNIT_CMD_LEN   (6)

Definition at line 288 of file usbstor.h.

◆ UNFORMATTED_MEDIA_CODE_DESCRIPTORY_TYPE

#define UNFORMATTED_MEDIA_CODE_DESCRIPTORY_TYPE   (1)

Definition at line 267 of file usbstor.h.

◆ USB_BULK_GET_MAX_LUN

#define USB_BULK_GET_MAX_LUN   0xFE

Definition at line 98 of file usbstor.h.

◆ USB_BULK_RESET_DEVICE

#define USB_BULK_RESET_DEVICE   0xFF

Definition at line 99 of file usbstor.h.

◆ USB_MAXCHILDREN

#define USB_MAXCHILDREN   (16)

Definition at line 11 of file usbstor.h.

◆ USB_RECOVERABLE_ERRORS

#define USB_RECOVERABLE_ERRORS
Value:
#define USBD_STATUS_ENDPOINT_HALTED
Definition: usb.h:190
#define USBD_STATUS_STALL_PID
Definition: usb.h:175
#define USBD_STATUS_DEV_NOT_RESPONDING
Definition: usb.h:176
#define USBD_STATUS_NO_BANDWIDTH
Definition: usb.h:195

Definition at line 36 of file usbstor.h.

◆ USB_STOR_TAG

#define USB_STOR_TAG   'sbsu'

Definition at line 10 of file usbstor.h.

◆ USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH

#define USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH   0x10000

Definition at line 12 of file usbstor.h.

◆ USBSTOR_FDO_FLAGS_DEVICE_RESETTING

#define USBSTOR_FDO_FLAGS_DEVICE_RESETTING   0x00000001

Definition at line 45 of file usbstor.h.

◆ USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE

#define USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE   0x00000002

Definition at line 46 of file usbstor.h.

Typedef Documentation

◆ ERRORHANDLER_WORKITEM_DATA

◆ PCBW

typedef struct CBW * PCBW

◆ PCSW

typedef struct CSW * PCSW

◆ PERRORHANDLER_WORKITEM_DATA

◆ PFDO_DEVICE_EXTENSION

◆ PIRP_CONTEXT

◆ PPDO_DEVICE_EXTENSION

◆ PUFI_CAPACITY_CMD

◆ PUFI_CAPACITY_DESCRIPTOR

◆ PUFI_CAPACITY_FORMAT_HEADER

◆ PUFI_CAPACITY_RESPONSE

◆ PUFI_MODE_PARAMETER_HEADER

◆ PUFI_READ_FORMAT_CAPACITY

◆ PUFI_SENSE_CMD

◆ PUFI_TEST_UNIT_CMD

◆ PUFI_TIMER_PROTECT_PAGE

◆ PUFI_UNKNOWN_CMD

◆ PUSBSTOR_COMMON_DEVICE_EXTENSION

◆ USBSTOR_COMMON_DEVICE_EXTENSION

Function Documentation

◆ AllocateItem()

PVOID AllocateItem ( IN POOL_TYPE  PoolType,
IN ULONG  ItemSize 
)

Definition at line 30 of file misc.c.

33 {
35  if (!Item)
36  return Item;
37 
39  return Item;
40 }
#define TAG_KS
Definition: misc.c:14
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS _Inout_ PLARGE_INTEGER NumberOfBytes
Definition: iotypes.h:998
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
_Must_inspect_result_ _In_ FLT_CONTEXT_TYPE _In_ SIZE_T _In_ POOL_TYPE PoolType
Definition: fltkernel.h:1444

◆ C_ASSERT() [1/10]

C_ASSERT ( sizeof(CBW = =31)

◆ C_ASSERT() [2/10]

C_ASSERT ( sizeof(UFI_READ_WRITE_CMD = =12)

◆ C_ASSERT() [3/10]

C_ASSERT ( sizeof(UFI_CAPACITY_CMD = =12)

◆ C_ASSERT() [4/10]

C_ASSERT ( sizeof(UFI_CAPACITY_RESPONSE = =8)

◆ C_ASSERT() [5/10]

C_ASSERT ( sizeof(UFI_SENSE_CMD = =12)

◆ C_ASSERT() [6/10]

C_ASSERT ( sizeof(UFI_MODE_PARAMETER_HEADER = =8)

◆ C_ASSERT() [7/10]

C_ASSERT ( sizeof(UFI_TIMER_PROTECT_PAGE = =8)

◆ C_ASSERT() [8/10]

C_ASSERT ( sizeof(UFI_READ_FORMAT_CAPACITY = =12)

◆ C_ASSERT() [9/10]

C_ASSERT ( sizeof(UFI_CAPACITY_FORMAT_HEADER = =4)

◆ C_ASSERT() [10/10]

C_ASSERT ( sizeof(UFI_TEST_UNIT_CMD = =12)

◆ FreeItem()

VOID FreeItem ( IN PVOID  Item)

Definition at line 43 of file misc.c.

45 {
46  ExFreePool(Item);
47 }
#define ExFreePool(addr)
Definition: env_spec_w32.h:352

◆ USBSTOR_CancelIo()

VOID NTAPI USBSTOR_CancelIo ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 28 of file queue.c.

31 {
32  PFDO_DEVICE_EXTENSION FDODeviceExtension;
33 
34  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
36  ASSERT(FDODeviceExtension->Common.IsFDO);
37 
38  // this IRP isn't in our list here
39  // now release the cancel lock
40  IoReleaseCancelSpinLock(Irp->CancelIrql);
41  Irp->IoStatus.Status = STATUS_CANCELLED;
42 
45 
47 }
_In_ PIRP Irp
Definition: csq.h:116
#define ASSERT_IRQL_EQUAL(x)
Definition: debug.h:43
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
VOID USBSTOR_QueueNextRequest(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:220
PVOID DeviceExtension
Definition: env_spec_w32.h:418
#define IoCompleteRequest
Definition: irp.c:1240
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
#define STATUS_CANCELLED
Definition: udferr_usr.h:170
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
VOID NTAPI IoReleaseCancelSpinLock(IN KIRQL Irql)
Definition: util.c:150
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
#define IO_NO_INCREMENT
Definition: iotypes.h:566

◆ USBSTOR_CreatePDO()

NTSTATUS USBSTOR_CreatePDO ( IN PDEVICE_OBJECT  DeviceObject,
IN UCHAR  LUN 
)

Definition at line 896 of file pdo.c.

899 {
900  PDEVICE_OBJECT PDO;
902  PPDO_DEVICE_EXTENSION PDODeviceExtension;
903  PFDO_DEVICE_EXTENSION FDODeviceExtension;
904 
905  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
906 
907  // create child device object
909  if (!NT_SUCCESS(Status))
910  {
911  DPRINT1("Failed to create PDO, status %x\n", Status);
912  return Status;
913  }
914 
915  // patch the stack size
917 
918  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)PDO->DeviceExtension;
919 
920  // initialize device extension
921  RtlZeroMemory(PDODeviceExtension, sizeof(PDO_DEVICE_EXTENSION));
922  PDODeviceExtension->Common.IsFDO = FALSE;
923  PDODeviceExtension->LowerDeviceObject = DeviceObject;
924  PDODeviceExtension->PDODeviceObject = &FDODeviceExtension->ChildPDO[LUN];
925  PDODeviceExtension->Self = PDO;
926  PDODeviceExtension->LUN = LUN;
927 
928  PDO->Flags |= DO_DIRECT_IO;
929 
930  // device is initialized
931  PDO->Flags &= ~DO_DEVICE_INITIALIZING;
932 
933  // output device object
934  FDODeviceExtension->ChildPDO[LUN] = PDO;
935 
936  // send inquiry command by irp
938 
939  if (!NT_SUCCESS(Status))
940  {
941  return Status;
942  }
943 
944  if (PDODeviceExtension->InquiryData->DeviceType == DIRECT_ACCESS_DEVICE || PDODeviceExtension->InquiryData->DeviceType == READ_ONLY_DIRECT_ACCESS_DEVICE)
945  {
946  PDODeviceExtension->IsFloppy = FALSE; // TODO: implement the actual check
947  }
948  else
949  {
950  // we work only with DIRECT_ACCESS_DEVICE for now
951  return STATUS_NOT_SUPPORTED;
952  }
953 
954  return Status;
955 }
#define DO_DEVICE_INITIALIZING
Definition: env_spec_w32.h:399
LONG NTSTATUS
Definition: precomp.h:26
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define FILE_DEVICE_MASS_STORAGE
Definition: imports.h:62
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
#define DO_DIRECT_IO
Definition: env_spec_w32.h:396
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:55
#define FILE_AUTOGENERATED_DEVICE_NAME
Definition: iotypes.h:138
#define DIRECT_ACCESS_DEVICE
Definition: cdrw_hw.h:1144
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
#define DPRINT1
Definition: precomp.h:8
#define READ_ONLY_DIRECT_ACCESS_DEVICE
Definition: cdrw_hw.h:1149
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
NTSTATUS NTAPI IoCreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1031
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define FILE_DEVICE_SECURE_OPEN
Definition: cdrw_usr.h:46
static NTSTATUS USBSTOR_FillInquiryData(IN PDEVICE_OBJECT PDODeviceObject)
Definition: pdo.c:845

◆ 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:2966
#define IRP_MN_QUERY_CAPABILITIES

◆ USBSTOR_GetBusInterface()

NTSTATUS NTAPI USBSTOR_GetBusInterface ( IN PDEVICE_OBJECT  DeviceObject,
OUT PUSB_BUS_INTERFACE_USBDI_V2  BusInterface 
)

Definition at line 57 of file misc.c.

60 {
61  KEVENT Event;
63  PIRP Irp;
65  PIO_STACK_LOCATION Stack;
66 
68  ASSERT(BusInterface);
69 
71 
74  NULL,
75  0,
76  NULL,
77  &Event,
78  &IoStatus);
79  if (Irp == NULL)
80  {
82  }
83 
84  // initialize request
86  Stack->MajorFunction = IRP_MJ_PNP;
88  Stack->Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
89  Stack->Parameters.QueryInterface.InterfaceType = (LPGUID)&USB_BUS_INTERFACE_USBDI_GUID;
90  Stack->Parameters.QueryInterface.Version = 2;
91  Stack->Parameters.QueryInterface.Interface = (PINTERFACE)BusInterface;
92  Stack->Parameters.QueryInterface.InterfaceSpecificData = NULL;
93  Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
94 
96 
97  if (Status == STATUS_PENDING)
98  {
100  Status = IoStatus.Status;
101  }
102 
103  return Status;
104 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
IN PLARGE_INTEGER IN ULONG IN BOOLEAN IN ULONG IN BOOLEAN OUT PIO_STATUS_BLOCK IoStatus
Definition: fatprocs.h:2650
PIRP NTAPI IoBuildSynchronousFsdRequest(IN ULONG MajorFunction, IN PDEVICE_OBJECT DeviceObject, IN PVOID Buffer, IN ULONG Length, IN PLARGE_INTEGER StartingOffset, IN PKEVENT Event, IN PIO_STATUS_BLOCK IoStatusBlock)
Definition: irp.c:1069
_In_ PIRP Irp
Definition: csq.h:116
#define IRP_MJ_PNP
Definition: cdrw_usr.h:52
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:435
smooth NULL
Definition: ftsmooth.c:416
struct _INTERFACE * PINTERFACE
#define STATUS_PENDING
Definition: ntstatus.h:82
#define IRP_MN_QUERY_INTERFACE
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2647
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
struct _BUS_INTERFACE_STANDARD BUS_INTERFACE_STANDARD
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
GUID * LPGUID
Definition: guiddef.h:81

◆ USBSTOR_GetDescriptors()

NTSTATUS USBSTOR_GetDescriptors ( IN PDEVICE_OBJECT  DeviceObject)

Definition at line 75 of file descriptor.c.

77 {
79  PFDO_DEVICE_EXTENSION DeviceExtension;
80  USHORT DescriptorLength;
81 
83 
84  // first get device descriptor
85  Status = USBSTOR_GetDescriptor(DeviceExtension->LowerDeviceObject, USB_DEVICE_DESCRIPTOR_TYPE, sizeof(USB_DEVICE_DESCRIPTOR), 0, 0, (PVOID*)&DeviceExtension->DeviceDescriptor);
86  if (!NT_SUCCESS(Status))
87  {
88  DeviceExtension->DeviceDescriptor = NULL;
89  return Status;
90  }
91 
92  // now get basic configuration descriptor
93  Status = USBSTOR_GetDescriptor(DeviceExtension->LowerDeviceObject, USB_CONFIGURATION_DESCRIPTOR_TYPE, sizeof(USB_CONFIGURATION_DESCRIPTOR), 0, 0, (PVOID*)&DeviceExtension->ConfigurationDescriptor);
94  if (!NT_SUCCESS(Status))
95  {
96  FreeItem(DeviceExtension->DeviceDescriptor);
97  DeviceExtension->DeviceDescriptor = NULL;
98  return Status;
99  }
100 
101  // backup length
102  DescriptorLength = DeviceExtension->ConfigurationDescriptor->wTotalLength;
103 
104  // release basic descriptor
105  FreeItem(DeviceExtension->ConfigurationDescriptor);
106  DeviceExtension->ConfigurationDescriptor = NULL;
107 
108  // allocate full descriptor
109  Status = USBSTOR_GetDescriptor(DeviceExtension->LowerDeviceObject, USB_CONFIGURATION_DESCRIPTOR_TYPE, DescriptorLength, 0, 0, (PVOID*)&DeviceExtension->ConfigurationDescriptor);
110  if (!NT_SUCCESS(Status))
111  {
112  FreeItem(DeviceExtension->DeviceDescriptor);
113  DeviceExtension->DeviceDescriptor = NULL;
114  return Status;
115  }
116 
117  // check if there is a serial number provided
118  if (DeviceExtension->DeviceDescriptor->iSerialNumber)
119  {
120  // get serial number
121  Status = USBSTOR_GetDescriptor(DeviceExtension->LowerDeviceObject, USB_STRING_DESCRIPTOR_TYPE, 100 * sizeof(WCHAR), DeviceExtension->DeviceDescriptor->iSerialNumber, 0x0409, (PVOID*)&DeviceExtension->SerialNumber);
122  if (!NT_SUCCESS(Status))
123  {
124  FreeItem(DeviceExtension->DeviceDescriptor);
125  DeviceExtension->DeviceDescriptor = NULL;
126 
127  FreeItem(DeviceExtension->ConfigurationDescriptor);
128  DeviceExtension->ConfigurationDescriptor = NULL;
129 
130  DeviceExtension->SerialNumber = NULL;
131  return Status;
132  }
133  }
134 
135  return Status;
136 }
LONG NTSTATUS
Definition: precomp.h:26
#define USB_STRING_DESCRIPTOR_TYPE
Definition: usb100.h:51
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
NTSTATUS NTAPI USBSTOR_GetDescriptor(IN PDEVICE_OBJECT DeviceObject, IN UCHAR DescriptorType, IN ULONG DescriptorLength, IN UCHAR DescriptorIndex, IN LANGID LanguageId, OUT PVOID *OutDescriptor)
Definition: descriptor.c:18
#define USB_DEVICE_DESCRIPTOR_TYPE
Definition: usb100.h:49
#define USB_CONFIGURATION_DESCRIPTOR_TYPE
Definition: usb100.h:50
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
unsigned short USHORT
Definition: pedump.c:61

◆ USBSTOR_GetEndpointStatus()

NTSTATUS USBSTOR_GetEndpointStatus ( IN PDEVICE_OBJECT  DeviceObject,
IN UCHAR  bEndpointAddress,
OUT PUSHORT  Value 
)

Definition at line 18 of file error.c.

22 {
23  PURB Urb;
25 
26  DPRINT("Allocating URB\n");
28  if (!Urb)
29  {
30  DPRINT1("OutofMemory!\n");
32  }
33 
34  // build status
36 
37  // send the request
38  DPRINT1("Sending Request DeviceObject %p, Urb %p\n", DeviceObject, Urb);
40 
41  FreeItem(Urb);
42  return Status;
43 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define UsbBuildGetStatusRequest(urb, op, index, transferBuffer, transferBufferMDL, link)
Definition: usbdlib.h:35
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
LONG NTSTATUS
Definition: precomp.h:26
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define URB_FUNCTION_GET_STATUS_FROM_ENDPOINT
Definition: usb.h:107
struct _URB * PURB
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
Definition: usb.h:529
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:107
#define DPRINT1
Definition: precomp.h:8

◆ USBSTOR_GetMaxLUN()

NTSTATUS USBSTOR_GetMaxLUN ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension 
)

Definition at line 204 of file misc.c.

207 {
208  PUCHAR Buffer;
210 
212  if (!Buffer)
213  {
214  FreeItem(Buffer);
216  }
217 
218  Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_GET_MAX_LUN, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_IN, sizeof(UCHAR), Buffer);
219 
220  DPRINT("MaxLUN: %x\n", *Buffer);
221 
222  if (NT_SUCCESS(Status))
223  {
224  if (*Buffer > 0xF)
225  {
226  // invalid response documented in usb mass storage specification
228  }
229  else
230  {
231  // store maxlun
232  DeviceExtension->MaxLUN = *Buffer;
233  }
234  }
235  else
236  {
237  // "USB Mass Storage Class. Bulk-Only Transport. Revision 1.0"
238  // 3.2 Get Max LUN (class-specific request) :
239  // Devices that do not support multiple LUNs may STALL this command.
240  USBSTOR_ResetDevice(DeviceExtension->LowerDeviceObject, DeviceExtension);
241 
242  DeviceExtension->MaxLUN = 0;
244  }
245 
246  FreeItem(Buffer);
247  return Status;
248 }
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
#define USBD_TRANSFER_DIRECTION_IN
Definition: usb.h:160
NTSTATUS USBSTOR_ResetDevice(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:251
NTSTATUS USBSTOR_ClassRequest(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension, IN UCHAR RequestType, IN USHORT Index, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer)
Definition: misc.c:170
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned char UCHAR
Definition: xmlstorage.h:181
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
#define USB_BULK_GET_MAX_LUN
Definition: usbstor.h:98
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
return STATUS_SUCCESS
Definition: btrfs.c:2966
#define STATUS_DEVICE_DATA_ERROR
Definition: udferr_usr.h:159

◆ USBSTOR_GetPipeHandles()

NTSTATUS USBSTOR_GetPipeHandles ( IN PFDO_DEVICE_EXTENSION  DeviceExtension)

Definition at line 319 of file descriptor.c.

321 {
322  ULONG Index;
323  BOOLEAN BulkInFound = FALSE, BulkOutFound = FALSE;
324 
325  // enumerate all pipes and extract bulk-in / bulk-out pipe handle
326  for (Index = 0; Index < DeviceExtension->InterfaceInformation->NumberOfPipes; Index++)
327  {
328  if (DeviceExtension->InterfaceInformation->Pipes[Index].PipeType == UsbdPipeTypeBulk)
329  {
330  if (USB_ENDPOINT_DIRECTION_IN(DeviceExtension->InterfaceInformation->Pipes[Index].EndpointAddress))
331  {
332  DeviceExtension->BulkInPipeIndex = Index;
333 
334  // there should not be another bulk in pipe
335  ASSERT(BulkInFound == FALSE);
336  BulkInFound = TRUE;
337  }
338  else
339  {
340  DeviceExtension->BulkOutPipeIndex = Index;
341 
342  // there should not be another bulk out pipe
343  ASSERT(BulkOutFound == FALSE);
344  BulkOutFound = TRUE;
345  }
346  }
347  }
348 
349  if (!BulkInFound || !BulkOutFound)
350  {
351  // WTF? usb port driver does not give us bulk pipe access
352  DPRINT1("USBSTOR_GetPipeHandles> BulkInFound %c BulkOutFound %c missing!!!\n", BulkInFound, BulkOutFound);
354  }
355 
356  return STATUS_SUCCESS;
357 }
#define TRUE
Definition: types.h:120
#define USB_ENDPOINT_DIRECTION_IN(x)
Definition: usb100.h:76
unsigned char BOOLEAN
static const UCHAR Index[8]
Definition: usbohci.c:18
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_DEVICE_CONFIGURATION_ERROR
Definition: ntstatus.h:605
return STATUS_SUCCESS
Definition: btrfs.c:2966

◆ USBSTOR_HandleDeviceControl()

NTSTATUS USBSTOR_HandleDeviceControl ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 418 of file disk.c.

421 {
422  PIO_STACK_LOCATION IoStack;
424  PPDO_DEVICE_EXTENSION PDODeviceExtension;
425  PSCSI_ADAPTER_BUS_INFO BusInfo;
426  PSCSI_INQUIRY_DATA ScsiInquiryData;
427  PINQUIRYDATA InquiryData;
428 
430 
431  switch (IoStack->Parameters.DeviceIoControl.IoControlCode)
432  {
435  break;
437  DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH NOT implemented\n");
439  break;
441  DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH_DIRECT NOT implemented\n");
443  break;
445  DPRINT1("USBSTOR_HandleDeviceControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER NOT implemented\n");
447  break;
449  {
451 
452  // Legacy port capability query
453  if (IoStack->Parameters.DeviceIoControl.OutputBufferLength == sizeof(PVOID))
454  {
455  Capabilities = *((PVOID *)Irp->AssociatedIrp.SystemBuffer) = ExAllocatePoolWithTag(NonPagedPool,
456  sizeof(IO_SCSI_CAPABILITIES),
457  USB_STOR_TAG);
458  Irp->IoStatus.Information = sizeof(PVOID);
459  }
460  else
461  {
462  Capabilities = Irp->AssociatedIrp.SystemBuffer;
463  Irp->IoStatus.Information = sizeof(IO_SCSI_CAPABILITIES);
464  }
465 
466  if (Capabilities)
467  {
468  Capabilities->MaximumTransferLength = USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH;
469  Capabilities->MaximumPhysicalPages = USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH / PAGE_SIZE + 1; // See CORE-10515 and CORE-10755
470  Capabilities->SupportedAsynchronousEvents = 0;
471  Capabilities->AlignmentMask = 0;
472  Capabilities->TaggedQueuing = FALSE;
473  Capabilities->AdapterScansDown = FALSE;
474  Capabilities->AdapterUsesPio = FALSE;
476  }
477  else
478  {
480  }
481 
482  break;
483  }
485  {
486  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
487  ASSERT(PDODeviceExtension);
488  ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
489 
490  // get parameters
491  BusInfo = Irp->AssociatedIrp.SystemBuffer;
492  ScsiInquiryData = (PSCSI_INQUIRY_DATA)(BusInfo + 1);
493  InquiryData = (PINQUIRYDATA)ScsiInquiryData->InquiryData;
494 
495 
496  BusInfo->NumberOfBuses = 1;
497  BusInfo->BusData[0].NumberOfLogicalUnits = 1; //FIXME
498  BusInfo->BusData[0].InitiatorBusId = 0;
499  BusInfo->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
500 
501  ScsiInquiryData->PathId = 0;
502  ScsiInquiryData->TargetId = 0;
503  ScsiInquiryData->Lun = PDODeviceExtension->LUN & MAX_LUN;
504  ScsiInquiryData->DeviceClaimed = PDODeviceExtension->Claimed;
505  ScsiInquiryData->InquiryDataLength = sizeof(INQUIRYDATA);
506  ScsiInquiryData->NextInquiryDataOffset = 0;
507 
508  // Note: INQUIRYDATA structure is larger than INQUIRYDATABUFFERSIZE
509  RtlZeroMemory(InquiryData, sizeof(INQUIRYDATA));
510  RtlCopyMemory(InquiryData, PDODeviceExtension->InquiryData, INQUIRYDATABUFFERSIZE);
511 
512  InquiryData->Versions = 0x04;
513  InquiryData->ResponseDataFormat = 0x02; // some devices set this to 1
514 
515  Irp->IoStatus.Information = sizeof(SCSI_ADAPTER_BUS_INFO) + sizeof(SCSI_INQUIRY_DATA) + sizeof(INQUIRYDATA) - 1;
517 
518  break;
519  }
521  {
522  PSCSI_ADDRESS Address = Irp->AssociatedIrp.SystemBuffer;
523 
524  Address->Length = sizeof(SCSI_ADDRESS);
525  Address->PortNumber = 0;
526  Address->PathId = 0;
527  Address->TargetId = 0;
529  Irp->IoStatus.Information = sizeof(SCSI_ADDRESS);
530 
532 
533  break;
534  }
535  default:
536  DPRINT("USBSTOR_HandleDeviceControl IoControl %x not supported\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
538  break;
539  }
540 
541  return Status;
542 }
struct _SCSI_ADAPTER_BUS_INFO SCSI_ADAPTER_BUS_INFO
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define IOCTL_SCSI_GET_ADDRESS
Definition: scsi_port.h:52
_In_ PIRP Irp
Definition: csq.h:116
#define USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH
Definition: usbstor.h:12
UCHAR ResponseDataFormat
Definition: cdrw_hw.h:1121
LONG NTSTATUS
Definition: precomp.h:26
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
struct _INQUIRYDATA * PINQUIRYDATA
ULONG NextInquiryDataOffset
Definition: scsi_port.h:118
SCSI_BUS_DATA BusData[1]
Definition: scsi_port.h:106
_Must_inspect_result_ typedef _Out_ PHIDP_CAPS Capabilities
Definition: hidclass.h:103
struct _SCSI_INQUIRY_DATA * PSCSI_INQUIRY_DATA
struct _INQUIRYDATA INQUIRYDATA
PVOID DeviceExtension
Definition: env_spec_w32.h:418
#define INQUIRYDATABUFFERSIZE
Definition: cdrw_hw.h:1113
static WCHAR Address[46]
Definition: ping.c:68
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:55
void DPRINT(...)
Definition: polytest.cpp:61
void * PVOID
Definition: retypes.h:9
ULONG InquiryDataOffset
Definition: scsi_port.h:98
#define IOCTL_STORAGE_QUERY_PROPERTY
Definition: ntddstor.h:167
#define IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER
Definition: ntddstor.h:140
#define MAX_LUN
Definition: usbstor.h:119
NTSTATUS USBSTOR_HandleQueryProperty(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: disk.c:226
UCHAR Versions
Definition: cdrw_hw.h:1120
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define USB_STOR_TAG
Definition: usbstor.h:10
#define PAGE_SIZE
Definition: env_spec_w32.h:49
UCHAR InquiryData[1]
Definition: scsi_port.h:119
Status
Definition: gdiplustypes.h:24
#define IOCTL_SCSI_PASS_THROUGH
Definition: scsi_port.h:47
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define IOCTL_SCSI_GET_INQUIRY_DATA
Definition: scsi_port.h:49
#define IOCTL_SCSI_PASS_THROUGH_DIRECT
Definition: scsi_port.h:51
ULONG InquiryDataLength
Definition: scsi_port.h:117
UCHAR InitiatorBusId
Definition: scsi_port.h:97
#define DPRINT1
Definition: precomp.h:8
BOOLEAN DeviceClaimed
Definition: scsi_port.h:116
#define IOCTL_SCSI_GET_CAPABILITIES
Definition: scsi_port.h:50
struct _SCSI_ADDRESS SCSI_ADDRESS
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
UCHAR NumberOfLogicalUnits
Definition: scsi_port.h:96
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
return STATUS_SUCCESS
Definition: btrfs.c:2966
struct _IO_SCSI_CAPABILITIES IO_SCSI_CAPABILITIES

◆ USBSTOR_HandleExecuteSCSI()

NTSTATUS USBSTOR_HandleExecuteSCSI ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 560 of file scsi.c.

563 {
565  PIO_STACK_LOCATION IoStack;
567  PPDO_DEVICE_EXTENSION PDODeviceExtension;
569 
570  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
571  ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
572 
574  Request = IoStack->Parameters.Scsi.Srb;
575 
576  DPRINT("USBSTOR_HandleExecuteSCSI Operation Code %x, Length %lu\n", SrbGetCdb(Request)->CDB10.OperationCode, Request->DataTransferLength);
577 
578  // check that we're sending to the right LUN
579  ASSERT(SrbGetCdb(Request)->CDB10.LogicalUnitNumber == (PDODeviceExtension->LUN & MAX_LUN));
581 
582  if (!Context)
583  {
585  }
586  else
587  {
588  Status = USBSTOR_SendCBWRequest(PDODeviceExtension->LowerDeviceObject->DeviceExtension, Irp, Context);
589  }
590 
591  return Status;
592 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ PIRP Irp
Definition: csq.h:116
LONG NTSTATUS
Definition: precomp.h:26
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
PVOID DeviceExtension
Definition: env_spec_w32.h:418
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:55
void DPRINT(...)
Definition: polytest.cpp:61
#define MAX_LUN
Definition: usbstor.h:119
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define USB_STOR_TAG
Definition: usbstor.h:10
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
#define SrbGetCdb(srb)
Definition: usbstor.h:33
struct tagContext Context
Definition: acpixf.h:1024
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
static NTSTATUS USBSTOR_SendCBWRequest(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN PIRP_CONTEXT Context)
Definition: scsi.c:474

Referenced by USBSTOR_HandleTransferError(), and USBSTOR_StartIo().

◆ USBSTOR_HandleInternalDeviceControl()

NTSTATUS USBSTOR_HandleInternalDeviceControl ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 81 of file disk.c.

84 {
85  PIO_STACK_LOCATION IoStack;
87  PPDO_DEVICE_EXTENSION PDODeviceExtension;
89 
91  Request = (PSCSI_REQUEST_BLOCK)IoStack->Parameters.Others.Argument1;
92  ASSERT(Request);
93  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
94  ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
95 
96  switch(Request->Function)
97  {
99  {
100  DPRINT("SRB_FUNCTION_EXECUTE_SCSI\n");
101 
102  if (!IsRequestValid(Irp))
103  {
105  break;
106  }
107 
108  if (Request->Cdb[0] == SCSIOP_MODE_SENSE)
109  {
110  DPRINT("USBSTOR_Scsi: SRB_FUNCTION_EXECUTE_SCSI - FIXME SCSIOP_MODE_SENSE\n");
111  // FIXME Get from registry WriteProtect for StorageDevicePolicies;
112  // L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\StorageDevicePolicies"
113  // QueryTable[0].Name = L"WriteProtect"
114  }
115 
117  Request->SrbStatus = SRB_STATUS_PENDING;
118 
119  // add the request
120  if (!USBSTOR_QueueAddIrp(PDODeviceExtension->LowerDeviceObject, Irp))
121  {
122  IoStartPacket(PDODeviceExtension->LowerDeviceObject, Irp, &Request->QueueSortKey, USBSTOR_CancelIo);
123  }
124 
125  return STATUS_PENDING;
126  }
128  {
129  DPRINT1("SRB_FUNCTION_RELEASE_DEVICE\n");
130  ASSERT(PDODeviceExtension->Claimed == TRUE);
131 
132  // release claim
133  PDODeviceExtension->Claimed = FALSE;
135  break;
136  }
138  {
139  DPRINT1("SRB_FUNCTION_CLAIM_DEVICE\n");
140 
141  // check if the device has been claimed
142  if (PDODeviceExtension->Claimed)
143  {
144  // device has already been claimed
146  Request->SrbStatus = SRB_STATUS_BUSY;
147  break;
148  }
149 
150  // claim device
151  PDODeviceExtension->Claimed = TRUE;
152 
153  // output device object
154  Request->DataBuffer = DeviceObject;
155 
157  break;
158  }
160  {
161  DPRINT1("SRB_FUNCTION_RELEASE_QUEUE\n");
162 
163  USBSTOR_QueueRelease(PDODeviceExtension->LowerDeviceObject);
164 
165  Request->SrbStatus = SRB_STATUS_SUCCESS;
167  break;
168  }
169 
171  case SRB_FUNCTION_FLUSH:
173  {
174  DPRINT1("SRB_FUNCTION_FLUSH / SRB_FUNCTION_FLUSH_QUEUE / SRB_FUNCTION_SHUTDOWN\n");
175 
176  // HACK: don't flush pending requests
177 #if 0 // we really need a proper storage stack
178  //
179  // wait for pending requests to finish
180  //
181  USBSTOR_QueueWaitForPendingRequests(PDODeviceExtension->LowerDeviceObject);
182 #endif
183 
184  Request->SrbStatus = SRB_STATUS_SUCCESS;
186  break;
187  }
188  default:
189  {
190  //
191  // not supported
192  //
194  Request->SrbStatus = SRB_STATUS_ERROR;
195  }
196  }
197 
198  Irp->IoStatus.Status = Status;
200  return Status;
201 }
#define TRUE
Definition: types.h:120
VOID NTAPI USBSTOR_CancelIo(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:28
struct _SCSI_REQUEST_BLOCK * PSCSI_REQUEST_BLOCK
_In_ PIRP Irp
Definition: csq.h:116
#define SCSIOP_MODE_SENSE
Definition: cdrw_hw.h:896
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
BOOLEAN USBSTOR_QueueAddIrp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:75
static BOOLEAN IsRequestValid(PIRP Irp)
Definition: disk.c:19
#define SRB_FUNCTION_FLUSH_QUEUE
Definition: srb.h:321
#define SRB_FUNCTION_RELEASE_QUEUE
Definition: srb.h:311
#define SRB_FUNCTION_CLAIM_DEVICE
Definition: srb.h:308
#define SRB_STATUS_ERROR
Definition: srb.h:336
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
VOID USBSTOR_QueueRelease(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:262
VOID NTAPI IoStartPacket(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PULONG Key, IN PDRIVER_CANCEL CancelFunction)
Definition: device.c:1876
#define SRB_STATUS_PENDING
Definition: srb.h:332
PVOID DeviceExtension
Definition: env_spec_w32.h:418
#define SRB_FUNCTION_RELEASE_DEVICE
Definition: srb.h:313
#define IoCompleteRequest
Definition: irp.c:1240
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:55
void DPRINT(...)
Definition: polytest.cpp:61
#define SRB_STATUS_BUSY
Definition: srb.h:337
#define SRB_FUNCTION_FLUSH
Definition: srb.h:315
#define STATUS_PENDING
Definition: ntstatus.h:82
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
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
#define SRB_FUNCTION_SHUTDOWN
Definition: srb.h:314
#define STATUS_DEVICE_BUSY
Definition: udferr_usr.h:129
#define DPRINT1
Definition: precomp.h:8
VOID USBSTOR_QueueWaitForPendingRequests(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:171
#define SRB_FUNCTION_EXECUTE_SCSI
Definition: srb.h:307
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
#define IO_NO_INCREMENT
Definition: iotypes.h:566
#define SRB_STATUS_SUCCESS
Definition: srb.h:333
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
return STATUS_SUCCESS
Definition: btrfs.c:2966
IoMarkIrpPending(Irp)

◆ USBSTOR_IsFloppy()

BOOLEAN USBSTOR_IsFloppy ( IN PUCHAR  Buffer,
IN ULONG  BufferLength,
OUT PUCHAR  MediumTypeCode 
)

Definition at line 427 of file misc.c.

431 {
432  PUFI_CAPACITY_FORMAT_HEADER FormatHeader;
434  ULONG Length, Index, BlockCount, BlockLength;
435 
436  //
437  // get format header
438  //
439  FormatHeader = (PUFI_CAPACITY_FORMAT_HEADER)Buffer;
440 
441  //
442  // sanity checks
443  //
444  ASSERT(FormatHeader->Reserved1 == 0x00);
445  ASSERT(FormatHeader->Reserved2 == 0x00);
446  ASSERT(FormatHeader->Reserved3 == 0x00);
447 
448  //
449  // is there capacity data
450  //
451  if (!FormatHeader->CapacityLength)
452  {
453  //
454  // no data provided
455  //
456  DPRINT1("[USBSTOR] No capacity length\n");
457  return FALSE;
458  }
459 
460  //
461  // the format header are always 8 bytes in length
462  //
463  ASSERT((FormatHeader->CapacityLength & 0x7) == 0);
464  DPRINT1("CapacityLength %x\n", FormatHeader->CapacityLength);
465 
466  //
467  // grab length and locate first descriptor
468  //
469  Length = FormatHeader->CapacityLength;
470  Descriptor = (PUFI_CAPACITY_DESCRIPTOR)(FormatHeader + 1);
471  for(Index = 0; Index < Length / sizeof(UFI_CAPACITY_DESCRIPTOR); Index++)
472  {
473  //
474  // blocks are little endian format
475  //
476  BlockCount = NTOHL(Descriptor->BlockCount);
477 
478  //
479  // get block length
480  //
481  BlockLength = NTOHL((Descriptor->BlockLengthByte0 << 24 | Descriptor->BlockLengthByte1 << 16 | Descriptor->BlockLengthByte2 << 8));
482 
483  DPRINT1("BlockCount %x BlockLength %x Code %x\n", BlockCount, BlockLength, Descriptor->Code);
484 
485  if (BlockLength == 512 && BlockCount == 1440)
486  {
487  //
488  // 720 KB DD
489  //
490  *MediumTypeCode = 0x1E;
491  return TRUE;
492  }
493  else if (BlockLength == 1024 && BlockCount == 1232)
494  {
495  //
496  // 1,25 MB
497  //
498  *MediumTypeCode = 0x93;
499  return TRUE;
500  }
501  else if (BlockLength == 512 && BlockCount == 2880)
502  {
503  //
504  // 1,44MB KB DD
505  //
506  *MediumTypeCode = 0x94;
507  return TRUE;
508  }
509 
510  //
511  // move to next descriptor
512  //
513  Descriptor = (Descriptor + 1);
514  }
515 
516  //
517  // no floppy detected
518  //
519  return FALSE;
520 }
#define TRUE
Definition: types.h:120
struct UFI_CAPACITY_DESCRIPTOR * PUFI_CAPACITY_DESCRIPTOR
Definition: bufpool.h:45
static const UCHAR Index[8]
Definition: usbohci.c:18
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define NTOHL(n)
Definition: usbstor.h:23
struct UFI_CAPACITY_FORMAT_HEADER * PUFI_CAPACITY_FORMAT_HEADER
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
_In_ PSTORAGE_PROPERTY_ID _Outptr_ PSTORAGE_DESCRIPTOR_HEADER * Descriptor
Definition: classpnp.h:966

Referenced by USBSTOR_SendFormatCapacityIrp().

◆ USBSTOR_PdoHandlePnp()

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

Definition at line 549 of file pdo.c.

552 {
553  PIO_STACK_LOCATION IoStack;
554  PPDO_DEVICE_EXTENSION DeviceExtension;
557  ULONG bDelete;
558 
561  ASSERT(DeviceExtension->Common.IsFDO == FALSE);
562 
563  switch(IoStack->MinorFunction)
564  {
566  {
568  break;
569  }
571  {
573  break;
574  }
575  case IRP_MN_QUERY_ID:
576  {
577  if (IoStack->Parameters.QueryId.IdType == BusQueryDeviceID)
578  {
580  break;
581  }
582  else if (IoStack->Parameters.QueryId.IdType == BusQueryHardwareIDs)
583  {
585  break;
586  }
587  else if (IoStack->Parameters.QueryId.IdType == BusQueryInstanceID)
588  {
590  break;
591  }
592  else if (IoStack->Parameters.QueryId.IdType == BusQueryCompatibleIDs)
593  {
595  break;
596  }
597 
598  DPRINT1("USBSTOR_PdoHandlePnp: IRP_MN_QUERY_ID IdType %x unimplemented\n", IoStack->Parameters.QueryId.IdType);
600  Irp->IoStatus.Information = 0;
601  break;
602  }
604  {
605  DPRINT("IRP_MN_REMOVE_DEVICE\n");
606 
607  if(*DeviceExtension->PDODeviceObject != NULL)
608  {
609  *DeviceExtension->PDODeviceObject = NULL;
610  bDelete = TRUE;
611  }
612  else
613  {
614  // device object already marked for deletion
615  bDelete = FALSE;
616  }
617 
618  // clean up the device extension
619  ASSERT(DeviceExtension->InquiryData);
620  ExFreePoolWithTag(DeviceExtension->InquiryData, USB_STOR_TAG);
621 
622  Irp->IoStatus.Status = STATUS_SUCCESS;
624 
625  if (bDelete)
626  {
628  }
629  return STATUS_SUCCESS;
630  }
632  {
633  // just forward irp to lower device
634  Status = USBSTOR_SyncForwardIrp(DeviceExtension->LowerDeviceObject, Irp);
636 
637  if (NT_SUCCESS(Status))
638  {
639  // check if no unique id
640  Caps = (PDEVICE_CAPABILITIES)IoStack->Parameters.DeviceCapabilities.Capabilities;
641  Caps->UniqueID = FALSE; // no unique id is supported
642  Caps->Removable = TRUE; //FIXME
643  }
644  break;
645  }
648  {
649 #if 0
650  //
651  // if we're not claimed it's ok
652  //
653  if (DeviceExtension->Claimed)
654 #else
655  if (TRUE)
656 #endif
657  {
659  DPRINT1("[USBSTOR] Request %x fails because device is still claimed\n", IoStack->MinorFunction);
660  }
661  else
663  break;
664  }
665  case IRP_MN_START_DEVICE:
666  {
667  // no-op for PDO
669  break;
670  }
672  {
674  break;
675  }
676  default:
677  {
678  // do nothing
679  Status = Irp->IoStatus.Status;
680  }
681  }
682 
683  if (Status != STATUS_PENDING)
684  {
685  Irp->IoStatus.Status = Status;
687  }
688 
689  return Status;
690 }
#define TRUE
Definition: types.h:120
#define IRP_MN_REMOVE_DEVICE
#define IRP_MN_QUERY_ID
NTSTATUS USBSTOR_PdoHandleDeviceRelations(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:515
_In_ PIRP Irp
Definition: csq.h:116
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS USBSTOR_PdoHandleQueryDeviceText(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: pdo.c:179
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
NTSTATUS USBSTOR_PdoHandleQueryDeviceId(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: pdo.c:234
#define IRP_MN_QUERY_REMOVE_DEVICE
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
#define IoCompleteRequest
Definition: irp.c:1240
NTSTATUS NTAPI USBSTOR_SyncForwardIrp(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: misc.c:34
NTSTATUS USBSTOR_PdoHandleQueryInstanceId(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:472
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:55
void DPRINT(...)
Definition: polytest.cpp:61
#define IRP_MN_QUERY_STOP_DEVICE
#define IRP_MN_SURPRISE_REMOVAL
Definition: ntifs_ex.h:408
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
#define IRP_MN_START_DEVICE
#define IRP_MN_QUERY_DEVICE_TEXT
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
NTSTATUS USBSTOR_PdoHandleQueryCompatibleId(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:435
#define USB_STOR_TAG
Definition: usbstor.h:10
NTSTATUS USBSTOR_PdoHandleQueryHardwareId(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:323
* PDEVICE_CAPABILITIES
Definition: iotypes.h:928
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
VOID NTAPI IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject)
Definition: device.c:1251
#define IRP_MN_QUERY_DEVICE_RELATIONS
#define DPRINT1
Definition: precomp.h:8
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:566
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
return STATUS_SUCCESS
Definition: btrfs.c:2966
#define IRP_MN_QUERY_CAPABILITIES

◆ USBSTOR_QueueAddIrp()

BOOLEAN USBSTOR_QueueAddIrp ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 75 of file queue.c.

78 {
79  PDRIVER_CANCEL OldDriverCancel;
80  KIRQL OldLevel;
81  PFDO_DEVICE_EXTENSION FDODeviceExtension;
82  BOOLEAN IrpListFreeze;
83  BOOLEAN SrbProcessing;
85  PSCSI_REQUEST_BLOCK Request = (PSCSI_REQUEST_BLOCK)IoStack->Parameters.Others.Argument1;
86 
87  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
88  ASSERT(FDODeviceExtension->Common.IsFDO);
89 
91 
92  KeAcquireSpinLock(&FDODeviceExtension->IrpListLock, &OldLevel);
93 
94  SrbProcessing = FDODeviceExtension->IrpPendingCount != 0;
95 
96  if (SrbProcessing)
97  {
98  // add irp to queue
99  InsertTailList(&FDODeviceExtension->IrpListHead, &Irp->Tail.Overlay.ListEntry);
100  }
101 
102  FDODeviceExtension->IrpPendingCount++;
103  KeClearEvent(&FDODeviceExtension->NoPendingRequests);
104 
105  // check if queue is freezed
106  IrpListFreeze = BooleanFlagOn(FDODeviceExtension->Flags, USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE);
107 
108  KeReleaseSpinLock(&FDODeviceExtension->IrpListLock, OldLevel);
109 
110  // synchronize with cancellations by holding the cancel lock
111  IoAcquireCancelSpinLock(&Irp->CancelIrql);
112 
113  if (SrbProcessing)
114  {
115  ASSERT(FDODeviceExtension->ActiveSrb != NULL);
116 
117  OldDriverCancel = IoSetCancelRoutine(Irp, USBSTOR_Cancel);
118  }
119  else
120  {
121  ASSERT(FDODeviceExtension->ActiveSrb == NULL);
122 
123  FDODeviceExtension->ActiveSrb = Request;
124  OldDriverCancel = IoSetCancelRoutine(Irp, USBSTOR_CancelIo);
125  }
126 
127  // check if the irp has already been cancelled
128  if (Irp->Cancel && OldDriverCancel == NULL)
129  {
130  // cancel irp
131  Irp->CancelRoutine(DeviceObject, Irp);
132  return FALSE;
133  }
134 
135  IoReleaseCancelSpinLock(Irp->CancelIrql);
136 
137  // if list is freezed, dont start this packet
138  DPRINT("IrpListFreeze: %lu IrpPendingCount %lu\n", IrpListFreeze, FDODeviceExtension->IrpPendingCount);
139 
140  return (IrpListFreeze || SrbProcessing);
141 }
DRIVER_CANCEL * PDRIVER_CANCEL
Definition: iotypes.h:2405
VOID NTAPI USBSTOR_CancelIo(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:28
struct _SCSI_REQUEST_BLOCK * PSCSI_REQUEST_BLOCK
_In_ PIRP Irp
Definition: csq.h:116
#define BooleanFlagOn(F, SF)
Definition: ext2fs.h:183
LIST_ENTRY ListEntry
Definition: pci.h:82
VOID NTAPI IoAcquireCancelSpinLock(OUT PKIRQL Irql)
Definition: util.c:56
#define InsertTailList(ListHead, Entry)
IoSetCancelRoutine(Irp, CancelRoutine)
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
UCHAR KIRQL
Definition: env_spec_w32.h:591
PVOID DeviceExtension
Definition: env_spec_w32.h:418
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
VOID NTAPI IoReleaseCancelSpinLock(IN KIRQL Irql)
Definition: util.c:150
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE
Definition: usbstor.h:46
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
IoMarkIrpPending(Irp)
VOID NTAPI USBSTOR_Cancel(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:51

◆ USBSTOR_QueueInitialize()

VOID USBSTOR_QueueInitialize ( PFDO_DEVICE_EXTENSION  FDODeviceExtension)

Definition at line 17 of file queue.c.

19 {
20  ASSERT(FDODeviceExtension->Common.IsFDO);
21  KeInitializeSpinLock(&FDODeviceExtension->IrpListLock);
22  InitializeListHead(&FDODeviceExtension->IrpListHead);
23  KeInitializeEvent(&FDODeviceExtension->NoPendingRequests, NotificationEvent, TRUE);
24 }
#define TRUE
Definition: types.h:120
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:251
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944

◆ USBSTOR_QueueNextRequest()

VOID USBSTOR_QueueNextRequest ( IN PDEVICE_OBJECT  DeviceObject)

Definition at line 220 of file queue.c.

222 {
223  PFDO_DEVICE_EXTENSION FDODeviceExtension;
224  PIRP Irp;
225  PIO_STACK_LOCATION IoStack;
227 
228  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
229  ASSERT(FDODeviceExtension->Common.IsFDO);
230 
231  // check first if there's already a request pending or the queue is frozen
232  if (FDODeviceExtension->ActiveSrb != NULL ||
234  {
235  // no work to do yet
236  return;
237  }
238 
239  // remove first irp from list
241 
242  // is there an irp pending
243  if (!Irp)
244  {
245  // no work to do
247  return;
248  }
249 
251  Request = (PSCSI_REQUEST_BLOCK)IoStack->Parameters.Others.Argument1;
252  ASSERT(Request);
253 
254  FDODeviceExtension->ActiveSrb = Request;
255 
256  // start next packet
259 }
#define TRUE
Definition: types.h:120
VOID NTAPI USBSTOR_CancelIo(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:28
struct _SCSI_REQUEST_BLOCK * PSCSI_REQUEST_BLOCK
_In_ PIRP Irp
Definition: csq.h:116
#define BooleanFlagOn(F, SF)
Definition: ext2fs.h:183
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
VOID NTAPI IoStartPacket(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PULONG Key, IN PDRIVER_CANCEL CancelFunction)
Definition: device.c:1876
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE
Definition: usbstor.h:46
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
PIRP USBSTOR_RemoveIrp(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:144
VOID NTAPI IoStartNextPacket(IN PDEVICE_OBJECT DeviceObject, IN BOOLEAN Cancelable)
Definition: device.c:1847

◆ USBSTOR_QueueRelease()

VOID USBSTOR_QueueRelease ( IN PDEVICE_OBJECT  DeviceObject)

Definition at line 262 of file queue.c.

264 {
265  PFDO_DEVICE_EXTENSION FDODeviceExtension;
266  PIRP Irp;
267  KIRQL OldLevel;
268  PIO_STACK_LOCATION IoStack;
270 
271  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
272  ASSERT(FDODeviceExtension->Common.IsFDO);
273 
274  KeAcquireSpinLock(&FDODeviceExtension->IrpListLock, &OldLevel);
275 
276  // clear freezed status
277  FDODeviceExtension->Flags &= ~USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE;
278 
279  KeReleaseSpinLock(&FDODeviceExtension->IrpListLock, OldLevel);
280 
281  // grab newest irp
283 
284  if (!Irp)
285  {
286  return;
287  }
288 
290  Request = (PSCSI_REQUEST_BLOCK)IoStack->Parameters.Others.Argument1;
291 
293  Irp,
294  &Request->QueueSortKey,
296 }
VOID NTAPI USBSTOR_CancelIo(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:28
struct _SCSI_REQUEST_BLOCK * PSCSI_REQUEST_BLOCK
_In_ PIRP Irp
Definition: csq.h:116
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
UCHAR KIRQL
Definition: env_spec_w32.h:591
VOID NTAPI IoStartPacket(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PULONG Key, IN PDRIVER_CANCEL CancelFunction)
Definition: device.c:1876
PVOID DeviceExtension
Definition: env_spec_w32.h:418
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE
Definition: usbstor.h:46
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
PIRP USBSTOR_RemoveIrp(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:144

◆ USBSTOR_QueueResetDevice()

VOID NTAPI USBSTOR_QueueResetDevice ( IN PFDO_DEVICE_EXTENSION  FDODeviceExtension)

Definition at line 149 of file error.c.

151 {
152  KIRQL OldIrql;
153 
154  DPRINT("USBSTOR_QueueResetDevice\n");
155 
156  KeAcquireSpinLock(&FDODeviceExtension->CommonLock, &OldIrql);
157  FDODeviceExtension->Flags |= USBSTOR_FDO_FLAGS_DEVICE_RESETTING;
158  KeReleaseSpinLock(&FDODeviceExtension->CommonLock, OldIrql);
159 
160  IoQueueWorkItem(FDODeviceExtension->ResetDeviceWorkItem,
163  NULL);
164 }
UCHAR KIRQL
Definition: env_spec_w32.h:591
VOID NTAPI USBSTOR_ResetDeviceWorkItemRoutine(IN PDEVICE_OBJECT FdoDevice, IN PVOID Context)
Definition: error.c:93
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
#define USBSTOR_FDO_FLAGS_DEVICE_RESETTING
Definition: usbstor.h:45
VOID NTAPI IoQueueWorkItem(IN PIO_WORKITEM IoWorkItem, IN PIO_WORKITEM_ROUTINE WorkerRoutine, IN WORK_QUEUE_TYPE QueueType, IN PVOID Context)
Definition: iowork.c:40
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627

Referenced by USBSTOR_CBWCompletionRoutine(), USBSTOR_CSWCompletionRoutine(), and USBSTOR_DataCompletionRoutine().

◆ USBSTOR_QueueResetPipe()

VOID NTAPI USBSTOR_QueueResetPipe ( IN PFDO_DEVICE_EXTENSION  FDODeviceExtension,
IN PIRP_CONTEXT  Context 
)

Definition at line 135 of file error.c.

138 {
139  DPRINT("USBSTOR_QueueResetPipe\n");
140 
141  IoQueueWorkItem(FDODeviceExtension->ResetDeviceWorkItem,
144  Context);
145 }
VOID NTAPI USBSTOR_ResetPipeWorkItemRoutine(IN PDEVICE_OBJECT FdoDevice, IN PVOID Ctx)
Definition: error.c:75
void DPRINT(...)
Definition: polytest.cpp:61
VOID NTAPI IoQueueWorkItem(IN PIO_WORKITEM IoWorkItem, IN PIO_WORKITEM_ROUTINE WorkerRoutine, IN WORK_QUEUE_TYPE QueueType, IN PVOID Context)
Definition: iowork.c:40

Referenced by USBSTOR_CSWCompletionRoutine(), and USBSTOR_DataCompletionRoutine().

◆ USBSTOR_QueueTerminateRequest()

VOID USBSTOR_QueueTerminateRequest ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 186 of file queue.c.

189 {
190  KIRQL OldLevel;
191  PFDO_DEVICE_EXTENSION FDODeviceExtension;
193  PSCSI_REQUEST_BLOCK Request = (PSCSI_REQUEST_BLOCK)IoStack->Parameters.Others.Argument1;
194 
195  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)FDODeviceObject->DeviceExtension;
196  ASSERT(FDODeviceExtension->Common.IsFDO);
197 
198  KeAcquireSpinLock(&FDODeviceExtension->IrpListLock, &OldLevel);
199 
200  FDODeviceExtension->IrpPendingCount--;
201 
202  // check if this was our current active SRB
203  if (FDODeviceExtension->ActiveSrb == Request)
204  {
205  // indicate processing is completed
206  FDODeviceExtension->ActiveSrb = NULL;
207  }
208 
209  // Set the event if nothing else is pending
210  if (FDODeviceExtension->IrpPendingCount == 0 &&
211  FDODeviceExtension->ActiveSrb == NULL)
212  {
213  KeSetEvent(&FDODeviceExtension->NoPendingRequests, IO_NO_INCREMENT, FALSE);
214  }
215 
216  KeReleaseSpinLock(&FDODeviceExtension->IrpListLock, OldLevel);
217 }
struct _SCSI_REQUEST_BLOCK * PSCSI_REQUEST_BLOCK
_In_ PIRP Irp
Definition: csq.h:116
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
UCHAR KIRQL
Definition: env_spec_w32.h:591
smooth NULL
Definition: ftsmooth.c:416
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define IO_NO_INCREMENT
Definition: iotypes.h:566
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772

◆ USBSTOR_QueueWaitForPendingRequests()

VOID USBSTOR_QueueWaitForPendingRequests ( IN PDEVICE_OBJECT  DeviceObject)

Definition at line 171 of file queue.c.

173 {
174  PFDO_DEVICE_EXTENSION FDODeviceExtension;
175 
176  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
177 
178  KeWaitForSingleObject(&FDODeviceExtension->NoPendingRequests,
179  Executive,
180  KernelMode,
181  FALSE,
182  NULL);
183 }
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560

◆ USBSTOR_ResetDevice()

NTSTATUS USBSTOR_ResetDevice ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension 
)

Definition at line 251 of file misc.c.

254 {
256 
257  Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_RESET_DEVICE, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_OUT, 0, NULL);
258  return Status;
259 }
#define USB_BULK_RESET_DEVICE
Definition: usbstor.h:99
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS USBSTOR_ClassRequest(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension, IN UCHAR RequestType, IN USHORT Index, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer)
Definition: misc.c:170
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
smooth NULL
Definition: ftsmooth.c:416
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560

◆ USBSTOR_ResetPipeWithHandle()

NTSTATUS USBSTOR_ResetPipeWithHandle ( IN PDEVICE_OBJECT  DeviceObject,
IN USBD_PIPE_HANDLE  PipeHandle 
)

Definition at line 46 of file error.c.

49 {
50  PURB Urb;
52 
53  DPRINT("Allocating URB\n");
54  Urb = (PURB)AllocateItem(NonPagedPool, sizeof(struct _URB_PIPE_REQUEST));
55  if (!Urb)
56  {
57  DPRINT1("OutofMemory!\n");
59  }
60 
61  Urb->UrbPipeRequest.Hdr.Length = sizeof(struct _URB_PIPE_REQUEST);
63  Urb->UrbPipeRequest.PipeHandle = PipeHandle;
64 
65  // send the request
66  DPRINT1("Sending Request DeviceObject %p, Urb %p\n", DeviceObject, Urb);
68 
69  FreeItem(Urb);
70  return Status;
71 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
LONG NTSTATUS
Definition: precomp.h:26
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
void DPRINT(...)
Definition: polytest.cpp:61
#define URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL
Definition: usb.h:116
struct _URB_PIPE_REQUEST UrbPipeRequest
Definition: usb.h:534
struct _URB * PURB
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
Definition: usb.h:529
static HANDLE PipeHandle
Definition: dhcpcsvc.c:21
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:107
#define DPRINT1
Definition: precomp.h:8

◆ USBSTOR_SelectConfigurationAndInterface()

NTSTATUS USBSTOR_SelectConfigurationAndInterface ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension 
)

Definition at line 234 of file descriptor.c.

237 {
238  PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
239  PUSB_ENDPOINT_DESCRIPTOR InEndpointDescriptor, OutEndpointDescriptor;
241  PURB Urb;
243 
244  Status = USBSTOR_ScanConfigurationDescriptor(DeviceExtension->ConfigurationDescriptor, &InterfaceDescriptor, &InEndpointDescriptor, &OutEndpointDescriptor);
245  if (!NT_SUCCESS(Status))
246  {
247  return Status;
248  }
249 
250  // now allocate one interface entry and terminating null entry
252  if (!InterfaceList)
253  {
255  }
256 
257  // initialize interface list entry
258  InterfaceList[0].InterfaceDescriptor = InterfaceDescriptor;
259 
260  // now allocate the urb
261  Urb = USBD_CreateConfigurationRequestEx(DeviceExtension->ConfigurationDescriptor, InterfaceList);
262  if (!Urb)
263  {
266  }
267 
268 
270 
271  // submit urb
272  Status = USBSTOR_SyncUrbRequest(DeviceExtension->LowerDeviceObject, Urb);
273  if (!NT_SUCCESS(Status))
274  {
275  // failed to set configuration
276  DPRINT1("USBSTOR_SelectConfiguration failed to set interface %x\n", Status);
278  ExFreePoolWithTag(Urb, 0);
279  return Status;
280  }
281 
282  // backup interface information
283  DeviceExtension->InterfaceInformation = (PUSBD_INTERFACE_INFORMATION)AllocateItem(NonPagedPool, Urb->UrbSelectConfiguration.Interface.Length);
284  if (!DeviceExtension->InterfaceInformation)
285  {
287  ExFreePoolWithTag(Urb, 0);
289  }
290 
291  // copy interface information
292  RtlCopyMemory(DeviceExtension->InterfaceInformation, &Urb->UrbSelectConfiguration.Interface, Urb->UrbSelectConfiguration.Interface.Length);
293 
294  // store pipe handle
295  DeviceExtension->ConfigurationHandle = Urb->UrbSelectConfiguration.ConfigurationHandle;
296 
297  // now prepare interface urb
298  UsbBuildSelectInterfaceRequest(Urb, GET_SELECT_INTERFACE_REQUEST_SIZE(InterfaceDescriptor->bNumEndpoints), DeviceExtension->ConfigurationHandle, InterfaceDescriptor->bInterfaceNumber, InterfaceDescriptor->bAlternateSetting);
299 
300  // copy interface information structure back - as offset for SelectConfiguration / SelectInterface request do differ
301  RtlCopyMemory(&Urb->UrbSelectInterface.Interface, DeviceExtension->InterfaceInformation, DeviceExtension->InterfaceInformation->Length);
302 
303  // now select the interface
304  Status = USBSTOR_SyncUrbRequest(DeviceExtension->LowerDeviceObject, Urb);
305  if (NT_SUCCESS(Status))
306  {
307  // update configuration info
308  ASSERT(Urb->UrbSelectInterface.Interface.Length == DeviceExtension->InterfaceInformation->Length);
309  RtlCopyMemory(DeviceExtension->InterfaceInformation, &Urb->UrbSelectInterface.Interface, Urb->UrbSelectInterface.Interface.Length);
310  }
311 
313  ExFreePoolWithTag(Urb, 0);
314 
315  return Status;
316 }
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
struct _USBD_INTERFACE_LIST_ENTRY * PUSBD_INTERFACE_LIST_ENTRY
_In_ PUSBD_INTERFACE_LIST_ENTRY InterfaceList
Definition: usbdlib.h:168
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
LONG NTSTATUS
Definition: precomp.h:26
#define GET_SELECT_INTERFACE_REQUEST_SIZE(totalPipes)
Definition: usbdlib.h:104
struct _USBD_INTERFACE_INFORMATION * PUSBD_INTERFACE_INFORMATION
Definition: usbdlib.h:7
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
NTSTATUS NTAPI USBSTOR_ScanConfigurationDescriptor(IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor, OUT PUSB_INTERFACE_DESCRIPTOR *OutInterfaceDescriptor, OUT PUSB_ENDPOINT_DESCRIPTOR *InEndpointDescriptor, OUT PUSB_ENDPOINT_DESCRIPTOR *OutEndpointDescriptor)
Definition: descriptor.c:140
IN PVOID IN PVOID IN USHORT IN USHORT IN PINTERFACE Interface
Definition: pci.h:359
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Status
Definition: gdiplustypes.h:24
struct _URB_SELECT_CONFIGURATION UrbSelectConfiguration
Definition: usb.h:533
PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor
Definition: usbdlib.h:8
Definition: usb.h:529
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:107
#define DPRINT1
Definition: precomp.h:8
#define UsbBuildSelectInterfaceRequest(urb, length, configurationHandle, interfaceNumber, alternateSetting)
Definition: usbdlib.h:59
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
PURB NTAPI USBD_CreateConfigurationRequestEx(PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor, PUSBD_INTERFACE_LIST_ENTRY InterfaceList)
Definition: usbd.c:329
struct _URB_SELECT_INTERFACE UrbSelectInterface
Definition: usb.h:532

◆ USBSTOR_SendCSWRequest()

NTSTATUS USBSTOR_SendCSWRequest ( PIRP_CONTEXT  Context,
PIRP  Irp 
)

Definition at line 258 of file scsi.c.

261 {
262  return USBSTOR_IssueBulkOrInterruptRequest(Context->FDODeviceExtension,
263  Irp,
264  Context->FDODeviceExtension->InterfaceInformation->Pipes[Context->FDODeviceExtension->BulkInPipeIndex].PipeHandle,
266  sizeof(CSW),
267  &Context->csw,
268  NULL,
270  Context);
271 }
_In_ PIRP Irp
Definition: csq.h:116
#define USBD_TRANSFER_DIRECTION_IN
Definition: usb.h:160
Definition: usbstor.h:125
IO_COMPLETION_ROUTINE USBSTOR_CSWCompletionRoutine
Definition: scsi.c:126
smooth NULL
Definition: ftsmooth.c:416
static NTSTATUS USBSTOR_IssueBulkOrInterruptRequest(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN USBD_PIPE_HANDLE PipeHandle, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer, IN PMDL TransferBufferMDL, IN PIO_COMPLETION_ROUTINE CompletionRoutine, IN PIRP_CONTEXT Context)
Definition: scsi.c:60

Referenced by USBSTOR_CBWCompletionRoutine(), USBSTOR_DataCompletionRoutine(), and USBSTOR_ResetPipeWorkItemRoutine().

◆ USBSTOR_StartIo()

VOID NTAPI USBSTOR_StartIo ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp 
)

Definition at line 300 of file queue.c.

303 {
304  PIO_STACK_LOCATION IoStack;
306  PFDO_DEVICE_EXTENSION FDODeviceExtension;
307  PPDO_DEVICE_EXTENSION PDODeviceExtension;
308  KIRQL OldLevel;
309  BOOLEAN ResetInProgress;
310 
311  DPRINT("USBSTOR_StartIo\n");
312 
313  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
314  ASSERT(FDODeviceExtension->Common.IsFDO);
315 
316  IoAcquireCancelSpinLock(&OldLevel);
317 
319 
320  // check if the irp has been cancelled
321  if (Irp->Cancel)
322  {
323  IoReleaseCancelSpinLock(OldLevel);
324 
325  Irp->IoStatus.Status = STATUS_CANCELLED;
326  Irp->IoStatus.Information = 0;
327 
331  return;
332  }
333 
334  IoReleaseCancelSpinLock(OldLevel);
335 
336  KeAcquireSpinLock(&FDODeviceExtension->CommonLock, &OldLevel);
337  ResetInProgress = BooleanFlagOn(FDODeviceExtension->Flags, USBSTOR_FDO_FLAGS_DEVICE_RESETTING);
338  KeReleaseSpinLock(&FDODeviceExtension->CommonLock, OldLevel);
339 
341 
342  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
343  Request = IoStack->Parameters.Scsi.Srb;
344  ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
345 
346  if (ResetInProgress)
347  {
348  // hard reset is in progress
349  Request->SrbStatus = SRB_STATUS_NO_DEVICE;
350  Request->DataTransferLength = 0;
351  Irp->IoStatus.Information = 0;
352  Irp->IoStatus.Status = STATUS_DEVICE_DOES_NOT_EXIST;
356  return;
357  }
358 
360 
361  // FIXME: handle error
362 }
#define STATUS_DEVICE_DOES_NOT_EXIST
Definition: ntstatus.h:414
_In_ PIRP Irp
Definition: csq.h:116
#define BooleanFlagOn(F, SF)
Definition: ext2fs.h:183
VOID NTAPI IoAcquireCancelSpinLock(OUT PKIRQL Irql)
Definition: util.c:56
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define SRB_STATUS_NO_DEVICE
Definition: srb.h:340
NTSTATUS USBSTOR_HandleExecuteSCSI(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: scsi.c:560
IoSetCancelRoutine(Irp, CancelRoutine)
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
UCHAR KIRQL
Definition: env_spec_w32.h:591
VOID USBSTOR_QueueNextRequest(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:220
PVOID DeviceExtension
Definition: env_spec_w32.h:418
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define IoCompleteRequest
Definition: irp.c:1240
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:55
void DPRINT(...)
Definition: polytest.cpp:61
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define STATUS_CANCELLED
Definition: udferr_usr.h:170
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define USBSTOR_FDO_FLAGS_DEVICE_RESETTING
Definition: usbstor.h:45
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
VOID NTAPI IoReleaseCancelSpinLock(IN KIRQL Irql)
Definition: util.c:150
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define IO_NO_INCREMENT
Definition: iotypes.h:566
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772

◆ USBSTOR_SyncForwardIrp()

NTSTATUS NTAPI USBSTOR_SyncForwardIrp ( IN PDEVICE_OBJECT  DeviceObject,
IN OUT PIRP  Irp 
)

◆ USBSTOR_SyncForwardIrpCompletionRoutine()

NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Context 
)

Definition at line 22 of file misc.c.

26 {
27  if (Irp->PendingReturned)
28  {
30  }
32 }
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:63
_In_ PIRP Irp
Definition: csq.h:116
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define IO_NO_INCREMENT
Definition: iotypes.h:566

◆ USBSTOR_SyncUrbRequest()

NTSTATUS USBSTOR_SyncUrbRequest ( IN PDEVICE_OBJECT  DeviceObject,
OUT PURB  UrbRequest 
)

Definition at line 107 of file misc.c.

110 {
111  PIRP Irp;
112  PIO_STACK_LOCATION IoStack;
113  KEVENT Event;
115 
117  if (!Irp)
118  {
120  }
121 
123 
124  IoStack = IoGetNextIrpStackLocation(Irp);
125 
126  // initialize stack location
128  IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
129  IoStack->Parameters.Others.Argument1 = (PVOID)UrbRequest;
130  IoStack->Parameters.DeviceIoControl.InputBufferLength = UrbRequest->UrbHeader.Length;
131  Irp->IoStatus.Status = STATUS_SUCCESS;
132 
134 
136 
137  if (Status == STATUS_PENDING)
138  {
140  Status = Irp->IoStatus.Status;
141  }
142 
143  IoFreeIrp(Irp);
144  return Status;
145 }
#define TRUE
Definition: types.h:120
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define IOCTL_INTERNAL_USB_SUBMIT_URB
Definition: usbioctl.h:32
_In_ PIRP Irp
Definition: csq.h:116
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:515
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:435
smooth NULL
Definition: ftsmooth.c:416
NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
Definition: misc.c:22
#define STATUS_PENDING
Definition: ntstatus.h:82
#define IRP_MJ_INTERNAL_DEVICE_CONTROL
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2647
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
VOID NTAPI IoFreeIrp(IN PIRP Irp)
Definition: irp.c:1666
static PVOID
Definition: misc.c:50
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
return STATUS_SUCCESS
Definition: btrfs.c:2966

◆ USBSTOR_TimerRoutine()

VOID NTAPI USBSTOR_TimerRoutine ( PDEVICE_OBJECT  DeviceObject,
PVOID  Context 
)

Definition at line 206 of file error.c.

209 {
210  PFDO_DEVICE_EXTENSION FDODeviceExtension;
212  PERRORHANDLER_WORKITEM_DATA WorkItemData;
213 
214  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)Context;
215  DPRINT1("[USBSTOR] TimerRoutine entered\n");
216  // DPRINT1("[USBSTOR] ActiveSrb %p ResetInProgress %x LastTimerActiveSrb %p\n", FDODeviceExtension->ActiveSrb, FDODeviceExtension->ResetInProgress, FDODeviceExtension->LastTimerActiveSrb);
217 
218  KeAcquireSpinLockAtDpcLevel(&FDODeviceExtension->IrpListLock);
219 
220  // is there an active srb and no global reset is in progress
221  if (FDODeviceExtension->ActiveSrb && /* FDODeviceExtension->ResetInProgress == FALSE && */ FDODeviceExtension->TimerWorkQueueEnabled)
222  {
223  if (FDODeviceExtension->LastTimerActiveSrb != NULL && FDODeviceExtension->LastTimerActiveSrb == FDODeviceExtension->ActiveSrb)
224  {
225  // check if empty
226  DPRINT1("[USBSTOR] ActiveSrb %p hang detected\n", FDODeviceExtension->ActiveSrb);
227  ResetDevice = TRUE;
228  }
229  else
230  {
231  // update pointer
232  FDODeviceExtension->LastTimerActiveSrb = FDODeviceExtension->ActiveSrb;
233  }
234  }
235  else
236  {
237  // reset srb
238  FDODeviceExtension->LastTimerActiveSrb = NULL;
239  }
240 
241  KeReleaseSpinLockFromDpcLevel(&FDODeviceExtension->IrpListLock);
242 
243 
244  if (ResetDevice && FDODeviceExtension->TimerWorkQueueEnabled && FDODeviceExtension->SrbErrorHandlingActive == FALSE)
245  {
246  WorkItemData = ExAllocatePoolWithTag(NonPagedPool,
248  USB_STOR_TAG);
249  if (WorkItemData)
250  {
251  // Initialize and queue the work item to handle the error
252  ExInitializeWorkItem(&WorkItemData->WorkQueueItem,
254  WorkItemData);
255 
256  WorkItemData->DeviceObject = FDODeviceExtension->FunctionalDeviceObject;
257 
258  DPRINT1("[USBSTOR] Queing Timer WorkItem\n");
260  }
261  }
262 }
#define TRUE
Definition: types.h:120
VOID NTAPI ExQueueWorkItem(IN PWORK_QUEUE_ITEM WorkItem, IN WORK_QUEUE_TYPE QueueType)
Definition: work.c:717
DWORD ResetDevice(DWORD private_handle)
VOID NTAPI KeAcquireSpinLockAtDpcLevel(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:192
WORK_QUEUE_ITEM WorkQueueItem
Definition: usbstor.h:315
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
PDEVICE_OBJECT DeviceObject
Definition: usbstor.h:313
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define ExInitializeWorkItem(Item, Routine, Context)
Definition: exfuncs.h:265
VOID NTAPI KeReleaseSpinLockFromDpcLevel(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:215
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define USB_STOR_TAG
Definition: usbstor.h:10
#define DPRINT1
Definition: precomp.h:8
VOID NTAPI USBSTOR_TimerWorkerRoutine(IN PVOID Context)
Definition: error.c:168