[USBSTOR]
[reactos.git] / drivers / usb / usbstor / scsi.c
index 8e81327..68b7aac 100644 (file)
@@ -31,7 +31,7 @@ USBSTOR_BuildCBW(
     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;
 
@@ -102,6 +102,9 @@ USBSTOR_CSWCompletionRoutine(
     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);
 
@@ -153,16 +156,65 @@ USBSTOR_CSWCompletionRoutine(
         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
         //
@@ -230,14 +282,14 @@ USBSTOR_CSWCompletionRoutine(
         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
@@ -253,6 +305,10 @@ USBSTOR_CSWCompletionRoutine(
         KeSetEvent(Context->Event, 0, FALSE);
     }
 
+    //
+    // free our allocated irp
+    //
+    IoFreeIrp(Irp);
 
     //
     // free context
@@ -391,7 +447,7 @@ USBSTOR_CBWCompletionRoutine(
                                                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);
 
         //
@@ -495,7 +551,7 @@ USBSTOR_SendRequest(
                                            Context->cbw,
                                            NULL,
                                            sizeof(CBW),
-                                           USBD_TRANSFER_DIRECTION_OUT | USBD_SHORT_TRANSFER_OK,
+                                           USBD_TRANSFER_DIRECTION_OUT,
                                            NULL);
 
     //
@@ -514,7 +570,7 @@ USBSTOR_SendRequest(
     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)
         {
@@ -527,13 +583,38 @@ USBSTOR_SendRequest(
                 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
             {
@@ -758,6 +839,16 @@ USBSTOR_SendModeSenseCmd(
     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
     //
@@ -772,23 +863,9 @@ USBSTOR_SendModeSenseCmd(
     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
     //
@@ -1123,13 +1200,9 @@ USBSTOR_HandleExecuteSCSI(
         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
         //
@@ -1148,7 +1221,7 @@ USBSTOR_HandleExecuteSCSI(
     }
     else
     {
-        UNIMPLEMENTED;
+        DPRINT1("UNIMPLEMENTED Operation Code %x\n", pCDB->AsByte[0]);
         Request->SrbStatus = SRB_STATUS_ERROR;
         Status = STATUS_NOT_SUPPORTED;
         DbgBreakPoint();