2011-05-03 19:40:03 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Universal Serial Bus Bulk Storage Driver
|
2019-03-24 20:01:43 +00:00
|
|
|
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
|
2011-05-03 19:40:03 +00:00
|
|
|
* PURPOSE: USB block storage device driver.
|
2019-03-24 20:01:43 +00:00
|
|
|
* COPYRIGHT: 2005-2006 James Tabor
|
|
|
|
* 2011-2012 Michael Martin (michael.martin@reactos.org)
|
|
|
|
* 2011-2013 Johannes Anderwald (johannes.anderwald@reactos.org)
|
2019-03-31 20:03:33 +00:00
|
|
|
* 2017 Vadim Galyant
|
|
|
|
* 2019 Victor Perevertkin (victor.perevertkin@reactos.org)
|
2011-05-03 19:40:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "usbstor.h"
|
|
|
|
|
2013-12-23 14:50:17 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2019-03-24 20:01:43 +00:00
|
|
|
|
2019-03-31 20:03:33 +00:00
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_SrbStatusToNtStatus(
|
|
|
|
IN PSCSI_REQUEST_BLOCK Srb)
|
|
|
|
{
|
|
|
|
UCHAR SrbStatus;
|
|
|
|
|
|
|
|
SrbStatus = SRB_STATUS(Srb->SrbStatus);
|
|
|
|
|
|
|
|
switch (SrbStatus)
|
|
|
|
{
|
|
|
|
case SRB_STATUS_SUCCESS:
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case SRB_STATUS_DATA_OVERRUN:
|
|
|
|
return STATUS_BUFFER_OVERFLOW;
|
|
|
|
|
|
|
|
case SRB_STATUS_BAD_FUNCTION:
|
|
|
|
case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
|
|
|
|
return STATUS_INVALID_DEVICE_REQUEST;
|
|
|
|
|
|
|
|
case SRB_STATUS_INVALID_LUN:
|
|
|
|
case SRB_STATUS_INVALID_TARGET_ID:
|
|
|
|
case SRB_STATUS_NO_HBA:
|
|
|
|
case SRB_STATUS_NO_DEVICE:
|
|
|
|
return STATUS_DEVICE_DOES_NOT_EXIST;
|
|
|
|
|
|
|
|
case SRB_STATUS_TIMEOUT:
|
|
|
|
return STATUS_IO_TIMEOUT;
|
|
|
|
|
|
|
|
case SRB_STATUS_BUS_RESET:
|
|
|
|
case SRB_STATUS_COMMAND_TIMEOUT:
|
|
|
|
case SRB_STATUS_SELECTION_TIMEOUT:
|
|
|
|
return STATUS_DEVICE_NOT_CONNECTED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return STATUS_IO_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
static
|
2011-05-03 19:40:03 +00:00
|
|
|
NTSTATUS
|
2019-03-31 22:07:55 +00:00
|
|
|
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)
|
2011-05-03 19:40:03 +00:00
|
|
|
{
|
2019-03-31 22:07:55 +00:00
|
|
|
PIO_STACK_LOCATION NextStack;
|
|
|
|
|
|
|
|
RtlZeroMemory(&Context->Urb, sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER));
|
|
|
|
|
|
|
|
Context->Urb.UrbHeader.Length = sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER);
|
|
|
|
Context->Urb.UrbHeader.Function = URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER;
|
2011-05-03 19:40:03 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
Context->Urb.UrbBulkOrInterruptTransfer.PipeHandle = PipeHandle;
|
|
|
|
Context->Urb.UrbBulkOrInterruptTransfer.TransferFlags = TransferFlags;
|
|
|
|
Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength = TransferBufferLength;
|
|
|
|
Context->Urb.UrbBulkOrInterruptTransfer.TransferBuffer = TransferBuffer;
|
|
|
|
Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL = TransferBufferMDL;
|
2011-05-03 19:40:03 +00:00
|
|
|
|
2019-06-25 16:04:20 +00:00
|
|
|
NextStack = IoGetNextIrpStackLocation(Irp);
|
|
|
|
NextStack->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
|
|
|
|
NextStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
|
|
|
|
NextStack->Parameters.Others.Argument1 = &Context->Urb;
|
2011-05-03 19:40:03 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
IoSetCompletionRoutine(Irp,
|
|
|
|
CompletionRoutine,
|
|
|
|
Context,
|
|
|
|
TRUE,
|
|
|
|
TRUE,
|
2019-06-25 16:04:20 +00:00
|
|
|
TRUE);
|
2019-03-31 22:07:55 +00:00
|
|
|
|
|
|
|
return IoCallDriver(FDODeviceExtension->LowerDeviceObject, Irp);
|
2011-05-03 19:40:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
static
|
2012-02-15 03:52:37 +00:00
|
|
|
BOOLEAN
|
|
|
|
USBSTOR_IsCSWValid(
|
|
|
|
PIRP_CONTEXT Context)
|
|
|
|
{
|
2019-03-31 23:33:35 +00:00
|
|
|
if (Context->csw.Signature != CSW_SIGNATURE)
|
2012-02-15 03:52:37 +00:00
|
|
|
{
|
2019-03-31 23:33:35 +00:00
|
|
|
DPRINT1("[USBSTOR] Expected Signature %x but got %x\n", CSW_SIGNATURE, Context->csw.Signature);
|
2012-02-15 03:52:37 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-03-31 23:33:35 +00:00
|
|
|
if (Context->csw.Tag != PtrToUlong(&Context->csw))
|
2012-02-15 03:52:37 +00:00
|
|
|
{
|
2019-03-31 23:33:35 +00:00
|
|
|
DPRINT1("[USBSTOR] Expected Tag %Ix but got %x\n", PtrToUlong(&Context->csw), Context->csw.Tag);
|
2012-02-15 03:52:37 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-04-01 00:03:40 +00:00
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_IssueRequestSense(
|
|
|
|
IN PFDO_DEVICE_EXTENSION FDODeviceExtension,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PIRP_CONTEXT Context);
|
|
|
|
|
2011-05-11 03:49:15 +00:00
|
|
|
IO_COMPLETION_ROUTINE USBSTOR_CSWCompletionRoutine;
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
USBSTOR_CSWCompletionRoutine(
|
|
|
|
PDEVICE_OBJECT DeviceObject,
|
2013-05-27 18:39:32 +00:00
|
|
|
PIRP Irp,
|
2011-05-11 03:49:15 +00:00
|
|
|
PVOID Ctx)
|
|
|
|
{
|
|
|
|
PIRP_CONTEXT Context;
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
2019-03-31 23:33:35 +00:00
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
2019-04-01 00:03:40 +00:00
|
|
|
PFDO_DEVICE_EXTENSION FDODeviceExtension;
|
2011-05-11 03:49:15 +00:00
|
|
|
PSCSI_REQUEST_BLOCK Request;
|
|
|
|
PUFI_CAPACITY_RESPONSE Response;
|
|
|
|
|
|
|
|
Context = (PIRP_CONTEXT)Ctx;
|
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
DPRINT("USBSTOR_CSWCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
|
2012-02-15 03:52:37 +00:00
|
|
|
|
2019-04-13 12:48:14 +00:00
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
|
|
|
|
FDODeviceExtension = Context->FDODeviceExtension;
|
|
|
|
Request = IoStack->Parameters.Scsi.Srb;
|
|
|
|
ASSERT(Request);
|
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
// first check for Irp errors
|
|
|
|
if (!NT_SUCCESS(Irp->IoStatus.Status))
|
2012-02-15 03:52:37 +00:00
|
|
|
{
|
2019-03-27 23:45:32 +00:00
|
|
|
if (USBD_STATUS(Context->Urb.UrbHeader.Status) == USBD_STATUS(USBD_STATUS_STALL_PID))
|
2012-02-15 03:52:37 +00:00
|
|
|
{
|
2019-04-10 00:22:03 +00:00
|
|
|
if (Context->StallRetryCount < 2)
|
2019-03-27 23:45:32 +00:00
|
|
|
{
|
2019-04-10 00:22:03 +00:00
|
|
|
++Context->StallRetryCount;
|
2019-03-27 23:45:32 +00:00
|
|
|
|
|
|
|
// clear stall and resend cbw
|
2019-04-13 12:48:14 +00:00
|
|
|
USBSTOR_QueueResetPipe(FDODeviceExtension, Context);
|
2012-02-15 03:52:37 +00:00
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT1("USBSTOR_CSWCompletionRoutine: Urb.Hdr.Status - %x\n", Context->Urb.UrbHeader.Status);
|
2012-02-15 03:52:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 12:48:14 +00:00
|
|
|
goto ResetRecovery;
|
2012-02-15 03:52:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
// now check the CSW packet validity
|
2019-03-31 23:33:35 +00:00
|
|
|
if (!USBSTOR_IsCSWValid(Context) || Context->csw.Status == CSW_STATUS_PHASE_ERROR)
|
2012-02-15 03:52:37 +00:00
|
|
|
{
|
2019-04-13 12:48:14 +00:00
|
|
|
goto ResetRecovery;
|
2012-02-15 03:52:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
// finally check for CSW errors
|
2019-03-31 23:33:35 +00:00
|
|
|
if (Context->csw.Status == CSW_STATUS_COMMAND_PASSED)
|
2012-02-19 02:47:35 +00:00
|
|
|
{
|
2019-04-01 00:03:40 +00:00
|
|
|
// should happen only when a sense request was sent
|
|
|
|
if (Request != FDODeviceExtension->ActiveSrb)
|
|
|
|
{
|
|
|
|
ASSERT(IoStack->Parameters.Scsi.Srb == &Context->SenseSrb);
|
|
|
|
FDODeviceExtension->ActiveSrb->SenseInfoBufferLength = Request->DataTransferLength;
|
|
|
|
Request = FDODeviceExtension->ActiveSrb;
|
|
|
|
IoStack->Parameters.Scsi.Srb = Request;
|
|
|
|
Request->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
// read capacity needs special work
|
|
|
|
if (Request->Cdb[0] == SCSIOP_READ_CAPACITY)
|
|
|
|
{
|
|
|
|
// get output buffer
|
2019-03-31 23:33:35 +00:00
|
|
|
Response = (PUFI_CAPACITY_RESPONSE)Request->DataBuffer;
|
2011-05-17 11:26:25 +00:00
|
|
|
|
2019-03-27 23:45:32 +00:00
|
|
|
// store in pdo
|
2019-03-31 23:33:35 +00:00
|
|
|
PDODeviceExtension->BlockLength = NTOHL(Response->BlockLength);
|
|
|
|
PDODeviceExtension->LastLogicBlockAddress = NTOHL(Response->LastLogicalBlockAddress);
|
2019-03-27 23:45:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 12:48:14 +00:00
|
|
|
Irp->IoStatus.Status = USBSTOR_SrbStatusToNtStatus(Request);
|
2019-03-27 23:45:32 +00:00
|
|
|
}
|
2019-03-31 23:33:35 +00:00
|
|
|
else if (Context->csw.Status == CSW_STATUS_COMMAND_FAILED)
|
2019-03-27 23:45:32 +00:00
|
|
|
{
|
2019-03-31 20:03:33 +00:00
|
|
|
// the command is correct but with failed status - issue request sense
|
2019-03-27 23:45:32 +00:00
|
|
|
DPRINT("USBSTOR_CSWCompletionRoutine: CSW_STATUS_COMMAND_FAILED\n");
|
2019-03-31 20:03:33 +00:00
|
|
|
|
2019-04-01 00:03:40 +00:00
|
|
|
ASSERT(FDODeviceExtension->ActiveSrb == Request);
|
2019-06-25 16:04:20 +00:00
|
|
|
|
2019-03-31 20:03:33 +00:00
|
|
|
// setting a generic error status, additional information
|
|
|
|
// should be read by higher-level driver from SenseInfoBuffer
|
|
|
|
Request->SrbStatus = SRB_STATUS_ERROR;
|
|
|
|
Request->ScsiStatus = 2;
|
|
|
|
Request->DataTransferLength = 0;
|
|
|
|
|
|
|
|
DPRINT("Flags: %x SBL: %x, buf: %p\n", Request->SrbFlags, Request->SenseInfoBufferLength, Request->SenseInfoBuffer);
|
|
|
|
|
|
|
|
if (!(Request->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE) &&
|
|
|
|
Request->SenseInfoBufferLength &&
|
|
|
|
Request->SenseInfoBuffer)
|
|
|
|
{
|
2019-04-01 00:03:40 +00:00
|
|
|
USBSTOR_IssueRequestSense(FDODeviceExtension, Irp, Context);
|
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
2019-03-31 20:03:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 12:48:14 +00:00
|
|
|
Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
|
2011-05-03 19:40:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 20:03:33 +00:00
|
|
|
Irp->IoStatus.Information = Request->DataTransferLength;
|
|
|
|
|
2012-02-19 02:47:35 +00:00
|
|
|
// terminate current request
|
2019-03-31 23:33:35 +00:00
|
|
|
USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
|
|
|
|
USBSTOR_QueueNextRequest(PDODeviceExtension->LowerDeviceObject);
|
2011-05-11 03:49:15 +00:00
|
|
|
|
2019-03-31 23:33:35 +00:00
|
|
|
ExFreePoolWithTag(Context, USB_STOR_TAG);
|
2019-04-13 12:48:14 +00:00
|
|
|
return STATUS_CONTINUE_COMPLETION;
|
|
|
|
|
|
|
|
ResetRecovery:
|
|
|
|
|
|
|
|
Request = FDODeviceExtension->ActiveSrb;
|
|
|
|
IoStack->Parameters.Scsi.Srb = Request;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
|
|
|
|
Request->SrbStatus = SRB_STATUS_BUS_RESET;
|
|
|
|
|
|
|
|
USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
|
|
|
|
USBSTOR_QueueResetDevice(FDODeviceExtension);
|
|
|
|
|
|
|
|
ExFreePoolWithTag(Context, USB_STOR_TAG);
|
|
|
|
return STATUS_CONTINUE_COMPLETION;
|
2011-05-03 19:40:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_SendCSWRequest(
|
2012-02-17 03:02:14 +00:00
|
|
|
PIRP_CONTEXT Context,
|
|
|
|
PIRP Irp)
|
2011-05-03 19:40:03 +00:00
|
|
|
{
|
2019-03-31 22:07:55 +00:00
|
|
|
return USBSTOR_IssueBulkOrInterruptRequest(Context->FDODeviceExtension,
|
|
|
|
Irp,
|
|
|
|
Context->FDODeviceExtension->InterfaceInformation->Pipes[Context->FDODeviceExtension->BulkInPipeIndex].PipeHandle,
|
|
|
|
USBD_TRANSFER_DIRECTION_IN,
|
|
|
|
sizeof(CSW),
|
2019-03-31 23:33:35 +00:00
|
|
|
&Context->csw,
|
2019-03-31 22:07:55 +00:00
|
|
|
NULL,
|
|
|
|
USBSTOR_CSWCompletionRoutine,
|
|
|
|
Context);
|
2012-02-17 03:02:14 +00:00
|
|
|
}
|
2011-05-13 09:14:28 +00:00
|
|
|
|
2012-02-17 03:02:14 +00:00
|
|
|
IO_COMPLETION_ROUTINE USBSTOR_DataCompletionRoutine;
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
USBSTOR_DataCompletionRoutine(
|
|
|
|
PDEVICE_OBJECT DeviceObject,
|
2013-05-27 18:39:32 +00:00
|
|
|
PIRP Irp,
|
2012-02-17 03:02:14 +00:00
|
|
|
PVOID Ctx)
|
|
|
|
{
|
|
|
|
PIRP_CONTEXT Context;
|
2019-03-31 22:07:55 +00:00
|
|
|
PIO_STACK_LOCATION IoStack;
|
|
|
|
PSCSI_REQUEST_BLOCK Request;
|
2019-04-13 12:48:14 +00:00
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
2012-02-17 03:02:14 +00:00
|
|
|
|
|
|
|
DPRINT("USBSTOR_DataCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
|
|
|
|
|
|
|
|
Context = (PIRP_CONTEXT)Ctx;
|
2019-03-31 22:07:55 +00:00
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
Request = IoStack->Parameters.Scsi.Srb;
|
2019-04-13 12:48:14 +00:00
|
|
|
PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
|
2019-03-31 22:07:55 +00:00
|
|
|
|
2019-04-01 00:03:40 +00:00
|
|
|
// for Sense Request a partial MDL was already freed (if existed)
|
|
|
|
if (Request == Context->FDODeviceExtension->ActiveSrb &&
|
|
|
|
Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL != Irp->MdlAddress)
|
2019-03-31 22:07:55 +00:00
|
|
|
{
|
|
|
|
IoFreeMdl(Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL);
|
|
|
|
}
|
2012-02-17 03:02:14 +00:00
|
|
|
|
2019-03-31 20:03:33 +00:00
|
|
|
if (NT_SUCCESS(Irp->IoStatus.Status))
|
|
|
|
{
|
|
|
|
if (Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength < Request->DataTransferLength)
|
|
|
|
{
|
|
|
|
Request->SrbStatus = SRB_STATUS_DATA_OVERRUN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Request->SrbStatus = SRB_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Request->DataTransferLength = Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength;
|
2019-03-31 22:07:55 +00:00
|
|
|
USBSTOR_SendCSWRequest(Context, Irp);
|
2019-03-31 20:03:33 +00:00
|
|
|
}
|
|
|
|
else if (USBD_STATUS(Context->Urb.UrbHeader.Status) == USBD_STATUS(USBD_STATUS_STALL_PID))
|
2012-02-17 03:02:14 +00:00
|
|
|
{
|
2019-04-10 00:22:03 +00:00
|
|
|
++Context->StallRetryCount;
|
2019-03-31 20:03:33 +00:00
|
|
|
|
|
|
|
Request->SrbStatus = SRB_STATUS_DATA_OVERRUN;
|
|
|
|
Request->DataTransferLength = Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength;
|
|
|
|
|
2012-02-17 03:02:14 +00:00
|
|
|
// clear stall and resend cbw
|
2019-04-13 12:48:14 +00:00
|
|
|
USBSTOR_QueueResetPipe(Context->FDODeviceExtension, Context);
|
2012-02-17 03:02:14 +00:00
|
|
|
}
|
2019-03-31 20:03:33 +00:00
|
|
|
else
|
|
|
|
{
|
2019-04-13 12:48:14 +00:00
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
|
|
|
|
Request->SrbStatus = SRB_STATUS_BUS_RESET;
|
|
|
|
|
|
|
|
USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
|
|
|
|
USBSTOR_QueueResetDevice(Context->FDODeviceExtension);
|
|
|
|
|
|
|
|
ExFreePoolWithTag(Context, USB_STOR_TAG);
|
|
|
|
return STATUS_CONTINUE_COMPLETION;
|
2019-03-31 20:03:33 +00:00
|
|
|
}
|
2012-02-17 03:02:14 +00:00
|
|
|
|
2011-05-11 03:49:15 +00:00
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
|
|
|
}
|
|
|
|
|
|
|
|
IO_COMPLETION_ROUTINE USBSTOR_CBWCompletionRoutine;
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
USBSTOR_CBWCompletionRoutine(
|
|
|
|
PDEVICE_OBJECT DeviceObject,
|
2013-05-27 18:39:32 +00:00
|
|
|
PIRP Irp,
|
2011-05-11 03:49:15 +00:00
|
|
|
PVOID Ctx)
|
|
|
|
{
|
|
|
|
PIRP_CONTEXT Context;
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
2019-03-31 20:03:33 +00:00
|
|
|
PSCSI_REQUEST_BLOCK Request;
|
2019-04-13 12:48:14 +00:00
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
2011-05-13 14:44:24 +00:00
|
|
|
USBD_PIPE_HANDLE PipeHandle;
|
2019-03-31 22:07:55 +00:00
|
|
|
ULONG TransferFlags;
|
|
|
|
PMDL Mdl = NULL;
|
|
|
|
PVOID TransferBuffer = NULL;
|
2011-05-11 03:49:15 +00:00
|
|
|
|
2012-02-17 03:02:14 +00:00
|
|
|
DPRINT("USBSTOR_CBWCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
|
2011-05-03 19:40:03 +00:00
|
|
|
|
2011-05-11 03:49:15 +00:00
|
|
|
Context = (PIRP_CONTEXT)Ctx;
|
2019-03-31 20:03:33 +00:00
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
Request = IoStack->Parameters.Scsi.Srb;
|
2019-04-13 12:48:14 +00:00
|
|
|
PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
|
2019-03-31 20:03:33 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Irp->IoStatus.Status))
|
|
|
|
{
|
2019-03-31 22:07:55 +00:00
|
|
|
goto ResetRecovery;
|
|
|
|
}
|
|
|
|
|
2019-04-01 00:03:40 +00:00
|
|
|
// a request without the buffer AND not a sense request
|
|
|
|
// for a sense request we provide just a TransferBuffer, an Mdl will be allocated by usbport (see below)
|
|
|
|
if (!Irp->MdlAddress && Request == Context->FDODeviceExtension->ActiveSrb)
|
2019-03-31 22:07:55 +00:00
|
|
|
{
|
|
|
|
Request->SrbStatus = SRB_STATUS_SUCCESS;
|
|
|
|
USBSTOR_SendCSWRequest(Context, Irp);
|
2019-03-31 20:03:33 +00:00
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
|
|
|
}
|
2011-05-03 19:40:03 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
// a request with the data buffer
|
|
|
|
|
|
|
|
if ((Request->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) == SRB_FLAGS_DATA_IN)
|
|
|
|
{
|
|
|
|
PipeHandle = Context->FDODeviceExtension->InterfaceInformation->Pipes[Context->FDODeviceExtension->BulkInPipeIndex].PipeHandle;
|
|
|
|
TransferFlags = USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK;
|
|
|
|
}
|
|
|
|
else if ((Request->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) == SRB_FLAGS_DATA_OUT)
|
|
|
|
{
|
|
|
|
PipeHandle = Context->FDODeviceExtension->InterfaceInformation->Pipes[Context->FDODeviceExtension->BulkOutPipeIndex].PipeHandle;
|
|
|
|
TransferFlags = USBD_TRANSFER_DIRECTION_OUT;
|
|
|
|
}
|
|
|
|
else
|
2011-05-11 03:49:15 +00:00
|
|
|
{
|
2019-03-31 22:07:55 +00:00
|
|
|
// we check the validity of a request in disk.c so we should never be here
|
|
|
|
DPRINT1("Warning: shouldn't be here\n");
|
|
|
|
goto ResetRecovery;
|
|
|
|
}
|
2011-05-13 14:44:24 +00:00
|
|
|
|
2019-04-01 00:03:40 +00:00
|
|
|
// if it is not a Sense Request
|
|
|
|
if (Request == Context->FDODeviceExtension->ActiveSrb)
|
|
|
|
{
|
2019-03-31 22:07:55 +00:00
|
|
|
if (MmGetMdlVirtualAddress(Irp->MdlAddress) == Request->DataBuffer)
|
2011-05-13 14:44:24 +00:00
|
|
|
{
|
2019-03-31 22:07:55 +00:00
|
|
|
Mdl = Irp->MdlAddress;
|
2011-05-13 14:44:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-31 22:07:55 +00:00
|
|
|
Mdl = IoAllocateMdl(Request->DataBuffer,
|
|
|
|
Request->DataTransferLength,
|
|
|
|
FALSE,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
2019-06-25 16:04:20 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
if (Mdl)
|
|
|
|
{
|
|
|
|
IoBuildPartialMdl(Irp->MdlAddress,
|
|
|
|
Mdl,
|
|
|
|
Request->DataBuffer,
|
|
|
|
Request->DataTransferLength);
|
|
|
|
}
|
2011-05-13 14:44:24 +00:00
|
|
|
}
|
2011-05-13 09:14:28 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
if (!Mdl)
|
2019-03-31 20:03:33 +00:00
|
|
|
{
|
2019-04-01 00:03:40 +00:00
|
|
|
DPRINT1("USBSTOR_CBWCompletionRoutine: Mdl - %p\n", Mdl);
|
2019-03-31 22:07:55 +00:00
|
|
|
goto ResetRecovery;
|
2019-03-31 20:03:33 +00:00
|
|
|
}
|
2019-04-01 00:03:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-13 12:48:14 +00:00
|
|
|
ASSERT(Request->DataBuffer);
|
2019-04-01 00:03:40 +00:00
|
|
|
TransferBuffer = Request->DataBuffer;
|
|
|
|
}
|
2019-03-31 20:03:33 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
USBSTOR_IssueBulkOrInterruptRequest(Context->FDODeviceExtension,
|
|
|
|
Irp,
|
|
|
|
PipeHandle,
|
|
|
|
TransferFlags,
|
|
|
|
Request->DataTransferLength,
|
|
|
|
TransferBuffer,
|
|
|
|
Mdl,
|
|
|
|
USBSTOR_DataCompletionRoutine,
|
|
|
|
Context);
|
2019-03-31 20:03:33 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
2011-05-13 09:14:28 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
ResetRecovery:
|
2019-04-13 12:48:14 +00:00
|
|
|
Request = Context->FDODeviceExtension->ActiveSrb;
|
|
|
|
IoStack->Parameters.Scsi.Srb = Request;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
|
|
|
|
Request->SrbStatus = SRB_STATUS_BUS_RESET;
|
|
|
|
|
|
|
|
USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
|
|
|
|
USBSTOR_QueueResetDevice(Context->FDODeviceExtension);
|
|
|
|
|
|
|
|
ExFreePoolWithTag(Context, USB_STOR_TAG);
|
|
|
|
return STATUS_CONTINUE_COMPLETION;
|
2011-05-03 19:40:03 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 03:52:37 +00:00
|
|
|
VOID
|
|
|
|
DumpCBW(
|
|
|
|
PUCHAR Block)
|
|
|
|
{
|
2013-05-26 17:49:43 +00:00
|
|
|
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",
|
2012-02-15 03:52:37 +00:00
|
|
|
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,
|
|
|
|
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,
|
2013-05-27 18:39:32 +00:00
|
|
|
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,
|
2012-02-15 03:52:37 +00:00
|
|
|
Block[30] & 0xFF);
|
2012-02-17 03:02:14 +00:00
|
|
|
}
|
2012-02-15 03:52:37 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
static
|
2011-05-03 19:40:03 +00:00
|
|
|
NTSTATUS
|
2019-03-31 22:07:55 +00:00
|
|
|
USBSTOR_SendCBWRequest(
|
|
|
|
IN PFDO_DEVICE_EXTENSION FDODeviceExtension,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PIRP_CONTEXT Context)
|
2011-05-03 19:40:03 +00:00
|
|
|
{
|
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
2019-03-31 22:07:55 +00:00
|
|
|
PIO_STACK_LOCATION IoStack;
|
|
|
|
PSCSI_REQUEST_BLOCK Request;
|
2011-05-11 03:49:15 +00:00
|
|
|
|
2019-03-31 23:33:35 +00:00
|
|
|
RtlZeroMemory(&Context->cbw, sizeof(CBW));
|
|
|
|
RtlZeroMemory(&Context->Urb, sizeof(URB));
|
2011-05-03 19:40:03 +00:00
|
|
|
|
2019-03-31 22:07:55 +00:00
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
PDODeviceExtension = IoStack->DeviceObject->DeviceExtension;
|
|
|
|
Request = IoStack->Parameters.Scsi.Srb;
|
2011-05-03 19:40:03 +00:00
|
|
|
|
2019-03-31 23:33:35 +00:00
|
|
|
Context->cbw.Signature = CBW_SIGNATURE;
|
|
|
|
Context->cbw.Tag = PtrToUlong(&Context->cbw);
|
|
|
|
Context->cbw.DataTransferLength = Request->DataTransferLength;
|
|
|
|
Context->cbw.Flags = ((UCHAR)Request->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) << 1;
|
|
|
|
Context->cbw.LUN = PDODeviceExtension->LUN;
|
|
|
|
Context->cbw.CommandBlockLength = Request->CdbLength;
|
2019-03-31 22:07:55 +00:00
|
|
|
|
2019-03-31 23:33:35 +00:00
|
|
|
RtlCopyMemory(&Context->cbw.CommandBlock, Request->Cdb, Request->CdbLength);
|
2011-05-11 03:49:15 +00:00
|
|
|
|
2019-03-31 23:33:35 +00:00
|
|
|
DPRINT("CBW for IRP %p\n", Irp);
|
|
|
|
DumpCBW((PUCHAR)&Context->cbw);
|
2012-02-11 19:26:18 +00:00
|
|
|
|
2011-05-11 03:49:15 +00:00
|
|
|
// initialize rest of context
|
2019-03-31 22:07:55 +00:00
|
|
|
Context->Irp = Irp;
|
2011-05-11 03:49:15 +00:00
|
|
|
Context->FDODeviceExtension = FDODeviceExtension;
|
2019-04-10 00:22:03 +00:00
|
|
|
Context->StallRetryCount = 0;
|
2019-03-31 22:07:55 +00:00
|
|
|
|
|
|
|
return USBSTOR_IssueBulkOrInterruptRequest(
|
|
|
|
FDODeviceExtension,
|
|
|
|
Irp,
|
|
|
|
FDODeviceExtension->InterfaceInformation->Pipes[FDODeviceExtension->BulkOutPipeIndex].PipeHandle,
|
|
|
|
USBD_TRANSFER_DIRECTION_OUT,
|
|
|
|
sizeof(CBW),
|
2019-03-31 23:33:35 +00:00
|
|
|
&Context->cbw,
|
2019-03-31 22:07:55 +00:00
|
|
|
NULL,
|
|
|
|
USBSTOR_CBWCompletionRoutine,
|
|
|
|
Context);
|
2012-02-17 03:02:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 00:03:40 +00:00
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_IssueRequestSense(
|
|
|
|
IN PFDO_DEVICE_EXTENSION FDODeviceExtension,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PIRP_CONTEXT Context)
|
|
|
|
{
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
|
|
|
PSCSI_REQUEST_BLOCK CurrentSrb;
|
|
|
|
PSCSI_REQUEST_BLOCK SenseSrb;
|
|
|
|
|
|
|
|
DPRINT("USBSTOR_IssueRequestSense: \n");
|
|
|
|
|
|
|
|
CurrentSrb = FDODeviceExtension->ActiveSrb;
|
|
|
|
SenseSrb = &Context->SenseSrb;
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
IoStack->Parameters.Scsi.Srb = SenseSrb;
|
|
|
|
|
|
|
|
RtlZeroMemory(SenseSrb, sizeof(*SenseSrb));
|
|
|
|
|
|
|
|
SenseSrb->Function = SRB_FUNCTION_EXECUTE_SCSI;
|
|
|
|
SenseSrb->Length = sizeof(*SenseSrb);
|
|
|
|
SenseSrb->CdbLength = CDB6GENERIC_LENGTH;
|
|
|
|
SenseSrb->SrbFlags = SRB_FLAGS_DATA_IN |
|
|
|
|
SRB_FLAGS_NO_QUEUE_FREEZE |
|
|
|
|
SRB_FLAGS_DISABLE_AUTOSENSE;
|
|
|
|
|
|
|
|
ASSERT(CurrentSrb->SenseInfoBufferLength);
|
|
|
|
ASSERT(CurrentSrb->SenseInfoBuffer);
|
|
|
|
DPRINT("SenseInfoBuffer %x, SenseInfoBufferLength %x\n", CurrentSrb->SenseInfoBuffer, CurrentSrb->SenseInfoBufferLength);
|
|
|
|
|
|
|
|
SenseSrb->DataTransferLength = CurrentSrb->SenseInfoBufferLength;
|
|
|
|
SenseSrb->DataBuffer = CurrentSrb->SenseInfoBuffer;
|
|
|
|
|
2019-06-25 16:04:20 +00:00
|
|
|
SrbGetCdb(SenseSrb)->CDB6GENERIC.OperationCode = SCSIOP_REQUEST_SENSE;
|
|
|
|
SrbGetCdb(SenseSrb)->AsByte[4] = CurrentSrb->SenseInfoBufferLength;
|
2019-04-01 00:03:40 +00:00
|
|
|
|
|
|
|
return USBSTOR_SendCBWRequest(FDODeviceExtension, Irp, Context);
|
|
|
|
}
|
|
|
|
|
2011-05-14 18:09:00 +00:00
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_HandleExecuteSCSI(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
2019-04-10 00:22:03 +00:00
|
|
|
IN PIRP Irp)
|
2011-05-14 18:09:00 +00:00
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
|
|
|
PSCSI_REQUEST_BLOCK Request;
|
2011-05-15 14:59:16 +00:00
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
2019-03-31 23:33:35 +00:00
|
|
|
PIRP_CONTEXT Context;
|
2011-05-15 14:59:16 +00:00
|
|
|
|
|
|
|
PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
|
|
|
|
ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
|
2011-05-14 18:09:00 +00:00
|
|
|
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
2019-03-26 11:26:06 +00:00
|
|
|
Request = IoStack->Parameters.Scsi.Srb;
|
2011-05-14 18:09:00 +00:00
|
|
|
|
2019-06-25 16:04:20 +00:00
|
|
|
DPRINT("USBSTOR_HandleExecuteSCSI Operation Code %x, Length %lu\n", SrbGetCdb(Request)->CDB10.OperationCode, Request->DataTransferLength);
|
2011-05-14 18:09:00 +00:00
|
|
|
|
2019-03-26 11:26:06 +00:00
|
|
|
// check that we're sending to the right LUN
|
2019-06-25 16:04:20 +00:00
|
|
|
ASSERT(SrbGetCdb(Request)->CDB10.LogicalUnitNumber == (PDODeviceExtension->LUN & MAX_LUN));
|
2019-03-31 23:33:35 +00:00
|
|
|
Context = ExAllocatePoolWithTag(NonPagedPool, sizeof(IRP_CONTEXT), USB_STOR_TAG);
|
|
|
|
|
|
|
|
if (!Context)
|
|
|
|
{
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = USBSTOR_SendCBWRequest(PDODeviceExtension->LowerDeviceObject->DeviceExtension, Irp, Context);
|
|
|
|
}
|
2011-05-14 18:09:00 +00:00
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|