mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
REFACTOR: Invert 'if' statement to reduce nesting.
This commit is contained in:
@@ -173,8 +173,11 @@ namespace DiscImageChef.Devices
|
||||
}
|
||||
}
|
||||
|
||||
if((command == (MmcCommands)SecureDigitalCommands.SendOperatingCondition ||
|
||||
command == MmcCommands.SendOpCond) && cachedOcr != null)
|
||||
if((command != (MmcCommands)SecureDigitalCommands.SendOperatingCondition &&
|
||||
command != MmcCommands.SendOpCond) || cachedOcr == null)
|
||||
return Command.SendMmcCommand(platformId, fd, command, write, isApplication, flags, argument, blockSize,
|
||||
blocks, ref buffer, out response, out duration, out sense, timeout);
|
||||
|
||||
{
|
||||
System.DateTime start = System.DateTime.Now;
|
||||
buffer = new byte[cachedOcr.Length];
|
||||
@@ -185,9 +188,6 @@ namespace DiscImageChef.Devices
|
||||
duration = (end - start).TotalMilliseconds;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Command.SendMmcCommand(platformId, fd, command, write, isApplication, flags, argument, blockSize,
|
||||
blocks, ref buffer, out response, out duration, out sense, timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -383,59 +383,58 @@ namespace DiscImageChef.Devices
|
||||
while(resolvedLink.Contains("usb"))
|
||||
{
|
||||
resolvedLink = System.IO.Path.GetDirectoryName(resolvedLink);
|
||||
if(System.IO.File.Exists(resolvedLink + "/descriptors") &&
|
||||
System.IO.File.Exists(resolvedLink + "/idProduct") &&
|
||||
System.IO.File.Exists(resolvedLink + "/idVendor"))
|
||||
if(!System.IO.File.Exists(resolvedLink + "/descriptors") ||
|
||||
!System.IO.File.Exists(resolvedLink + "/idProduct") ||
|
||||
!System.IO.File.Exists(resolvedLink + "/idVendor")) continue;
|
||||
|
||||
System.IO.FileStream usbFs;
|
||||
System.IO.StreamReader usbSr;
|
||||
string usbTemp;
|
||||
|
||||
usbFs = new System.IO.FileStream(resolvedLink + "/descriptors",
|
||||
System.IO.FileMode.Open,
|
||||
System.IO.FileAccess.Read);
|
||||
byte[] usbBuf = new byte[65536];
|
||||
int usbCount = usbFs.Read(usbBuf, 0, 65536);
|
||||
usbDescriptors = new byte[usbCount];
|
||||
Array.Copy(usbBuf, 0, usbDescriptors, 0, usbCount);
|
||||
usbFs.Close();
|
||||
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/idProduct");
|
||||
usbTemp = usbSr.ReadToEnd();
|
||||
ushort.TryParse(usbTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out usbProduct);
|
||||
usbSr.Close();
|
||||
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/idVendor");
|
||||
usbTemp = usbSr.ReadToEnd();
|
||||
ushort.TryParse(usbTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out usbVendor);
|
||||
usbSr.Close();
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/manufacturer"))
|
||||
{
|
||||
System.IO.FileStream usbFs;
|
||||
System.IO.StreamReader usbSr;
|
||||
string usbTemp;
|
||||
|
||||
usbFs = new System.IO.FileStream(resolvedLink + "/descriptors",
|
||||
System.IO.FileMode.Open,
|
||||
System.IO.FileAccess.Read);
|
||||
byte[] usbBuf = new byte[65536];
|
||||
int usbCount = usbFs.Read(usbBuf, 0, 65536);
|
||||
usbDescriptors = new byte[usbCount];
|
||||
Array.Copy(usbBuf, 0, usbDescriptors, 0, usbCount);
|
||||
usbFs.Close();
|
||||
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/idProduct");
|
||||
usbTemp = usbSr.ReadToEnd();
|
||||
ushort.TryParse(usbTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out usbProduct);
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/manufacturer");
|
||||
usbManufacturerString = usbSr.ReadToEnd().Trim();
|
||||
usbSr.Close();
|
||||
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/idVendor");
|
||||
usbTemp = usbSr.ReadToEnd();
|
||||
ushort.TryParse(usbTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out usbVendor);
|
||||
usbSr.Close();
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/manufacturer"))
|
||||
{
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/manufacturer");
|
||||
usbManufacturerString = usbSr.ReadToEnd().Trim();
|
||||
usbSr.Close();
|
||||
}
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/product"))
|
||||
{
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/product");
|
||||
usbProductString = usbSr.ReadToEnd().Trim();
|
||||
usbSr.Close();
|
||||
}
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/serial"))
|
||||
{
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/serial");
|
||||
usbSerialString = usbSr.ReadToEnd().Trim();
|
||||
usbSr.Close();
|
||||
}
|
||||
|
||||
usb = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/product"))
|
||||
{
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/product");
|
||||
usbProductString = usbSr.ReadToEnd().Trim();
|
||||
usbSr.Close();
|
||||
}
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/serial"))
|
||||
{
|
||||
usbSr = new System.IO.StreamReader(resolvedLink + "/serial");
|
||||
usbSerialString = usbSr.ReadToEnd().Trim();
|
||||
usbSr.Close();
|
||||
}
|
||||
|
||||
usb = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -487,49 +486,48 @@ namespace DiscImageChef.Devices
|
||||
while(resolvedLink.Contains("firewire"))
|
||||
{
|
||||
resolvedLink = System.IO.Path.GetDirectoryName(resolvedLink);
|
||||
if(System.IO.File.Exists(resolvedLink + "/model") &&
|
||||
System.IO.File.Exists(resolvedLink + "/vendor") &&
|
||||
System.IO.File.Exists(resolvedLink + "/guid"))
|
||||
if(!System.IO.File.Exists(resolvedLink + "/model") ||
|
||||
!System.IO.File.Exists(resolvedLink + "/vendor") ||
|
||||
!System.IO.File.Exists(resolvedLink + "/guid")) continue;
|
||||
|
||||
System.IO.StreamReader fwSr;
|
||||
string fwTemp;
|
||||
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/model");
|
||||
fwTemp = fwSr.ReadToEnd();
|
||||
uint.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out firewireModel);
|
||||
fwSr.Close();
|
||||
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/vendor");
|
||||
fwTemp = fwSr.ReadToEnd();
|
||||
uint.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
out firewireVendor);
|
||||
fwSr.Close();
|
||||
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/guid");
|
||||
fwTemp = fwSr.ReadToEnd();
|
||||
ulong.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out firewireGuid);
|
||||
fwSr.Close();
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/model_name"))
|
||||
{
|
||||
System.IO.StreamReader fwSr;
|
||||
string fwTemp;
|
||||
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/model");
|
||||
fwTemp = fwSr.ReadToEnd();
|
||||
uint.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out firewireModel);
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/model_name");
|
||||
firewireModelName = fwSr.ReadToEnd().Trim();
|
||||
fwSr.Close();
|
||||
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/vendor");
|
||||
fwTemp = fwSr.ReadToEnd();
|
||||
uint.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
out firewireVendor);
|
||||
fwSr.Close();
|
||||
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/guid");
|
||||
fwTemp = fwSr.ReadToEnd();
|
||||
ulong.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out firewireGuid);
|
||||
fwSr.Close();
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/model_name"))
|
||||
{
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/model_name");
|
||||
firewireModelName = fwSr.ReadToEnd().Trim();
|
||||
fwSr.Close();
|
||||
}
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/vendor_name"))
|
||||
{
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/vendor_name");
|
||||
firewireVendorName = fwSr.ReadToEnd().Trim();
|
||||
fwSr.Close();
|
||||
}
|
||||
|
||||
firewire = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if(System.IO.File.Exists(resolvedLink + "/vendor_name"))
|
||||
{
|
||||
fwSr = new System.IO.StreamReader(resolvedLink + "/vendor_name");
|
||||
firewireVendorName = fwSr.ReadToEnd().Trim();
|
||||
fwSr.Close();
|
||||
}
|
||||
|
||||
firewire = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -554,36 +552,33 @@ namespace DiscImageChef.Devices
|
||||
while(resolvedLink.Contains("/sys/devices"))
|
||||
{
|
||||
resolvedLink = System.IO.Path.GetDirectoryName(resolvedLink);
|
||||
if(System.IO.Directory.Exists(resolvedLink + "/pcmcia_socket"))
|
||||
{
|
||||
string[] subdirs =
|
||||
System.IO.Directory.GetDirectories(resolvedLink + "/pcmcia_socket",
|
||||
"pcmcia_socket*",
|
||||
System.IO.SearchOption.TopDirectoryOnly);
|
||||
if(!System.IO.Directory.Exists(resolvedLink + "/pcmcia_socket")) continue;
|
||||
|
||||
if(subdirs.Length > 0)
|
||||
{
|
||||
string possibleDir =
|
||||
System.IO.Path.Combine(resolvedLink, "pcmcia_socket", subdirs[0]);
|
||||
if(System.IO.File.Exists(possibleDir + "/card_type") &&
|
||||
System.IO.File.Exists(possibleDir + "/cis"))
|
||||
{
|
||||
System.IO.FileStream cisFs;
|
||||
string[] subdirs =
|
||||
System.IO.Directory.GetDirectories(resolvedLink + "/pcmcia_socket",
|
||||
"pcmcia_socket*",
|
||||
System.IO.SearchOption.TopDirectoryOnly);
|
||||
|
||||
cisFs = new System.IO.FileStream(possibleDir + "/cis",
|
||||
System.IO.FileMode.Open,
|
||||
System.IO.FileAccess.Read);
|
||||
byte[] cisBuf = new byte[65536];
|
||||
int cisCount = cisFs.Read(cisBuf, 0, 65536);
|
||||
cis = new byte[cisCount];
|
||||
Array.Copy(cisBuf, 0, cis, 0, cisCount);
|
||||
cisFs.Close();
|
||||
if(subdirs.Length <= 0) continue;
|
||||
|
||||
pcmcia = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
string possibleDir =
|
||||
System.IO.Path.Combine(resolvedLink, "pcmcia_socket", subdirs[0]);
|
||||
if(!System.IO.File.Exists(possibleDir + "/card_type") ||
|
||||
!System.IO.File.Exists(possibleDir + "/cis")) continue;
|
||||
|
||||
System.IO.FileStream cisFs;
|
||||
|
||||
cisFs = new System.IO.FileStream(possibleDir + "/cis",
|
||||
System.IO.FileMode.Open,
|
||||
System.IO.FileAccess.Read);
|
||||
byte[] cisBuf = new byte[65536];
|
||||
int cisCount = cisFs.Read(cisBuf, 0, 65536);
|
||||
cis = new byte[cisCount];
|
||||
Array.Copy(cisBuf, 0, cis, 0, cisCount);
|
||||
cisFs.Close();
|
||||
|
||||
pcmcia = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -678,13 +673,12 @@ namespace DiscImageChef.Devices
|
||||
else foreach(char c in serial) if(char.IsControl(c)) serial = usbSerialString;
|
||||
}
|
||||
|
||||
if(firewire)
|
||||
{
|
||||
if(string.IsNullOrEmpty(manufacturer)) manufacturer = firewireVendorName;
|
||||
if(string.IsNullOrEmpty(model)) model = firewireModelName;
|
||||
if(string.IsNullOrEmpty(serial)) serial = string.Format("{0:X16}", firewireGuid);
|
||||
else foreach(char c in serial) if(char.IsControl(c)) serial = string.Format("{0:X16}", firewireGuid);
|
||||
}
|
||||
if(!firewire) return;
|
||||
|
||||
if(string.IsNullOrEmpty(manufacturer)) manufacturer = firewireVendorName;
|
||||
if(string.IsNullOrEmpty(model)) model = firewireModelName;
|
||||
if(string.IsNullOrEmpty(serial)) serial = string.Format("{0:X16}", firewireGuid);
|
||||
else foreach(char c in serial) if(char.IsControl(c)) serial = string.Format("{0:X16}", firewireGuid);
|
||||
}
|
||||
|
||||
static int ConvertFromHexAscii(string file, out byte[] outBuf)
|
||||
|
||||
@@ -43,19 +43,20 @@ namespace DiscImageChef.Devices
|
||||
/// </summary>
|
||||
~Device()
|
||||
{
|
||||
if(fd != null)
|
||||
switch(platformId)
|
||||
{
|
||||
case Interop.PlatformID.Win32NT:
|
||||
Windows.Extern.CloseHandle((SafeFileHandle)fd);
|
||||
break;
|
||||
case Interop.PlatformID.Linux:
|
||||
Linux.Extern.close((int)fd);
|
||||
break;
|
||||
case Interop.PlatformID.FreeBSD:
|
||||
FreeBSD.Extern.cam_close_device((IntPtr)fd);
|
||||
break;
|
||||
}
|
||||
if(fd == null) return;
|
||||
|
||||
switch(platformId)
|
||||
{
|
||||
case Interop.PlatformID.Win32NT:
|
||||
Windows.Extern.CloseHandle((SafeFileHandle)fd);
|
||||
break;
|
||||
case Interop.PlatformID.Linux:
|
||||
Linux.Extern.close((int)fd);
|
||||
break;
|
||||
case Interop.PlatformID.FreeBSD:
|
||||
FreeBSD.Extern.cam_close_device((IntPtr)fd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -233,13 +233,12 @@ namespace DiscImageChef.Devices
|
||||
|
||||
DicConsole.DebugWriteLine("SCSI Device", "PLEXTOR POWEREC GET SPEEDS took {0} ms.", duration);
|
||||
|
||||
if(!sense && !error)
|
||||
{
|
||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||
selected = BigEndianBitConverter.ToUInt16(buf, 4);
|
||||
max = BigEndianBitConverter.ToUInt16(buf, 6);
|
||||
last = BigEndianBitConverter.ToUInt16(buf, 8);
|
||||
}
|
||||
if(sense || error) return sense;
|
||||
|
||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||
selected = BigEndianBitConverter.ToUInt16(buf, 4);
|
||||
max = BigEndianBitConverter.ToUInt16(buf, 6);
|
||||
last = BigEndianBitConverter.ToUInt16(buf, 8);
|
||||
|
||||
return sense;
|
||||
}
|
||||
@@ -274,12 +273,11 @@ namespace DiscImageChef.Devices
|
||||
|
||||
DicConsole.DebugWriteLine("SCSI Device", "PLEXTOR POWEREC GET SPEEDS took {0} ms.", duration);
|
||||
|
||||
if(!sense && !error)
|
||||
{
|
||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||
enabled = buf[2] != 0;
|
||||
speed = BigEndianBitConverter.ToUInt16(buf, 4);
|
||||
}
|
||||
if(sense || error) return sense;
|
||||
|
||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||
enabled = buf[2] != 0;
|
||||
speed = BigEndianBitConverter.ToUInt16(buf, 4);
|
||||
|
||||
return sense;
|
||||
}
|
||||
|
||||
@@ -494,57 +494,57 @@ namespace DiscImageChef.Devices.Windows
|
||||
// We start at the "root" of the device tree and look for all
|
||||
// devices that match the interface GUID of a Hub Controller
|
||||
IntPtr h = SetupDiGetClassDevs(ref hostGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE)
|
||||
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbController>(hostList);
|
||||
|
||||
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
|
||||
bool success;
|
||||
int i = 0;
|
||||
do
|
||||
{
|
||||
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
|
||||
bool success;
|
||||
int i = 0;
|
||||
do
|
||||
UsbController host = new UsbController();
|
||||
host.ControllerIndex = i;
|
||||
|
||||
// create a Device Interface Data structure
|
||||
SpDeviceInterfaceData dia = new SpDeviceInterfaceData();
|
||||
dia.cbSize = Marshal.SizeOf(dia);
|
||||
|
||||
// start the enumeration
|
||||
success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref hostGuid, i, ref dia);
|
||||
if(success)
|
||||
{
|
||||
UsbController host = new UsbController();
|
||||
host.ControllerIndex = i;
|
||||
// build a DevInfo Data structure
|
||||
SpDevinfoData da = new SpDevinfoData();
|
||||
da.cbSize = Marshal.SizeOf(da);
|
||||
|
||||
// create a Device Interface Data structure
|
||||
SpDeviceInterfaceData dia = new SpDeviceInterfaceData();
|
||||
dia.cbSize = Marshal.SizeOf(dia);
|
||||
// build a Device Interface Detail Data structure
|
||||
SpDeviceInterfaceDetailData didd = new SpDeviceInterfaceDetailData();
|
||||
didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)
|
||||
|
||||
// start the enumeration
|
||||
success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref hostGuid, i, ref dia);
|
||||
if(success)
|
||||
// now we can get some more detailed information
|
||||
int nRequiredSize = 0;
|
||||
int nBytes = BUFFER_SIZE;
|
||||
if(SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
|
||||
{
|
||||
// build a DevInfo Data structure
|
||||
SpDevinfoData da = new SpDevinfoData();
|
||||
da.cbSize = Marshal.SizeOf(da);
|
||||
host.ControllerDevicePath = didd.DevicePath;
|
||||
|
||||
// build a Device Interface Detail Data structure
|
||||
SpDeviceInterfaceDetailData didd = new SpDeviceInterfaceDetailData();
|
||||
didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)
|
||||
// get the Device Description and DriverKeyName
|
||||
int requiredSize = 0;
|
||||
int regType = REG_SZ;
|
||||
|
||||
// now we can get some more detailed information
|
||||
int nRequiredSize = 0;
|
||||
int nBytes = BUFFER_SIZE;
|
||||
if(SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
|
||||
{
|
||||
host.ControllerDevicePath = didd.DevicePath;
|
||||
|
||||
// get the Device Description and DriverKeyName
|
||||
int requiredSize = 0;
|
||||
int regType = REG_SZ;
|
||||
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
|
||||
BUFFER_SIZE, ref requiredSize)) host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf,
|
||||
BUFFER_SIZE, ref requiredSize)) host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
}
|
||||
hostList.Add(host);
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
|
||||
BUFFER_SIZE, ref requiredSize)) host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf,
|
||||
BUFFER_SIZE, ref requiredSize)) host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
}
|
||||
i++;
|
||||
hostList.Add(host);
|
||||
}
|
||||
while(success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
i++;
|
||||
}
|
||||
while(success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
|
||||
// convert it into a Collection
|
||||
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbController>(hostList);
|
||||
@@ -602,50 +602,49 @@ namespace DiscImageChef.Devices.Windows
|
||||
// Open a handle to the Host Controller
|
||||
h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
int nBytesReturned;
|
||||
UsbRootHubName hubName = new UsbRootHubName();
|
||||
int nBytes = Marshal.SizeOf(hubName);
|
||||
IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes);
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE) return root;
|
||||
|
||||
// get the Hub Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes,
|
||||
int nBytesReturned;
|
||||
UsbRootHubName hubName = new UsbRootHubName();
|
||||
int nBytes = Marshal.SizeOf(hubName);
|
||||
IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes);
|
||||
|
||||
// get the Hub Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
hubName = (UsbRootHubName)Marshal.PtrToStructure(ptrHubName, typeof(UsbRootHubName));
|
||||
root.HubDevicePath = @"\\.\" + hubName.RootHubName;
|
||||
}
|
||||
|
||||
// TODO: Get DriverKeyName for Root Hub
|
||||
|
||||
// Now let's open the Hub (based upon the HubName we got above)
|
||||
h2 = CreateFile(root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
UsbNodeInformation nodeInfo = new UsbNodeInformation();
|
||||
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
|
||||
nBytes = Marshal.SizeOf(nodeInfo);
|
||||
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
|
||||
|
||||
// get the Hub Information
|
||||
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
hubName = (UsbRootHubName)Marshal.PtrToStructure(ptrHubName, typeof(UsbRootHubName));
|
||||
root.HubDevicePath = @"\\.\" + hubName.RootHubName;
|
||||
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
|
||||
typeof(UsbNodeInformation));
|
||||
root.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
|
||||
root.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
|
||||
}
|
||||
|
||||
// TODO: Get DriverKeyName for Root Hub
|
||||
|
||||
// Now let's open the Hub (based upon the HubName we got above)
|
||||
h2 = CreateFile(root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
UsbNodeInformation nodeInfo = new UsbNodeInformation();
|
||||
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
|
||||
nBytes = Marshal.SizeOf(nodeInfo);
|
||||
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
|
||||
|
||||
// get the Hub Information
|
||||
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
|
||||
typeof(UsbNodeInformation));
|
||||
root.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
|
||||
root.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrNodeInfo);
|
||||
CloseHandle(h2);
|
||||
}
|
||||
|
||||
Marshal.FreeHGlobal(ptrHubName);
|
||||
CloseHandle(h);
|
||||
Marshal.FreeHGlobal(ptrNodeInfo);
|
||||
CloseHandle(h2);
|
||||
}
|
||||
|
||||
Marshal.FreeHGlobal(ptrHubName);
|
||||
CloseHandle(h);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
@@ -740,50 +739,49 @@ namespace DiscImageChef.Devices.Windows
|
||||
// Open a handle to the Hub device
|
||||
IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE)
|
||||
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbPort>(portList);
|
||||
|
||||
int nBytes = Marshal.SizeOf(typeof(UsbNodeConnectionInformationEx));
|
||||
IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);
|
||||
|
||||
// loop thru all of the ports on the hub
|
||||
// BTW: Ports are numbered starting at 1
|
||||
for(int i = 1; i <= HubPortCount; i++)
|
||||
{
|
||||
int nBytes = Marshal.SizeOf(typeof(UsbNodeConnectionInformationEx));
|
||||
IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);
|
||||
int nBytesReturned;
|
||||
UsbNodeConnectionInformationEx nodeConnection = new UsbNodeConnectionInformationEx();
|
||||
nodeConnection.ConnectionIndex = i;
|
||||
Marshal.StructureToPtr(nodeConnection, ptrNodeConnection, true);
|
||||
|
||||
// loop thru all of the ports on the hub
|
||||
// BTW: Ports are numbered starting at 1
|
||||
for(int i = 1; i <= HubPortCount; i++)
|
||||
{
|
||||
int nBytesReturned;
|
||||
UsbNodeConnectionInformationEx nodeConnection = new UsbNodeConnectionInformationEx();
|
||||
nodeConnection.ConnectionIndex = i;
|
||||
Marshal.StructureToPtr(nodeConnection, ptrNodeConnection, true);
|
||||
if(!DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes,
|
||||
ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero)) continue;
|
||||
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes,
|
||||
ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
nodeConnection =
|
||||
(UsbNodeConnectionInformationEx)Marshal.PtrToStructure(ptrNodeConnection,
|
||||
typeof(
|
||||
UsbNodeConnectionInformationEx
|
||||
));
|
||||
nodeConnection =
|
||||
(UsbNodeConnectionInformationEx)Marshal.PtrToStructure(ptrNodeConnection,
|
||||
typeof(
|
||||
UsbNodeConnectionInformationEx
|
||||
));
|
||||
|
||||
// load up the USBPort class
|
||||
UsbPort port = new UsbPort();
|
||||
port.PortPortNumber = i;
|
||||
port.PortHubDevicePath = HubDevicePath;
|
||||
UsbConnectionStatus status = (UsbConnectionStatus)nodeConnection.ConnectionStatus;
|
||||
port.PortStatus = status.ToString();
|
||||
UsbDeviceSpeed speed = (UsbDeviceSpeed)nodeConnection.Speed;
|
||||
port.PortSpeed = speed.ToString();
|
||||
port.PortIsDeviceConnected =
|
||||
nodeConnection.ConnectionStatus == (int)UsbConnectionStatus.DeviceConnected;
|
||||
port.PortIsHub = Convert.ToBoolean(nodeConnection.DeviceIsHub);
|
||||
port.PortDeviceDescriptor = nodeConnection.DeviceDescriptor;
|
||||
// load up the USBPort class
|
||||
UsbPort port = new UsbPort();
|
||||
port.PortPortNumber = i;
|
||||
port.PortHubDevicePath = HubDevicePath;
|
||||
UsbConnectionStatus status = (UsbConnectionStatus)nodeConnection.ConnectionStatus;
|
||||
port.PortStatus = status.ToString();
|
||||
UsbDeviceSpeed speed = (UsbDeviceSpeed)nodeConnection.Speed;
|
||||
port.PortSpeed = speed.ToString();
|
||||
port.PortIsDeviceConnected =
|
||||
nodeConnection.ConnectionStatus == (int)UsbConnectionStatus.DeviceConnected;
|
||||
port.PortIsHub = Convert.ToBoolean(nodeConnection.DeviceIsHub);
|
||||
port.PortDeviceDescriptor = nodeConnection.DeviceDescriptor;
|
||||
|
||||
// add it to the list
|
||||
portList.Add(port);
|
||||
}
|
||||
}
|
||||
|
||||
Marshal.FreeHGlobal(ptrNodeConnection);
|
||||
CloseHandle(h);
|
||||
// add it to the list
|
||||
portList.Add(port);
|
||||
}
|
||||
|
||||
Marshal.FreeHGlobal(ptrNodeConnection);
|
||||
CloseHandle(h);
|
||||
// convert it into a Collection
|
||||
return new System.Collections.ObjectModel.ReadOnlyCollection<UsbPort>(portList);
|
||||
}
|
||||
@@ -862,143 +860,142 @@ namespace DiscImageChef.Devices.Windows
|
||||
// Open a handle to the Hub device
|
||||
IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE) return device;
|
||||
|
||||
int nBytesReturned;
|
||||
int nBytes = BUFFER_SIZE;
|
||||
// We use this to zero fill a buffer
|
||||
string nullString = new string((char)0, BUFFER_SIZE / Marshal.SystemDefaultCharSize);
|
||||
|
||||
// The iManufacturer, iProduct and iSerialNumber entries in the
|
||||
// Device Descriptor are really just indexes. So, we have to
|
||||
// request a String Descriptor to get the values for those strings.
|
||||
|
||||
if(PortDeviceDescriptor.iManufacturer > 0)
|
||||
{
|
||||
int nBytesReturned;
|
||||
int nBytes = BUFFER_SIZE;
|
||||
// We use this to zero fill a buffer
|
||||
string nullString = new string((char)0, BUFFER_SIZE / Marshal.SystemDefaultCharSize);
|
||||
|
||||
// The iManufacturer, iProduct and iSerialNumber entries in the
|
||||
// Device Descriptor are really just indexes. So, we have to
|
||||
// request a String Descriptor to get the values for those strings.
|
||||
|
||||
if(PortDeviceDescriptor.iManufacturer > 0)
|
||||
{
|
||||
// build a request for string descriptor
|
||||
UsbDescriptorRequest request = new UsbDescriptorRequest();
|
||||
request.ConnectionIndex = PortPortNumber;
|
||||
request.SetupPacket.wValue =
|
||||
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iManufacturer);
|
||||
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
|
||||
request.SetupPacket.wIndex = 0x409; // Language Code
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(request, ptrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// The location of the string descriptor is immediately after
|
||||
// the Request structure. Because this location is not "covered"
|
||||
// by the structure allocation, we're forced to zero out this
|
||||
// chunk of memory by using the StringToHGlobalAuto() hack above
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
|
||||
UsbStringDescriptor stringDesc =
|
||||
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(UsbStringDescriptor));
|
||||
device.DeviceManufacturer = stringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
}
|
||||
if(PortDeviceDescriptor.iProduct > 0)
|
||||
{
|
||||
// build a request for string descriptor
|
||||
UsbDescriptorRequest request = new UsbDescriptorRequest();
|
||||
request.ConnectionIndex = PortPortNumber;
|
||||
request.SetupPacket.wValue =
|
||||
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iProduct);
|
||||
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
|
||||
request.SetupPacket.wIndex = 0x409; // Language Code
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(request, ptrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// the location of the string descriptor is immediately after the Request structure
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
|
||||
UsbStringDescriptor stringDesc =
|
||||
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(UsbStringDescriptor));
|
||||
device.DeviceProduct = stringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
}
|
||||
if(PortDeviceDescriptor.iSerialNumber > 0)
|
||||
{
|
||||
// build a request for string descriptor
|
||||
UsbDescriptorRequest request = new UsbDescriptorRequest();
|
||||
request.ConnectionIndex = PortPortNumber;
|
||||
request.SetupPacket.wValue =
|
||||
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iSerialNumber);
|
||||
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
|
||||
request.SetupPacket.wIndex = 0x409; // Language Code
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(request, ptrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// the location of the string descriptor is immediately after the Request structure
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
|
||||
UsbStringDescriptor stringDesc =
|
||||
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(UsbStringDescriptor));
|
||||
device.DeviceSerialNumber = stringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
}
|
||||
|
||||
// build a request for configuration descriptor
|
||||
UsbDescriptorRequest dcrRequest = new UsbDescriptorRequest();
|
||||
dcrRequest.ConnectionIndex = PortPortNumber;
|
||||
dcrRequest.SetupPacket.wValue = (short)(USB_CONFIGURATION_DESCRIPTOR_TYPE << 8);
|
||||
dcrRequest.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(dcrRequest));
|
||||
dcrRequest.SetupPacket.wIndex = 0;
|
||||
// build a request for string descriptor
|
||||
UsbDescriptorRequest request = new UsbDescriptorRequest();
|
||||
request.ConnectionIndex = PortPortNumber;
|
||||
request.SetupPacket.wValue =
|
||||
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iManufacturer);
|
||||
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
|
||||
request.SetupPacket.wIndex = 0x409; // Language Code
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr dcrPtrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(dcrRequest, dcrPtrRequest, true);
|
||||
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(request, ptrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes,
|
||||
dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
IntPtr ptrStringDesc = new IntPtr(dcrPtrRequest.ToInt32() + Marshal.SizeOf(dcrRequest));
|
||||
device.BinaryDeviceDescriptors = new byte[nBytesReturned];
|
||||
Marshal.Copy(ptrStringDesc, device.BinaryDeviceDescriptors, 0, nBytesReturned);
|
||||
// The location of the string descriptor is immediately after
|
||||
// the Request structure. Because this location is not "covered"
|
||||
// by the structure allocation, we're forced to zero out this
|
||||
// chunk of memory by using the StringToHGlobalAuto() hack above
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
|
||||
UsbStringDescriptor stringDesc =
|
||||
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(UsbStringDescriptor));
|
||||
device.DeviceManufacturer = stringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(dcrPtrRequest);
|
||||
|
||||
// Get the Driver Key Name (usefull in locating a device)
|
||||
UsbNodeConnectionDriverkeyName driverKey = new UsbNodeConnectionDriverkeyName();
|
||||
driverKey.ConnectionIndex = PortPortNumber;
|
||||
nBytes = Marshal.SizeOf(driverKey);
|
||||
IntPtr ptrDriverKey = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(driverKey, ptrDriverKey, true);
|
||||
|
||||
// Use an IOCTL call to request the Driver Key Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ptrDriverKey, nBytes,
|
||||
ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
driverKey = (UsbNodeConnectionDriverkeyName)Marshal.PtrToStructure(ptrDriverKey,
|
||||
typeof(
|
||||
UsbNodeConnectionDriverkeyName
|
||||
));
|
||||
device.DeviceDriverKey = driverKey.DriverKeyName;
|
||||
|
||||
// use the DriverKeyName to get the Device Description and Instance ID
|
||||
device.DeviceName = GetDescriptionByKeyName(device.DeviceDriverKey);
|
||||
device.DeviceInstanceId = GetInstanceIdByKeyName(device.DeviceDriverKey);
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrDriverKey);
|
||||
CloseHandle(h);
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
}
|
||||
if(PortDeviceDescriptor.iProduct > 0)
|
||||
{
|
||||
// build a request for string descriptor
|
||||
UsbDescriptorRequest request = new UsbDescriptorRequest();
|
||||
request.ConnectionIndex = PortPortNumber;
|
||||
request.SetupPacket.wValue =
|
||||
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iProduct);
|
||||
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
|
||||
request.SetupPacket.wIndex = 0x409; // Language Code
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(request, ptrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// the location of the string descriptor is immediately after the Request structure
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
|
||||
UsbStringDescriptor stringDesc =
|
||||
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(UsbStringDescriptor));
|
||||
device.DeviceProduct = stringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
}
|
||||
if(PortDeviceDescriptor.iSerialNumber > 0)
|
||||
{
|
||||
// build a request for string descriptor
|
||||
UsbDescriptorRequest request = new UsbDescriptorRequest();
|
||||
request.ConnectionIndex = PortPortNumber;
|
||||
request.SetupPacket.wValue =
|
||||
(short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iSerialNumber);
|
||||
request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(request));
|
||||
request.SetupPacket.wIndex = 0x409; // Language Code
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(request, ptrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// the location of the string descriptor is immediately after the Request structure
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(request));
|
||||
UsbStringDescriptor stringDesc =
|
||||
(UsbStringDescriptor)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(UsbStringDescriptor));
|
||||
device.DeviceSerialNumber = stringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
}
|
||||
|
||||
// build a request for configuration descriptor
|
||||
UsbDescriptorRequest dcrRequest = new UsbDescriptorRequest();
|
||||
dcrRequest.ConnectionIndex = PortPortNumber;
|
||||
dcrRequest.SetupPacket.wValue = (short)(USB_CONFIGURATION_DESCRIPTOR_TYPE << 8);
|
||||
dcrRequest.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(dcrRequest));
|
||||
dcrRequest.SetupPacket.wIndex = 0;
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr dcrPtrRequest = Marshal.StringToHGlobalAuto(nullString);
|
||||
Marshal.StructureToPtr(dcrRequest, dcrPtrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes,
|
||||
dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
IntPtr ptrStringDesc = new IntPtr(dcrPtrRequest.ToInt32() + Marshal.SizeOf(dcrRequest));
|
||||
device.BinaryDeviceDescriptors = new byte[nBytesReturned];
|
||||
Marshal.Copy(ptrStringDesc, device.BinaryDeviceDescriptors, 0, nBytesReturned);
|
||||
}
|
||||
Marshal.FreeHGlobal(dcrPtrRequest);
|
||||
|
||||
// Get the Driver Key Name (usefull in locating a device)
|
||||
UsbNodeConnectionDriverkeyName driverKey = new UsbNodeConnectionDriverkeyName();
|
||||
driverKey.ConnectionIndex = PortPortNumber;
|
||||
nBytes = Marshal.SizeOf(driverKey);
|
||||
IntPtr ptrDriverKey = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(driverKey, ptrDriverKey, true);
|
||||
|
||||
// Use an IOCTL call to request the Driver Key Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ptrDriverKey, nBytes,
|
||||
ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
driverKey = (UsbNodeConnectionDriverkeyName)Marshal.PtrToStructure(ptrDriverKey,
|
||||
typeof(
|
||||
UsbNodeConnectionDriverkeyName
|
||||
));
|
||||
device.DeviceDriverKey = driverKey.DriverKeyName;
|
||||
|
||||
// use the DriverKeyName to get the Device Description and Instance ID
|
||||
device.DeviceName = GetDescriptionByKeyName(device.DeviceDriverKey);
|
||||
device.DeviceInstanceId = GetInstanceIdByKeyName(device.DeviceDriverKey);
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrDriverKey);
|
||||
CloseHandle(h);
|
||||
return device;
|
||||
}
|
||||
|
||||
@@ -1015,61 +1012,59 @@ namespace DiscImageChef.Devices.Windows
|
||||
// Open a handle to the Host Controller
|
||||
h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
// Get the DevicePath for downstream hub
|
||||
int nBytesReturned;
|
||||
UsbNodeConnectionName nodeName = new UsbNodeConnectionName();
|
||||
nodeName.ConnectionIndex = PortPortNumber;
|
||||
int nBytes = Marshal.SizeOf(nodeName);
|
||||
IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(nodeName, ptrNodeName, true);
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE) return hub;
|
||||
// Get the DevicePath for downstream hub
|
||||
int nBytesReturned;
|
||||
UsbNodeConnectionName nodeName = new UsbNodeConnectionName();
|
||||
nodeName.ConnectionIndex = PortPortNumber;
|
||||
int nBytes = Marshal.SizeOf(nodeName);
|
||||
IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(nodeName, ptrNodeName, true);
|
||||
|
||||
// Use an IOCTL call to request the Node Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes,
|
||||
// Use an IOCTL call to request the Node Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
nodeName = (UsbNodeConnectionName)Marshal.PtrToStructure(ptrNodeName,
|
||||
typeof(UsbNodeConnectionName));
|
||||
hub.HubDevicePath = @"\\.\" + nodeName.NodeName;
|
||||
}
|
||||
|
||||
// Now let's open the Hub (based upon the HubName we got above)
|
||||
h2 = CreateFile(hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
UsbNodeInformation nodeInfo = new UsbNodeInformation();
|
||||
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
|
||||
nBytes = Marshal.SizeOf(nodeInfo);
|
||||
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
|
||||
|
||||
// get the Hub Information
|
||||
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
nodeName = (UsbNodeConnectionName)Marshal.PtrToStructure(ptrNodeName,
|
||||
typeof(UsbNodeConnectionName));
|
||||
hub.HubDevicePath = @"\\.\" + nodeName.NodeName;
|
||||
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
|
||||
typeof(UsbNodeInformation));
|
||||
hub.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
|
||||
hub.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
|
||||
}
|
||||
|
||||
// Now let's open the Hub (based upon the HubName we got above)
|
||||
h2 = CreateFile(hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
UsbNodeInformation nodeInfo = new UsbNodeInformation();
|
||||
nodeInfo.NodeType = (int)UsbHubNode.UsbHub;
|
||||
nBytes = Marshal.SizeOf(nodeInfo);
|
||||
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
|
||||
Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true);
|
||||
|
||||
// get the Hub Information
|
||||
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo,
|
||||
typeof(UsbNodeInformation));
|
||||
hub.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered);
|
||||
hub.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrNodeInfo);
|
||||
CloseHandle(h2);
|
||||
}
|
||||
|
||||
// Fill in the missing Manufacture, Product, and SerialNumber values
|
||||
// values by just creating a Device instance and copying the values
|
||||
UsbDevice device = GetDevice();
|
||||
hub.HubInstanceId = device.DeviceInstanceId;
|
||||
hub.HubManufacturer = device.Manufacturer;
|
||||
hub.HubProduct = device.Product;
|
||||
hub.HubSerialNumber = device.SerialNumber;
|
||||
hub.HubDriverKey = device.DriverKey;
|
||||
|
||||
Marshal.FreeHGlobal(ptrNodeName);
|
||||
CloseHandle(h);
|
||||
Marshal.FreeHGlobal(ptrNodeInfo);
|
||||
CloseHandle(h2);
|
||||
}
|
||||
|
||||
// Fill in the missing Manufacture, Product, and SerialNumber values
|
||||
// values by just creating a Device instance and copying the values
|
||||
UsbDevice device = GetDevice();
|
||||
hub.HubInstanceId = device.DeviceInstanceId;
|
||||
hub.HubManufacturer = device.Manufacturer;
|
||||
hub.HubProduct = device.Product;
|
||||
hub.HubSerialNumber = device.SerialNumber;
|
||||
hub.HubDriverKey = device.DriverKey;
|
||||
|
||||
Marshal.FreeHGlobal(ptrNodeName);
|
||||
CloseHandle(h);
|
||||
return hub;
|
||||
}
|
||||
}
|
||||
@@ -1161,46 +1156,45 @@ namespace DiscImageChef.Devices.Windows
|
||||
// Use the "enumerator form" of the SetupDiGetClassDevs API
|
||||
// to generate a list of all USB devices
|
||||
IntPtr h = SetupDiGetClassDevs(0, devEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans;
|
||||
|
||||
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
|
||||
string keyName;
|
||||
|
||||
bool success;
|
||||
int i = 0;
|
||||
do
|
||||
{
|
||||
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
|
||||
string keyName;
|
||||
// create a Device Interface Data structure
|
||||
SpDevinfoData da = new SpDevinfoData();
|
||||
da.cbSize = Marshal.SizeOf(da);
|
||||
|
||||
bool success;
|
||||
int i = 0;
|
||||
do
|
||||
// start the enumeration
|
||||
success = SetupDiEnumDeviceInfo(h, i, ref da);
|
||||
if(success)
|
||||
{
|
||||
// create a Device Interface Data structure
|
||||
SpDevinfoData da = new SpDevinfoData();
|
||||
da.cbSize = Marshal.SizeOf(da);
|
||||
int requiredSize = 0;
|
||||
int regType = REG_SZ;
|
||||
keyName = "";
|
||||
|
||||
// start the enumeration
|
||||
success = SetupDiEnumDeviceInfo(h, i, ref da);
|
||||
if(success)
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
|
||||
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
|
||||
// is it a match?
|
||||
if(keyName == driverKeyName)
|
||||
{
|
||||
int requiredSize = 0;
|
||||
int regType = REG_SZ;
|
||||
keyName = "";
|
||||
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
|
||||
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
|
||||
// is it a match?
|
||||
if(keyName == driverKeyName)
|
||||
{
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
|
||||
BUFFER_SIZE, ref requiredSize)) ans = Marshal.PtrToStringAuto(ptrBuf);
|
||||
break;
|
||||
}
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
|
||||
BUFFER_SIZE, ref requiredSize)) ans = Marshal.PtrToStringAuto(ptrBuf);
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
while(success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
i++;
|
||||
}
|
||||
while(success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
|
||||
return ans;
|
||||
}
|
||||
@@ -1216,48 +1210,47 @@ namespace DiscImageChef.Devices.Windows
|
||||
// Use the "enumerator form" of the SetupDiGetClassDevs API
|
||||
// to generate a list of all USB devices
|
||||
IntPtr h = SetupDiGetClassDevs(0, devEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans;
|
||||
|
||||
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
|
||||
string keyName;
|
||||
|
||||
bool success;
|
||||
int i = 0;
|
||||
do
|
||||
{
|
||||
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
|
||||
string keyName;
|
||||
// create a Device Interface Data structure
|
||||
SpDevinfoData da = new SpDevinfoData();
|
||||
da.cbSize = Marshal.SizeOf(da);
|
||||
|
||||
bool success;
|
||||
int i = 0;
|
||||
do
|
||||
// start the enumeration
|
||||
success = SetupDiEnumDeviceInfo(h, i, ref da);
|
||||
if(success)
|
||||
{
|
||||
// create a Device Interface Data structure
|
||||
SpDevinfoData da = new SpDevinfoData();
|
||||
da.cbSize = Marshal.SizeOf(da);
|
||||
int requiredSize = 0;
|
||||
int regType = REG_SZ;
|
||||
|
||||
// start the enumeration
|
||||
success = SetupDiEnumDeviceInfo(h, i, ref da);
|
||||
if(success)
|
||||
keyName = "";
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
|
||||
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
|
||||
// is it a match?
|
||||
if(keyName == driverKeyName)
|
||||
{
|
||||
int requiredSize = 0;
|
||||
int regType = REG_SZ;
|
||||
|
||||
keyName = "";
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
|
||||
ref requiredSize)) keyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
|
||||
// is it a match?
|
||||
if(keyName == driverKeyName)
|
||||
{
|
||||
int nBytes = BUFFER_SIZE;
|
||||
StringBuilder sb = new StringBuilder(nBytes);
|
||||
SetupDiGetDeviceInstanceId(h, ref da, sb, nBytes, out requiredSize);
|
||||
ans = sb.ToString();
|
||||
break;
|
||||
}
|
||||
int nBytes = BUFFER_SIZE;
|
||||
StringBuilder sb = new StringBuilder(nBytes);
|
||||
SetupDiGetDeviceInstanceId(h, ref da, sb, nBytes, out requiredSize);
|
||||
ans = sb.ToString();
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
while(success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
i++;
|
||||
}
|
||||
while(success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
@@ -86,15 +86,13 @@ namespace DiscImageChef.Devices.Windows
|
||||
if(port.IsHub) SearchHubDriverKeyName(port.GetHub(), ref foundDevice, driverKeyName);
|
||||
else
|
||||
{
|
||||
if(port.IsDeviceConnected)
|
||||
{
|
||||
UsbDevice device = port.GetDevice();
|
||||
if(device.DeviceDriverKey == driverKeyName)
|
||||
{
|
||||
foundDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!port.IsDeviceConnected) continue;
|
||||
|
||||
UsbDevice device = port.GetDevice();
|
||||
if(device.DeviceDriverKey != driverKeyName) continue;
|
||||
|
||||
foundDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,15 +119,13 @@ namespace DiscImageChef.Devices.Windows
|
||||
if(port.IsHub) SearchHubInstanceId(port.GetHub(), ref foundDevice, instanceId);
|
||||
else
|
||||
{
|
||||
if(port.IsDeviceConnected)
|
||||
{
|
||||
UsbDevice device = port.GetDevice();
|
||||
if(device.InstanceId == instanceId)
|
||||
{
|
||||
foundDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!port.IsDeviceConnected) continue;
|
||||
|
||||
UsbDevice device = port.GetDevice();
|
||||
if(device.InstanceId != instanceId) continue;
|
||||
|
||||
foundDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,24 +269,23 @@ namespace DiscImageChef.Devices.Windows
|
||||
int ans = -1;
|
||||
|
||||
IntPtr h = CreateFile(devicePath.TrimEnd('\\'), 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
int requiredSize;
|
||||
StorageDeviceNumber sdn = new StorageDeviceNumber();
|
||||
int nBytes = Marshal.SizeOf(sdn);
|
||||
IntPtr ptrSdn = Marshal.AllocHGlobal(nBytes);
|
||||
if(h.ToInt32() == INVALID_HANDLE_VALUE) return ans;
|
||||
|
||||
if(DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, ptrSdn, nBytes, out requiredSize,
|
||||
IntPtr.Zero))
|
||||
{
|
||||
sdn = (StorageDeviceNumber)Marshal.PtrToStructure(ptrSdn, typeof(StorageDeviceNumber));
|
||||
// just my way of combining the relevant parts of the
|
||||
// STORAGE_DEVICE_NUMBER into a single number
|
||||
ans = (sdn.DeviceType << 8) + sdn.DeviceNumber;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrSdn);
|
||||
CloseHandle(h);
|
||||
int requiredSize;
|
||||
StorageDeviceNumber sdn = new StorageDeviceNumber();
|
||||
int nBytes = Marshal.SizeOf(sdn);
|
||||
IntPtr ptrSdn = Marshal.AllocHGlobal(nBytes);
|
||||
|
||||
if(DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, ptrSdn, nBytes, out requiredSize,
|
||||
IntPtr.Zero))
|
||||
{
|
||||
sdn = (StorageDeviceNumber)Marshal.PtrToStructure(ptrSdn, typeof(StorageDeviceNumber));
|
||||
// just my way of combining the relevant parts of the
|
||||
// STORAGE_DEVICE_NUMBER into a single number
|
||||
ans = (sdn.DeviceType << 8) + sdn.DeviceNumber;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrSdn);
|
||||
CloseHandle(h);
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user