[USBAUDIO]
[reactos.git] / reactos / drivers / usb / usbstor / misc.c
index 4c49e75..6b60cf8 100644 (file)
@@ -11,6 +11,9 @@
 
 #include "usbstor.h"
 
+#define NDEBUG
+#include <debug.h>
+
 //
 // driver verifier
 //
@@ -20,7 +23,7 @@ NTSTATUS
 NTAPI
 USBSTOR_SyncForwardIrpCompletionRoutine(
     PDEVICE_OBJECT DeviceObject,
-    PIRP Irp, 
+    PIRP Irp,
     PVOID Context)
 {
     if (Irp->PendingReturned)
@@ -289,7 +292,6 @@ USBSTOR_ClassRequest(
 
 {
     PURB Urb;
-    PUCHAR Buffer;
     NTSTATUS Status;
 
     //
@@ -304,19 +306,6 @@ USBSTOR_ClassRequest(
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
-    //
-    // allocate 1-byte buffer
-    //
-    Buffer = (PUCHAR)AllocateItem(NonPagedPool, sizeof(UCHAR));
-    if (!Buffer)
-    {
-        //
-        // no memory
-        //
-        FreeItem(Buffer);
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
     //
     // initialize vendor request
     //
@@ -373,19 +362,34 @@ USBSTOR_GetMaxLUN(
 
     DPRINT("MaxLUN: %x\n", *Buffer);
 
-    if (*Buffer > 0xF)
+    if (NT_SUCCESS(Status))
     {
-        //
-        // invalid response documented in usb mass storage specification
-        //
-        Status = STATUS_DEVICE_DATA_ERROR;
+        if (*Buffer > 0xF)
+        {
+            //
+            // invalid response documented in usb mass storage specification
+            //
+            Status = STATUS_DEVICE_DATA_ERROR;
+        }
+        else
+        {
+            //
+            // store maxlun
+            //
+            DeviceExtension->MaxLUN = *Buffer;
+        }
     }
     else
     {
         //
-        // store maxlun
+        // "USB Mass Storage Class. Bulk-Only Transport. Revision 1.0"
+        // 3.2  Get Max LUN (class-specific request) :
+        // Devices that do not support multiple LUNs may STALL this command.
         //
-        DeviceExtension->MaxLUN = *Buffer;
+        USBSTOR_ResetDevice(DeviceExtension->LowerDeviceObject, DeviceExtension);
+
+        DeviceExtension->MaxLUN = 0;
+        Status = STATUS_SUCCESS;
     }
 
     //
@@ -411,7 +415,6 @@ USBSTOR_ResetDevice(
     // execute request
     //
     Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_RESET_DEVICE, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_OUT, 0, NULL);
-    DPRINT1("Status %x\n", Status);
 
     //
     // done
@@ -419,3 +422,99 @@ USBSTOR_ResetDevice(
     return Status;
 
 }
+
+BOOLEAN
+USBSTOR_IsFloppy(
+    IN PUCHAR Buffer,
+    IN ULONG BufferLength,
+    OUT PUCHAR MediumTypeCode)
+{
+    PUFI_CAPACITY_FORMAT_HEADER FormatHeader;
+    PUFI_CAPACITY_DESCRIPTOR Descriptor;
+    ULONG Length, Index, BlockCount, BlockLength;
+
+    //
+    // get format header
+    //
+    FormatHeader = (PUFI_CAPACITY_FORMAT_HEADER)Buffer;
+
+    //
+    // sanity checks
+    //
+    ASSERT(FormatHeader->Reserved1 == 0x00);
+    ASSERT(FormatHeader->Reserved2 == 0x00);
+    ASSERT(FormatHeader->Reserved3 == 0x00);
+
+    //
+    // is there capacity data
+    //
+    if (!FormatHeader->CapacityLength)
+    {
+        //
+        // no data provided
+        //
+        DPRINT1("[USBSTOR] No capacity length\n");
+        return FALSE;
+    }
+
+    //
+    // the format header are always 8 bytes in length
+    //
+    ASSERT((FormatHeader->CapacityLength & 0x7) == 0);
+    DPRINT1("CapacityLength %x\n", FormatHeader->CapacityLength);
+
+    //
+    // grab length and locate first descriptor
+    //
+    Length = FormatHeader->CapacityLength;
+    Descriptor = (PUFI_CAPACITY_DESCRIPTOR)(FormatHeader + 1);
+    for(Index = 0; Index < Length / sizeof(UFI_CAPACITY_DESCRIPTOR); Index++)
+    {
+        //
+        // blocks are little endian format
+        //
+        BlockCount = NTOHL(Descriptor->BlockCount);
+
+        //
+        // get block length
+        //
+        BlockLength = NTOHL((Descriptor->BlockLengthByte0 << 24 | Descriptor->BlockLengthByte1 << 16 | Descriptor->BlockLengthByte2 << 8));
+
+        DPRINT1("BlockCount %x BlockLength %x Code %x\n", BlockCount, BlockLength, Descriptor->Code);
+
+        if (BlockLength == 512 && BlockCount == 1440)
+        {
+            //
+            // 720 KB DD
+            //
+            *MediumTypeCode = 0x1E;
+            return TRUE;
+        }
+        else if (BlockLength == 1024 && BlockCount == 1232)
+        {
+            //
+            // 1,25 MB
+            //
+            *MediumTypeCode = 0x93;
+            return TRUE;
+        }
+        else if (BlockLength == 512 && BlockCount == 2880)
+        {
+            //
+            // 1,44MB KB DD
+            //
+            *MediumTypeCode = 0x94;
+            return TRUE;
+        }
+
+        //
+        // move to next descriptor
+        //
+        Descriptor = (Descriptor + 1);
+    }
+
+    //
+    // no floppy detected
+    //
+    return FALSE;
+}