Control->Signature = CBW_SIGNATURE;
Control->Tag = Tag;
Control->DataTransferLength = DataTransferLength;
- Control->Flags = 0x80;
+ Control->Flags = (CommandBlock[0] != SCSIOP_WRITE) ? 0x80 : 0x00;
Control->LUN = (LUN & MAX_LUN);
Control->CommandBlockLength = CommandBlockLength;
PREAD_CAPACITY_DATA_EX CapacityDataEx;
PREAD_CAPACITY_DATA CapacityData;
PUFI_CAPACITY_RESPONSE Response;
+ PERRORHANDLER_WORKITEM_DATA ErrorHandlerWorkItemData;
+ NTSTATUS Status;
+ PURB Urb;
DPRINT("USBSTOR_CSWCompletionRoutine Irp %p Ctx %p\n", Irp, Ctx);
Request = (PSCSI_REQUEST_BLOCK)IoStack->Parameters.Others.Argument1;
ASSERT(Request);
- //
- // FIXME: check status
- //
- Request->SrbStatus = SRB_STATUS_SUCCESS;
+ Status = Irp->IoStatus.Status;
+
+ Urb = &Context->Urb;
//
// get SCSI command data block
//
pCDB = (PCDB)Request->Cdb;
+ //
+ // check status
+ //
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Status %x\n", Status);
+ DPRINT1("UrbStatus %x\n", Urb->UrbHeader.Status);
+
+ //
+ // Check for errors that can be handled
+ // FIXME: Verify all usb errors that can be recovered via pipe reset/port reset/controller reset
+ //
+ if ((Urb->UrbHeader.Status & USB_RECOVERABLE_ERRORS) == Urb->UrbHeader.Status)
+ {
+ DPRINT1("Attempting Error Recovery\n");
+ //
+ // free the allocated irp
+ //
+ IoFreeIrp(Irp);
+
+ //
+ // Allocate Work Item Data
+ //
+ ErrorHandlerWorkItemData = ExAllocatePoolWithTag(NonPagedPool, sizeof(ERRORHANDLER_WORKITEM_DATA), USB_STOR_TAG);
+ if (!ErrorHandlerWorkItemData)
+ {
+ DPRINT1("Failed to allocate memory\n");
+ Status = STATUS_INSUFFICIENT_RESOURCES;
+ }
+ else
+ {
+ //
+ // Initialize and queue the work item to handle the error
+ //
+ ExInitializeWorkItem(&ErrorHandlerWorkItemData->WorkQueueItem,
+ ErrorHandlerWorkItemRoutine,
+ ErrorHandlerWorkItemData);
+
+ ErrorHandlerWorkItemData->DeviceObject = Context->FDODeviceExtension->FunctionalDeviceObject;
+ ErrorHandlerWorkItemData->Context = Context;
+ DPRINT1("Queuing WorkItemROutine\n");
+ ExQueueWorkItem(&ErrorHandlerWorkItemData->WorkQueueItem, DelayedWorkQueue);
+
+ return STATUS_MORE_PROCESSING_REQUIRED;
+ }
+ }
+ }
+
+ Request->SrbStatus = SRB_STATUS_SUCCESS;
+
//
// read capacity needs special work
//
Context->Irp->IoStatus.Information = Context->TransferDataLength;
//
- // complete request
+ // terminate current request
//
- IoCompleteRequest(Context->Irp, IO_NO_INCREMENT);
+ USBSTOR_QueueTerminateRequest(Context->PDODeviceExtension->LowerDeviceObject, Context->Irp);
//
- // terminate current request
+ // complete request
//
- USBSTOR_QueueTerminateRequest(Context->PDODeviceExtension->LowerDeviceObject, TRUE);
+ IoCompleteRequest(Context->Irp, IO_NO_INCREMENT);
//
// start next request
KeSetEvent(Context->Event, 0, FALSE);
}
+ //
+ // free our allocated irp
+ //
+ IoFreeIrp(Irp);
//
// free context
NULL,
Context->TransferBufferMDL,
Context->TransferDataLength,
- USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK,
+ ((Code == SCSIOP_WRITE) ? USBD_TRANSFER_DIRECTION_OUT : (USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK)),
NULL);
//
Context->cbw,
NULL,
sizeof(CBW),
- USBD_TRANSFER_DIRECTION_OUT | USBD_SHORT_TRANSFER_OK,
+ USBD_TRANSFER_DIRECTION_OUT,
NULL);
//
if (Context->TransferDataLength)
{
//
- // check if the original request already does not have an mdl associated
+ // check if the original request already does have an mdl associated
//
if (OriginalRequest)
{
if (CommandLength == UFI_READ_WRITE_CMD_LEN)
{
MdlVirtualAddress = MmGetMdlVirtualAddress(OriginalRequest->MdlAddress);
- ASSERT(MdlVirtualAddress == Context->TransferData);
+
+ //
+ // is there an offset
+ //
+ if (MdlVirtualAddress != Context->TransferData)
+ {
+ //
+ // lets build an mdl
+ //
+ Context->TransferBufferMDL = IoAllocateMdl(Context->TransferData, MmGetMdlByteCount(OriginalRequest->MdlAddress), FALSE, FALSE, NULL);
+ if (!Context->TransferBufferMDL)
+ {
+ //
+ // failed to allocate MDL
+ //
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ //
+ // now build the partial mdl
+ //
+ IoBuildPartialMdl(OriginalRequest->MdlAddress, Context->TransferBufferMDL, Context->TransferData, Context->TransferDataLength);
+ }
}
- //
- // I/O paging request
- //
- Context->TransferBufferMDL = OriginalRequest->MdlAddress;
+ if (!Context->TransferBufferMDL)
+ {
+ //
+ // I/O paging request
+ //
+ Context->TransferBufferMDL = OriginalRequest->MdlAddress;
+ }
}
else
{
PIO_STACK_LOCATION IoStack;
PSCSI_REQUEST_BLOCK Request;
+ //
+ // get PDO device extension
+ //
+ PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+
+ //
+ // sanity check
+ //
+ ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
+
//
// get current stack location
//
Request->SrbStatus = SRB_STATUS_SUCCESS;
Irp->IoStatus.Information = Request->DataTransferLength;
Irp->IoStatus.Status = STATUS_SUCCESS;
+ USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
IoCompleteRequest(Irp, IO_NO_INCREMENT);
- //
- // get PDO device extension
- //
- PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
-
- //
- // sanity check
- //
- ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
-
- //
- // terminate current request
- //
- USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, TRUE);
-
//
// start next request
//
Request->SrbStatus = SRB_STATUS_SUCCESS;
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = Request->DataTransferLength;
+ USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
IoCompleteRequest(Irp, IO_NO_INCREMENT);
- //
- // terminate current request
- //
- USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, TRUE);
-
//
// start next request
//
}
else
{
- UNIMPLEMENTED;
+ DPRINT1("UNIMPLEMENTED Operation Code %x\n", pCDB->AsByte[0]);
Request->SrbStatus = SRB_STATUS_ERROR;
Status = STATUS_NOT_SUPPORTED;
DbgBreakPoint();