ReactOS  0.4.14-dev-297-g23e575c
misc.c File Reference
#include "usbstor.h"
#include <debug.h>
Include dependency graph for misc.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
 
NTSTATUS NTAPI USBSTOR_SyncForwardIrp (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 
NTSTATUS NTAPI USBSTOR_GetBusInterface (IN PDEVICE_OBJECT DeviceObject, OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface)
 
NTSTATUS USBSTOR_SyncUrbRequest (IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
 
PVOID AllocateItem (IN POOL_TYPE PoolType, IN ULONG ItemSize)
 
VOID FreeItem (IN PVOID Item)
 
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)
 
NTSTATUS USBSTOR_GetMaxLUN (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
 
NTSTATUS USBSTOR_ResetDevice (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
 

Variables

IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file misc.c.

Function Documentation

◆ AllocateItem()

PVOID AllocateItem ( IN POOL_TYPE  PoolType,
IN ULONG  ItemSize 
)

Definition at line 148 of file misc.c.

151 {
153 
154  if (Item)
155  {
156  RtlZeroMemory(Item, ItemSize);
157  }
158 
159  return Item;
160 }
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define USB_STOR_TAG
Definition: usbstor.h:10
static UCHAR ItemSize[4]
Definition: parser.c:16
#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

◆ FreeItem()

VOID FreeItem ( IN PVOID  Item)

Definition at line 163 of file misc.c.

165 {
167 }
#define USB_STOR_TAG
Definition: usbstor.h:10
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099

◆ USBSTOR_ClassRequest()

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 at line 170 of file misc.c.

179 {
180  PURB Urb;
182 
184  if (!Urb)
185  {
187  }
188 
191  Urb->UrbControlVendorClassRequest.TransferFlags = TransferFlags;
192  Urb->UrbControlVendorClassRequest.TransferBufferLength = TransferBufferLength;
193  Urb->UrbControlVendorClassRequest.TransferBuffer = TransferBuffer;
194  Urb->UrbControlVendorClassRequest.Request = RequestType;
195  Urb->UrbControlVendorClassRequest.Index = Index;
196 
198 
199  FreeItem(Urb);
200  return Status;
201 }
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
struct _URB_CONTROL_VENDOR_OR_CLASS_REQUEST UrbControlVendorClassRequest
Definition: usb.h:548
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
LONG NTSTATUS
Definition: precomp.h:26
static const UCHAR Index[8]
Definition: usbohci.c:18
struct _URB * PURB
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
Definition: usb.h:529
#define URB_FUNCTION_CLASS_INTERFACE
Definition: usb.h:113
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:107
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30

Referenced by USBSTOR_GetMaxLUN(), and USBSTOR_ResetDevice().

◆ 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

Referenced by USBSTOR_FdoHandleStartDevice().

◆ 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

Referenced by USBSTOR_FdoHandleStartDevice().

◆ 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

Referenced by USBSTOR_GetMaxLUN(), USBSTOR_HandleTransferError(), USBSTOR_ResetDeviceWorkItemRoutine(), and USBSTOR_TimerWorkerRoutine().

◆ USBSTOR_SyncForwardIrp()

NTSTATUS NTAPI USBSTOR_SyncForwardIrp ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp 
)

Definition at line 34 of file misc.c.

35 {
36  KEVENT Event;
38 
42 
44 
45  if (Status == STATUS_PENDING)
46  {
47  // wait for the request to finish
49  Status = Irp->IoStatus.Status;
50  }
51 
52  return Status;
53 }
#define TRUE
Definition: types.h:120
_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
FORCEINLINE VOID IoCopyCurrentIrpStackLocationToNext(_Inout_ PIRP Irp)
Definition: iofuncs.h:2820
_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
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

Referenced by USBSTOR_FdoHandleDeviceRelations(), USBSTOR_FdoHandleStartDevice(), and USBSTOR_PdoHandlePnp().

◆ USBSTOR_SyncForwardIrpCompletionRoutine()

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

Definition at line 20 of file misc.c.

24 {
25  if (Irp->PendingReturned)
26  {
28  }
30 }
#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

Referenced by USBSTOR_ClassRequest(), USBSTOR_GetDescriptor(), USBSTOR_GetEndpointStatus(), USBSTOR_ResetPipeWithHandle(), and USBSTOR_SelectConfigurationAndInterface().

Variable Documentation

◆ SyncForwardIrpCompletionRoutine

IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine

Definition at line 16 of file misc.c.