ReactOS  0.4.14-dev-376-gaedba84
scsi.c File Reference
#include "usbstor.h"
#include <debug.h>
Include dependency graph for scsi.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

static NTSTATUS USBSTOR_SrbStatusToNtStatus (IN PSCSI_REQUEST_BLOCK Srb)
 
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)
 
static BOOLEAN USBSTOR_IsCSWValid (PIRP_CONTEXT Context)
 
static NTSTATUS USBSTOR_IssueRequestSense (IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN PIRP_CONTEXT Context)
 
NTSTATUS NTAPI USBSTOR_CSWCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Ctx)
 
NTSTATUS USBSTOR_SendCSWRequest (PIRP_CONTEXT Context, PIRP Irp)
 
NTSTATUS NTAPI USBSTOR_DataCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Ctx)
 
NTSTATUS NTAPI USBSTOR_CBWCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Ctx)
 
VOID DumpCBW (PUCHAR Block)
 
static NTSTATUS USBSTOR_SendCBWRequest (IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN PIRP_CONTEXT Context)
 
NTSTATUS USBSTOR_HandleExecuteSCSI (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 

Variables

IO_COMPLETION_ROUTINE USBSTOR_CSWCompletionRoutine
 
IO_COMPLETION_ROUTINE USBSTOR_DataCompletionRoutine
 
IO_COMPLETION_ROUTINE USBSTOR_CBWCompletionRoutine
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file scsi.c.

Function Documentation

◆ DumpCBW()

VOID DumpCBW ( PUCHAR  Block)

Definition at line 462 of file scsi.c.

464 {
465  DPRINT("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
466  Block[0] & 0xFF, Block[1] & 0xFF, Block[2] & 0xFF, Block[3] & 0xFF, Block[4] & 0xFF, Block[5] & 0xFF, Block[6] & 0xFF, Block[7] & 0xFF, Block[8] & 0xFF, Block[9] & 0xFF,
467  Block[10] & 0xFF, Block[11] & 0xFF, Block[12] & 0xFF, Block[13] & 0xFF, Block[14] & 0xFF, Block[15] & 0xFF, Block[16] & 0xFF, Block[17] & 0xFF, Block[18] & 0xFF, Block[19] & 0xFF,
468  Block[20] & 0xFF, Block[21] & 0xFF, Block[22] & 0xFF, Block[23] & 0xFF, Block[24] & 0xFF, Block[25] & 0xFF, Block[26] & 0xFF, Block[27] & 0xFF, Block[28] & 0xFF, Block[29] & 0xFF,
469  Block[30] & 0xFF);
470 }
void DPRINT(...)
Definition: polytest.cpp:61

Referenced by USBSTOR_SendCBWRequest(), and USBSTOR_SendRequest().

◆ USBSTOR_CBWCompletionRoutine()

NTSTATUS NTAPI USBSTOR_CBWCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Ctx 
)

Definition at line 345 of file scsi.c.

349 {
351  PIO_STACK_LOCATION IoStack;
353  PPDO_DEVICE_EXTENSION PDODeviceExtension;
356  PMDL Mdl = NULL;
358 
359  DPRINT("USBSTOR_CBWCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
360 
361  Context = (PIRP_CONTEXT)Ctx;
363  Request = IoStack->Parameters.Scsi.Srb;
364  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
365 
366  if (!NT_SUCCESS(Irp->IoStatus.Status))
367  {
368  goto ResetRecovery;
369  }
370 
371  // a request without the buffer AND not a sense request
372  // for a sense request we provide just a TransferBuffer, an Mdl will be allocated by usbport (see below)
373  if (!Irp->MdlAddress && Request == Context->FDODeviceExtension->ActiveSrb)
374  {
375  Request->SrbStatus = SRB_STATUS_SUCCESS;
378  }
379 
380  // a request with the data buffer
381 
383  {
384  PipeHandle = Context->FDODeviceExtension->InterfaceInformation->Pipes[Context->FDODeviceExtension->BulkInPipeIndex].PipeHandle;
386  }
388  {
389  PipeHandle = Context->FDODeviceExtension->InterfaceInformation->Pipes[Context->FDODeviceExtension->BulkOutPipeIndex].PipeHandle;
391  }
392  else
393  {
394  // we check the validity of a request in disk.c so we should never be here
395  DPRINT1("Warning: shouldn't be here\n");
396  goto ResetRecovery;
397  }
398 
399  // if it is not a Sense Request
400  if (Request == Context->FDODeviceExtension->ActiveSrb)
401  {
402  if (MmGetMdlVirtualAddress(Irp->MdlAddress) == Request->DataBuffer)
403  {
404  Mdl = Irp->MdlAddress;
405  }
406  else
407  {
408  Mdl = IoAllocateMdl(Request->DataBuffer,
409  Request->DataTransferLength,
410  FALSE,
411  FALSE,
412  NULL);
413 
414  if (Mdl)
415  {
416  IoBuildPartialMdl(Irp->MdlAddress,
417  Mdl,
418  Request->DataBuffer,
419  Request->DataTransferLength);
420  }
421  }
422 
423  if (!Mdl)
424  {
425  DPRINT1("USBSTOR_CBWCompletionRoutine: Mdl - %p\n", Mdl);
426  goto ResetRecovery;
427  }
428  }
429  else
430  {
431  ASSERT(Request->DataBuffer);
432  TransferBuffer = Request->DataBuffer;
433  }
434 
435  USBSTOR_IssueBulkOrInterruptRequest(Context->FDODeviceExtension,
436  Irp,
437  PipeHandle,
439  Request->DataTransferLength,
441  Mdl,
443  Context);
444 
446 
447 ResetRecovery:
448  Request = Context->FDODeviceExtension->ActiveSrb;
449  IoStack->Parameters.Scsi.Srb = Request;
450  Irp->IoStatus.Information = 0;
451  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
452  Request->SrbStatus = SRB_STATUS_BUS_RESET;
453 
454  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
455  USBSTOR_QueueResetDevice(Context->FDODeviceExtension);
456 
459 }
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
#define SRB_FLAGS_UNSPECIFIED_DIRECTION
Definition: srb.h:395
IRP_CONTEXT * PIRP_CONTEXT
Definition: cdstruc.h:1217
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
#define MmGetMdlVirtualAddress(_Mdl)
_In_ PIRP Irp
Definition: csq.h:116
#define USBD_TRANSFER_DIRECTION_IN
Definition: usb.h:160
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define STATUS_CONTINUE_COMPLETION
#define SRB_FLAGS_DATA_IN
Definition: srb.h:392
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
VOID NTAPI IoBuildPartialMdl(IN PMDL SourceMdl, IN PMDL TargetMdl, IN PVOID VirtualAddress, IN ULONG Length)
Definition: iomdl.c:96
IO_COMPLETION_ROUTINE USBSTOR_DataCompletionRoutine
Definition: scsi.c:273
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
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
#define USB_STOR_TAG
Definition: usbstor.h:10
PMDL NTAPI IoAllocateMdl(IN PVOID VirtualAddress, IN ULONG Length, IN BOOLEAN SecondaryBuffer, IN BOOLEAN ChargeQuota, IN PIRP Irp)
Definition: iomdl.c:22
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define USBD_SHORT_TRANSFER_OK
Definition: usb.h:154
static HANDLE PipeHandle
Definition: dhcpcsvc.c:21
#define SRB_FLAGS_DATA_OUT
Definition: srb.h:393
#define DPRINT1
Definition: precomp.h:8
struct tagContext Context
Definition: acpixf.h:1030
ULONG TransferFlags
Definition: usb.h:469
unsigned int ULONG
Definition: retypes.h:1
#define SRB_STATUS_SUCCESS
Definition: srb.h:333
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
VOID NTAPI USBSTOR_QueueResetDevice(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:149
PVOID TransferBuffer
Definition: usb.h:471
NTSTATUS USBSTOR_SendCSWRequest(PIRP_CONTEXT Context, PIRP Irp)
Definition: scsi.c:258

◆ USBSTOR_CSWCompletionRoutine()

NTSTATUS NTAPI USBSTOR_CSWCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Ctx 
)

Definition at line 130 of file scsi.c.

134 {
136  PIO_STACK_LOCATION IoStack;
137  PPDO_DEVICE_EXTENSION PDODeviceExtension;
138  PFDO_DEVICE_EXTENSION FDODeviceExtension;
141 
142  Context = (PIRP_CONTEXT)Ctx;
143 
144  DPRINT("USBSTOR_CSWCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
145 
147  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
148  FDODeviceExtension = Context->FDODeviceExtension;
149  Request = IoStack->Parameters.Scsi.Srb;
150  ASSERT(Request);
151 
152  // first check for Irp errors
153  if (!NT_SUCCESS(Irp->IoStatus.Status))
154  {
155  if (USBD_STATUS(Context->Urb.UrbHeader.Status) == USBD_STATUS(USBD_STATUS_STALL_PID))
156  {
157  if (Context->StallRetryCount < 2)
158  {
159  ++Context->StallRetryCount;
160 
161  // clear stall and resend cbw
162  USBSTOR_QueueResetPipe(FDODeviceExtension, Context);
163 
165  }
166  }
167  else
168  {
169  DPRINT1("USBSTOR_CSWCompletionRoutine: Urb.Hdr.Status - %x\n", Context->Urb.UrbHeader.Status);
170  }
171 
172  goto ResetRecovery;
173  }
174 
175  // now check the CSW packet validity
177  {
178  goto ResetRecovery;
179  }
180 
181  // finally check for CSW errors
182  if (Context->csw.Status == CSW_STATUS_COMMAND_PASSED)
183  {
184  // should happen only when a sense request was sent
185  if (Request != FDODeviceExtension->ActiveSrb)
186  {
187  ASSERT(IoStack->Parameters.Scsi.Srb == &Context->SenseSrb);
188  FDODeviceExtension->ActiveSrb->SenseInfoBufferLength = Request->DataTransferLength;
189  Request = FDODeviceExtension->ActiveSrb;
190  IoStack->Parameters.Scsi.Srb = Request;
191  Request->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
192  }
193 
194  // read capacity needs special work
195  if (Request->Cdb[0] == SCSIOP_READ_CAPACITY)
196  {
197  // get output buffer
198  Response = (PUFI_CAPACITY_RESPONSE)Request->DataBuffer;
199 
200  // store in pdo
201  PDODeviceExtension->BlockLength = NTOHL(Response->BlockLength);
202  PDODeviceExtension->LastLogicBlockAddress = NTOHL(Response->LastLogicalBlockAddress);
203  }
204 
205  Irp->IoStatus.Status = USBSTOR_SrbStatusToNtStatus(Request);
206  }
207  else if (Context->csw.Status == CSW_STATUS_COMMAND_FAILED)
208  {
209  // the command is correct but with failed status - issue request sense
210  DPRINT("USBSTOR_CSWCompletionRoutine: CSW_STATUS_COMMAND_FAILED\n");
211 
212  ASSERT(FDODeviceExtension->ActiveSrb == Request);
213 
214  // setting a generic error status, additional information
215  // should be read by higher-level driver from SenseInfoBuffer
216  Request->SrbStatus = SRB_STATUS_ERROR;
217  Request->ScsiStatus = 2;
218  Request->DataTransferLength = 0;
219 
220  DPRINT("Flags: %x SBL: %x, buf: %p\n", Request->SrbFlags, Request->SenseInfoBufferLength, Request->SenseInfoBuffer);
221 
222  if (!(Request->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE) &&
223  Request->SenseInfoBufferLength &&
224  Request->SenseInfoBuffer)
225  {
226  USBSTOR_IssueRequestSense(FDODeviceExtension, Irp, Context);
228  }
229 
230  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
231  }
232 
233  Irp->IoStatus.Information = Request->DataTransferLength;
234 
235  // terminate current request
236  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
237  USBSTOR_QueueNextRequest(PDODeviceExtension->LowerDeviceObject);
238 
241 
242 ResetRecovery:
243 
244  Request = FDODeviceExtension->ActiveSrb;
245  IoStack->Parameters.Scsi.Srb = Request;
246  Irp->IoStatus.Information = 0;
247  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
248  Request->SrbStatus = SRB_STATUS_BUS_RESET;
249 
250  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
251  USBSTOR_QueueResetDevice(FDODeviceExtension);
252 
255 }
#define SRB_FLAGS_DISABLE_AUTOSENSE
Definition: srb.h:391
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
VOID NTAPI USBSTOR_QueueResetPipe(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP_CONTEXT Context)
Definition: error.c:135
static NTSTATUS USBSTOR_SrbStatusToNtStatus(IN PSCSI_REQUEST_BLOCK Srb)
Definition: scsi.c:20
IRP_CONTEXT * PIRP_CONTEXT
Definition: cdstruc.h:1217
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
#define SRB_STATUS_AUTOSENSE_VALID
Definition: srb.h:379
_In_ PIRP Irp
Definition: csq.h:116
#define CSW_STATUS_COMMAND_PASSED
Definition: usbstor.h:121
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define STATUS_CONTINUE_COMPLETION
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
#define SRB_STATUS_ERROR
Definition: srb.h:336
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
VOID USBSTOR_QueueNextRequest(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:220
PVOID DeviceExtension
Definition: env_spec_w32.h:418
void DPRINT(...)
Definition: polytest.cpp:61
#define CSW_STATUS_COMMAND_FAILED
Definition: usbstor.h:122
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
static BOOLEAN USBSTOR_IsCSWValid(PIRP_CONTEXT Context)
Definition: scsi.c:101
#define CSW_STATUS_PHASE_ERROR
Definition: usbstor.h:123
static NTSTATUS USBSTOR_IssueRequestSense(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN PIRP_CONTEXT Context)
Definition: scsi.c:521
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
struct UFI_CAPACITY_RESPONSE * PUFI_CAPACITY_RESPONSE
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
Definition: ncftp.h:89
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
LONG USBD_STATUS
Definition: usb.h:165
#define USB_STOR_TAG
Definition: usbstor.h:10
#define NTOHL(n)
Definition: usbstor.h:23
#define USBD_STATUS_STALL_PID
Definition: usb.h:175
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define SCSIOP_READ_CAPACITY
Definition: cdrw_hw.h:904
#define DPRINT1
Definition: precomp.h:8
struct tagContext Context
Definition: acpixf.h:1030
struct Response Response
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
VOID NTAPI USBSTOR_QueueResetDevice(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:149

◆ USBSTOR_DataCompletionRoutine()

NTSTATUS NTAPI USBSTOR_DataCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Ctx 
)

Definition at line 277 of file scsi.c.

281 {
283  PIO_STACK_LOCATION IoStack;
285  PPDO_DEVICE_EXTENSION PDODeviceExtension;
286 
287  DPRINT("USBSTOR_DataCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
288 
289  Context = (PIRP_CONTEXT)Ctx;
291  Request = IoStack->Parameters.Scsi.Srb;
292  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
293 
294  // for Sense Request a partial MDL was already freed (if existed)
295  if (Request == Context->FDODeviceExtension->ActiveSrb &&
296  Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL != Irp->MdlAddress)
297  {
298  IoFreeMdl(Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL);
299  }
300 
301  if (NT_SUCCESS(Irp->IoStatus.Status))
302  {
303  if (Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength < Request->DataTransferLength)
304  {
305  Request->SrbStatus = SRB_STATUS_DATA_OVERRUN;
306  }
307  else
308  {
309  Request->SrbStatus = SRB_STATUS_SUCCESS;
310  }
311 
312  Request->DataTransferLength = Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength;
314  }
315  else if (USBD_STATUS(Context->Urb.UrbHeader.Status) == USBD_STATUS(USBD_STATUS_STALL_PID))
316  {
317  ++Context->StallRetryCount;
318 
319  Request->SrbStatus = SRB_STATUS_DATA_OVERRUN;
320  Request->DataTransferLength = Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength;
321 
322  // clear stall and resend cbw
323  USBSTOR_QueueResetPipe(Context->FDODeviceExtension, Context);
324  }
325  else
326  {
327  Irp->IoStatus.Information = 0;
328  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
329  Request->SrbStatus = SRB_STATUS_BUS_RESET;
330 
331  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
332  USBSTOR_QueueResetDevice(Context->FDODeviceExtension);
333 
336  }
337 
339 }
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
VOID NTAPI USBSTOR_QueueResetPipe(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP_CONTEXT Context)
Definition: error.c:135
IRP_CONTEXT * PIRP_CONTEXT
Definition: cdstruc.h:1217
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
_In_ PIRP Irp
Definition: csq.h:116
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define STATUS_CONTINUE_COMPLETION
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
PVOID DeviceExtension
Definition: env_spec_w32.h:418
void DPRINT(...)
Definition: polytest.cpp:61
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
#define SRB_STATUS_DATA_OVERRUN
Definition: srb.h:349
VOID NTAPI IoFreeMdl(PMDL Mdl)
Definition: iomdl.c:146
LONG USBD_STATUS
Definition: usb.h:165
#define USB_STOR_TAG
Definition: usbstor.h:10
#define USBD_STATUS_STALL_PID
Definition: usb.h:175
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
struct tagContext Context
Definition: acpixf.h:1030
#define SRB_STATUS_SUCCESS
Definition: srb.h:333
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
VOID NTAPI USBSTOR_QueueResetDevice(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:149
NTSTATUS USBSTOR_SendCSWRequest(PIRP_CONTEXT Context, PIRP Irp)
Definition: scsi.c:258

◆ 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:1030
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_IsCSWValid()

static BOOLEAN USBSTOR_IsCSWValid ( PIRP_CONTEXT  Context)
static

Definition at line 101 of file scsi.c.

103 {
104  if (Context->csw.Signature != CSW_SIGNATURE)
105  {
106  DPRINT1("[USBSTOR] Expected Signature %x but got %x\n", CSW_SIGNATURE, Context->csw.Signature);
107  return FALSE;
108  }
109 
110  if (Context->csw.Tag != PtrToUlong(&Context->csw))
111  {
112  DPRINT1("[USBSTOR] Expected Tag %Ix but got %x\n", PtrToUlong(&Context->csw), Context->csw.Tag);
113  return FALSE;
114  }
115 
116  return TRUE;
117 }
#define TRUE
Definition: types.h:120
#define PtrToUlong(u)
Definition: config.h:107
#define CSW_SIGNATURE
Definition: usbstor.h:117
#define DPRINT1
Definition: precomp.h:8

Referenced by USBSTOR_CSWCompletionRoutine().

◆ USBSTOR_IssueBulkOrInterruptRequest()

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 
)
static

Definition at line 60 of file scsi.c.

70 {
71  PIO_STACK_LOCATION NextStack;
72 
74 
75  Context->Urb.UrbHeader.Length = sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER);
76  Context->Urb.UrbHeader.Function = URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER;
77 
78  Context->Urb.UrbBulkOrInterruptTransfer.PipeHandle = PipeHandle;
79  Context->Urb.UrbBulkOrInterruptTransfer.TransferFlags = TransferFlags;
80  Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength = TransferBufferLength;
81  Context->Urb.UrbBulkOrInterruptTransfer.TransferBuffer = TransferBuffer;
82  Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL = TransferBufferMDL;
83 
84  NextStack = IoGetNextIrpStackLocation(Irp);
86  NextStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
87  NextStack->Parameters.Others.Argument1 = &Context->Urb;
88 
91  Context,
92  TRUE,
93  TRUE,
94  TRUE);
95 
96  return IoCallDriver(FDODeviceExtension->LowerDeviceObject, Irp);
97 }
#define TRUE
Definition: types.h:120
#define IOCTL_INTERNAL_USB_SUBMIT_URB
Definition: usbioctl.h:32
_In_ PIRP Irp
Definition: csq.h:116
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:515
#define IRP_MJ_INTERNAL_DEVICE_CONTROL
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2647
static HANDLE PipeHandle
Definition: dhcpcsvc.c:21
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
_In_ PIRP _In_opt_ PVOID _In_opt_ POPLOCK_WAIT_COMPLETE_ROUTINE CompletionRoutine
Definition: fsrtlfuncs.h:673
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
#define URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER
Definition: usb.h:95

Referenced by USBSTOR_CBWCompletionRoutine(), USBSTOR_SendCBWRequest(), and USBSTOR_SendCSWRequest().

◆ USBSTOR_IssueRequestSense()

static NTSTATUS USBSTOR_IssueRequestSense ( IN PFDO_DEVICE_EXTENSION  FDODeviceExtension,
IN PIRP  Irp,
IN PIRP_CONTEXT  Context 
)
static

Definition at line 521 of file scsi.c.

525 {
526  PIO_STACK_LOCATION IoStack;
527  PSCSI_REQUEST_BLOCK CurrentSrb;
528  PSCSI_REQUEST_BLOCK SenseSrb;
529 
530  DPRINT("USBSTOR_IssueRequestSense: \n");
531 
532  CurrentSrb = FDODeviceExtension->ActiveSrb;
533  SenseSrb = &Context->SenseSrb;
535  IoStack->Parameters.Scsi.Srb = SenseSrb;
536 
537  RtlZeroMemory(SenseSrb, sizeof(*SenseSrb));
538 
540  SenseSrb->Length = sizeof(*SenseSrb);
541  SenseSrb->CdbLength = CDB6GENERIC_LENGTH;
542  SenseSrb->SrbFlags = SRB_FLAGS_DATA_IN |
545 
546  ASSERT(CurrentSrb->SenseInfoBufferLength);
547  ASSERT(CurrentSrb->SenseInfoBuffer);
548  DPRINT("SenseInfoBuffer %x, SenseInfoBufferLength %x\n", CurrentSrb->SenseInfoBuffer, CurrentSrb->SenseInfoBufferLength);
549 
550  SenseSrb->DataTransferLength = CurrentSrb->SenseInfoBufferLength;
551  SenseSrb->DataBuffer = CurrentSrb->SenseInfoBuffer;
552 
553  SrbGetCdb(SenseSrb)->CDB6GENERIC.OperationCode = SCSIOP_REQUEST_SENSE;
554  SrbGetCdb(SenseSrb)->AsByte[4] = CurrentSrb->SenseInfoBufferLength;
555 
556  return USBSTOR_SendCBWRequest(FDODeviceExtension, Irp, Context);
557 }
#define SRB_FLAGS_DISABLE_AUTOSENSE
Definition: srb.h:391
ULONG SrbFlags
Definition: srb.h:252
_In_ PIRP Irp
Definition: csq.h:116
#define SRB_FLAGS_NO_QUEUE_FREEZE
Definition: srb.h:396
PVOID DataBuffer
Definition: srb.h:255
ULONG DataTransferLength
Definition: srb.h:253
UCHAR CdbLength
Definition: srb.h:250
#define SRB_FLAGS_DATA_IN
Definition: srb.h:392
void DPRINT(...)
Definition: polytest.cpp:61
#define CDB6GENERIC_LENGTH
Definition: cdrw_hw.h:830
UCHAR Function
Definition: srb.h:242
USHORT Length
Definition: srb.h:241
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
UCHAR SenseInfoBufferLength
Definition: srb.h:251
PVOID SenseInfoBuffer
Definition: srb.h:256
#define SrbGetCdb(srb)
Definition: usbstor.h:33
#define SRB_FUNCTION_EXECUTE_SCSI
Definition: srb.h:307
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define SCSIOP_REQUEST_SENSE
Definition: cdrw_hw.h:870
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_CSWCompletionRoutine().

◆ USBSTOR_SendCBWRequest()

static NTSTATUS USBSTOR_SendCBWRequest ( IN PFDO_DEVICE_EXTENSION  FDODeviceExtension,
IN PIRP  Irp,
IN PIRP_CONTEXT  Context 
)
static

Definition at line 474 of file scsi.c.

478 {
479  PPDO_DEVICE_EXTENSION PDODeviceExtension;
480  PIO_STACK_LOCATION IoStack;
482 
483  RtlZeroMemory(&Context->cbw, sizeof(CBW));
484  RtlZeroMemory(&Context->Urb, sizeof(URB));
485 
487  PDODeviceExtension = IoStack->DeviceObject->DeviceExtension;
488  Request = IoStack->Parameters.Scsi.Srb;
489 
490  Context->cbw.Signature = CBW_SIGNATURE;
491  Context->cbw.Tag = PtrToUlong(&Context->cbw);
492  Context->cbw.DataTransferLength = Request->DataTransferLength;
493  Context->cbw.Flags = ((UCHAR)Request->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) << 1;
494  Context->cbw.LUN = PDODeviceExtension->LUN;
495  Context->cbw.CommandBlockLength = Request->CdbLength;
496 
497  RtlCopyMemory(&Context->cbw.CommandBlock, Request->Cdb, Request->CdbLength);
498 
499  DPRINT("CBW for IRP %p\n", Irp);
500  DumpCBW((PUCHAR)&Context->cbw);
501 
502  // initialize rest of context
503  Context->Irp = Irp;
504  Context->FDODeviceExtension = FDODeviceExtension;
505  Context->StallRetryCount = 0;
506 
508  FDODeviceExtension,
509  Irp,
510  FDODeviceExtension->InterfaceInformation->Pipes[FDODeviceExtension->BulkOutPipeIndex].PipeHandle,
512  sizeof(CBW),
513  &Context->cbw,
514  NULL,
516  Context);
517 }
#define SRB_FLAGS_UNSPECIFIED_DIRECTION
Definition: srb.h:395
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
_In_ PIRP Irp
Definition: csq.h:116
unsigned char * PUCHAR
Definition: retypes.h:3
Definition: usbstor.h:102
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define PtrToUlong(u)
Definition: config.h:107
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
unsigned char UCHAR
Definition: xmlstorage.h:181
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
VOID DumpCBW(PUCHAR Block)
Definition: scsi.c:462
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
Definition: usb.h:529
IO_COMPLETION_ROUTINE USBSTOR_CBWCompletionRoutine
Definition: scsi.c:341
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
#define CBW_SIGNATURE
Definition: usbstor.h:116

Referenced by USBSTOR_HandleExecuteSCSI(), and USBSTOR_IssueRequestSense().

◆ 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_SrbStatusToNtStatus()

static NTSTATUS USBSTOR_SrbStatusToNtStatus ( IN PSCSI_REQUEST_BLOCK  Srb)
static

Definition at line 20 of file scsi.c.

22 {
23  UCHAR SrbStatus;
24 
25  SrbStatus = SRB_STATUS(Srb->SrbStatus);
26 
27  switch (SrbStatus)
28  {
29  case SRB_STATUS_SUCCESS:
30  return STATUS_SUCCESS;
31 
34 
38 
41  case SRB_STATUS_NO_HBA:
44 
45  case SRB_STATUS_TIMEOUT:
46  return STATUS_IO_TIMEOUT;
47 
52 
53  default:
55  }
56 }
#define STATUS_DEVICE_DOES_NOT_EXIST
Definition: ntstatus.h:414
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
#define SRB_STATUS_COMMAND_TIMEOUT
Definition: srb.h:343
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
#define SRB_STATUS_NO_DEVICE
Definition: srb.h:340
#define SRB_STATUS_BAD_SRB_BLOCK_LENGTH
Definition: srb.h:352
#define SRB_STATUS_INVALID_TARGET_ID
Definition: srb.h:355
#define SRB_STATUS(Status)
Definition: srb.h:381
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
#define SRB_STATUS_NO_HBA
Definition: srb.h:348
#define STATUS_DEVICE_NOT_CONNECTED
Definition: udferr_usr.h:160
#define SRB_STATUS_BAD_FUNCTION
Definition: srb.h:356
#define SRB_STATUS_DATA_OVERRUN
Definition: srb.h:349
unsigned char UCHAR
Definition: xmlstorage.h:181
#define SRB_STATUS_SELECTION_TIMEOUT
Definition: srb.h:342
#define SRB_STATUS_INVALID_LUN
Definition: srb.h:354
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
#define SRB_STATUS_TIMEOUT
Definition: srb.h:341
#define STATUS_IO_TIMEOUT
Definition: udferr_usr.h:163
#define SRB_STATUS_SUCCESS
Definition: srb.h:333
IN PSCSI_REQUEST_BLOCK Srb
Definition: class2.h:49
return STATUS_SUCCESS
Definition: btrfs.c:2938

Referenced by USBSTOR_CSWCompletionRoutine().

Variable Documentation

◆ USBSTOR_CBWCompletionRoutine

IO_COMPLETION_ROUTINE USBSTOR_CBWCompletionRoutine

Definition at line 341 of file scsi.c.

Referenced by USBSTOR_SendCBW(), and USBSTOR_SendCBWRequest().

◆ USBSTOR_CSWCompletionRoutine

IO_COMPLETION_ROUTINE USBSTOR_CSWCompletionRoutine

Definition at line 126 of file scsi.c.

Referenced by USBSTOR_CBWCompletionRoutine(), USBSTOR_SendCSW(), and USBSTOR_SendCSWRequest().

◆ USBSTOR_DataCompletionRoutine

IO_COMPLETION_ROUTINE USBSTOR_DataCompletionRoutine

Definition at line 273 of file scsi.c.

Referenced by USBSTOR_CBWCompletionRoutine().