[USBSTOR]
[reactos.git] / reactos / drivers / usb / usbstor / scsi.c
index 8a12446..ee5bfba 100644 (file)
@@ -11,6 +11,9 @@
 
 #include "usbstor.h"
 
+#define NDEBUG
+#include <debug.h>
+
 NTSTATUS
 USBSTOR_BuildCBW(
     IN ULONG Tag,
@@ -171,7 +174,7 @@ NTSTATUS
 NTAPI
 USBSTOR_CSWCompletionRoutine(
     PDEVICE_OBJECT DeviceObject,
-    PIRP Irp, 
+    PIRP Irp,
     PVOID Ctx)
 {
     PIRP_CONTEXT Context;
@@ -181,9 +184,7 @@ USBSTOR_CSWCompletionRoutine(
     PREAD_CAPACITY_DATA_EX CapacityDataEx;
     PREAD_CAPACITY_DATA CapacityData;
     PUFI_CAPACITY_RESPONSE Response;
-
     NTSTATUS Status;
-    PURB Urb;
 
     //
     // access context
@@ -274,7 +275,6 @@ USBSTOR_CSWCompletionRoutine(
     ASSERT(Request);
 
     Status = Irp->IoStatus.Status;
-    Urb = &Context->Urb;
 
     //
     // get SCSI command data block
@@ -430,7 +430,7 @@ NTSTATUS
 NTAPI
 USBSTOR_DataCompletionRoutine(
     PDEVICE_OBJECT DeviceObject,
-    PIRP Irp, 
+    PIRP Irp,
     PVOID Ctx)
 {
     PIRP_CONTEXT Context;
@@ -475,7 +475,7 @@ NTSTATUS
 NTAPI
 USBSTOR_CBWCompletionRoutine(
     PDEVICE_OBJECT DeviceObject,
-    PIRP Irp, 
+    PIRP Irp,
     PVOID Ctx)
 {
     PIRP_CONTEXT Context;
@@ -509,7 +509,7 @@ USBSTOR_CBWCompletionRoutine(
         {
             //
             // write request use bulk out pipe
-            // 
+            //
             PipeHandle = Context->FDODeviceExtension->InterfaceInformation->Pipes[Context->FDODeviceExtension->BulkOutPipeIndex].PipeHandle;
         }
         else
@@ -579,10 +579,10 @@ VOID
 DumpCBW(
     PUCHAR Block)
 {
-    DPRINT1("%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",
+    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",
         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,
-        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, 
+        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,
         Block[30] & 0xFF);
 
 }
@@ -726,6 +726,8 @@ USBSTOR_SendRequest(
                             //
                             // failed to allocate MDL
                             //
+                            FreeItem(Context->cbw);
+                            FreeItem(Context);
                             return STATUS_INSUFFICIENT_RESOURCES;
                         }
 
@@ -755,6 +757,8 @@ USBSTOR_SendRequest(
                     //
                     // failed to allocate MDL
                     //
+                    FreeItem(Context->cbw);
+                    FreeItem(Context);
                     return STATUS_INSUFFICIENT_RESOURCES;
                 }
 
@@ -775,6 +779,8 @@ USBSTOR_SendRequest(
                 //
                 // failed to allocate MDL
                 //
+                FreeItem(Context->cbw);
+                FreeItem(Context);
                 return STATUS_INSUFFICIENT_RESOURCES;
             }
 
@@ -1079,11 +1085,11 @@ USBSTOR_SendModeSense(
     // first struct is the header
     // MODE_PARAMETER_HEADER / _MODE_PARAMETER_HEADER10
     //
-    // followed by 
+    // followed by
     // MODE_PARAMETER_BLOCK
     //
-    // 
-    UNIMPLEMENTED
+    //
+    UNIMPLEMENTED;
 
     //
     // send csw
@@ -1195,7 +1201,7 @@ USBSTOR_SendReadWrite(
     // sanity check
     //
     Temp = (Cmd.ContiguousLogicBlocksByte0 << 8 | Cmd.ContiguousLogicBlocksByte1);
-    ASSERT(NTOHL(Temp == BlockCount));
+    ASSERT(Temp == BlockCount);
 
     DPRINT("USBSTOR_SendReadWrite BlockAddress %x%x%x%x BlockCount %lu BlockLength %lu\n", Cmd.LogicalBlockByte0, Cmd.LogicalBlockByte1, Cmd.LogicalBlockByte2, Cmd.LogicalBlockByte3, BlockCount, PDODeviceExtension->BlockLength);
 
@@ -1249,6 +1255,52 @@ USBSTOR_SendTestUnit(
     return USBSTOR_SendRequest(DeviceObject, Irp, UFI_TEST_UNIT_CMD_LEN, (PUCHAR)&Cmd, 0, NULL, RetryCount);
 }
 
+NTSTATUS
+USBSTOR_SendUnknownRequest(
+    IN PDEVICE_OBJECT DeviceObject,
+    IN OUT PIRP Irp,
+    IN ULONG RetryCount)
+{
+    PPDO_DEVICE_EXTENSION PDODeviceExtension;
+    PIO_STACK_LOCATION IoStack;
+    PSCSI_REQUEST_BLOCK Request;
+    UFI_UNKNOWN_CMD Cmd;
+
+    //
+    // get current stack location
+    //
+    IoStack = IoGetCurrentIrpStackLocation(Irp);
+
+    //
+    // get request block
+    //
+    Request = IoStack->Parameters.Others.Argument1;
+
+    //
+    // get PDO device extension
+    //
+    PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+
+    //
+    // check that we're sending to the right LUN
+    //
+    ASSERT(Request->Cdb[1] == (PDODeviceExtension->LUN & MAX_LUN));
+
+    //
+    // sanity check
+    //
+    ASSERT(Request->CdbLength <= sizeof(UFI_UNKNOWN_CMD));
+
+    //
+    // initialize test unit cmd
+    //
+    RtlCopyMemory(&Cmd, Request->Cdb, Request->CdbLength);
+
+    //
+    // send the request
+    //
+    return USBSTOR_SendRequest(DeviceObject, Irp, Request->CdbLength, (PUCHAR)&Cmd, Request->DataTransferLength, Request->DataBuffer, RetryCount);
+}
 
 NTSTATUS
 USBSTOR_HandleExecuteSCSI(
@@ -1368,10 +1420,9 @@ USBSTOR_HandleExecuteSCSI(
     }
     else
     {
-        DPRINT1("UNIMPLEMENTED Operation Code %x\n", pCDB->AsByte[0]);
-        Request->SrbStatus = SRB_STATUS_ERROR;
-        Status = STATUS_NOT_SUPPORTED;
-        DbgBreakPoint();
+        // Unknown request. Simply forward
+        DPRINT1("Forwarding unknown Operation Code %x\n", pCDB->AsByte[0]);
+        Status = USBSTOR_SendUnknownRequest(DeviceObject, Irp, RetryCount);
     }
 
     return Status;