// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename       : Structs.cs
// Author(s)      : Natalia Portillo <claunia@claunia.com>
//
// Component      : Windows direct device access.
//
// --[ Description ] ----------------------------------------------------------
//
//     Contains structures necessary for directly interfacing devices under
//     Windows.
//
// --[ License ] --------------------------------------------------------------
//
//     This library is free software; you can redistribute it and/or modify
//     it under the terms of the GNU Lesser General Public License as
//     published by the Free Software Foundation; either version 2.1 of the
//     License, or (at your option) any later version.
//
//     This library is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//     Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General Public
//     License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2017 Natalia Portillo
// ****************************************************************************/

using System;
using System.Runtime.InteropServices;

namespace DiscImageChef.Devices.Windows
{
    [StructLayout(LayoutKind.Sequential)]
    struct ScsiPassThroughDirect
    {
        public ushort Length;
        public byte ScsiStatus;
        public byte PathId;
        public byte TargetId;
        public byte Lun;
        public byte CdbLength;
        public byte SenseInfoLength;
        [MarshalAs(UnmanagedType.U1)]
        public ScsiIoctlDirection DataIn;
        public uint DataTransferLength;
        public uint TimeOutValue;
        public IntPtr DataBuffer;
        public uint SenseInfoOffset;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] Cdb;
    };

    [StructLayout(LayoutKind.Sequential)]
    struct ScsiPassThroughDirectAndSenseBuffer
    {
        public ScsiPassThroughDirect sptd;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] SenseBuf;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct AtaPassThroughDirect
    {
        /// <summary>
        /// Length in bytes of this structure
        /// </summary>
        public ushort Length;
        /// <summary>
        /// Indicates transfer direction and kind of operation
        /// </summary>
        public AtaFlags AtaFlags;
        /// <summary>
        /// Indicates IDE port or bus, set by driver
        /// </summary>
        public byte PathId;
        /// <summary>
        /// Indicates target device on bus, set by driver
        /// </summary>
        public byte TargetId;
        /// <summary>
        /// Indicates logical unit number of device, set by driver
        /// </summary>
        public byte Lun;
        /// <summary>
        /// Reserved
        /// </summary>
        public byte ReservedAsUchar;
        /// <summary>
        /// Data transfer length in bytes
        /// </summary>
        public uint DataTransferLength;
        /// <summary>
        /// Timeout value in seconds
        /// </summary>
        public uint TimeOutValue;
        /// <summary>
        /// Reserved
        /// </summary>
        public uint ReservedAsUlong;
        /// <summary>
        /// Pointer to data buffer
        /// </summary>
        public IntPtr DataBuffer;
        /// <summary>
        /// Previous ATA registers, for LBA48
        /// </summary>
        public AtaTaskFile PreviousTaskFile;
        /// <summary>
        /// ATA registers
        /// </summary>
        public AtaTaskFile CurrentTaskFile;
    }

    [StructLayout(LayoutKind.Explicit)]
    struct AtaTaskFile
    {
        // Fields for commands sent
        [FieldOffset(0)]
        public byte Features;
        [FieldOffset(6)]
        public byte Command;

        // Fields on command return
        [FieldOffset(0)]
        public byte Error;
        [FieldOffset(6)]
        public byte Status;

        // Common fields
        [FieldOffset(1)]
        public byte SectorCount;
        [FieldOffset(2)]
        public byte SectorNumber;
        [FieldOffset(3)]
        public byte CylinderLow;
        [FieldOffset(4)]
        public byte CylinderHigh;
        [FieldOffset(5)]
        public byte DeviceHead;
        [FieldOffset(7)]
        public byte Reserved;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct StoragePropertyQuery
    {
        [MarshalAs(UnmanagedType.U4)]
        public StoragePropertyId PropertyId;
        [MarshalAs(UnmanagedType.U4)]
        public StorageQueryType QueryType;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
        public byte[] AdditionalParameters;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct StorageDescriptorHeader
    {
        public uint Version;
        public uint Size;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct StorageDeviceDescriptor
    {
        public uint Version;
        public uint Size;
        public byte DeviceType;
        public byte DeviceTypeModifier;
        [MarshalAs(UnmanagedType.U1)]
        public bool RemovableMedia;
        [MarshalAs(UnmanagedType.U1)]
        public bool CommandQueueing;
        public uint VendorIdOffset;
        public uint ProductIdOffset;
        public uint ProductRevisionOffset;
        public uint SerialNumberOffset;
        public StorageBusType BusType;
        public uint RawPropertiesLength;
        public byte[] RawDeviceProperties;
    }
}

