diff USBLib/Windows/USB/UsbDevice.cs @ 60:3424fa5a12c9

Updated Windows USB enumeration classes and VBoxUSB backend
author Ivo Smits <Ivo@UCIS.nl>
date Sat, 12 Oct 2013 16:35:24 +0200
parents fd63c453ff65
children 2b24666cd759
line wrap: on
line diff
--- a/USBLib/Windows/USB/UsbDevice.cs	Wed Oct 09 20:56:28 2013 +0200
+++ b/USBLib/Windows/USB/UsbDevice.cs	Sat Oct 12 16:35:24 2013 +0200
@@ -1,8 +1,6 @@
 using System;
-using System.Collections.Generic;
 using System.ComponentModel;
 using System.Runtime.InteropServices;
-using System.Text;
 using Microsoft.Win32.SafeHandles;
 using UCIS.HWLib.Windows.Devices;
 using UCIS.USBLib.Communication;
@@ -10,8 +8,8 @@
 using UCIS.USBLib.Internal.Windows;
 
 namespace UCIS.HWLib.Windows.USB {
-	public class UsbDevice : IUsbInterface {
-		internal static SafeFileHandle OpenHandle(String path) {
+	public class UsbDevice : IUsbDevice, IUsbInterface {
+		protected internal static SafeFileHandle OpenHandle(String path) {
 			SafeFileHandle handle = Kernel32.CreateFile(path, Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero);
 			if (handle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error());
 			return handle;
@@ -29,7 +27,7 @@
 				throw new Win32Exception(Marshal.GetLastWin32Error());
 			return true;
 		}
-		internal static String GetNodeConnectionName(SafeFileHandle handle, UInt32 port) {
+		protected static String GetNodeConnectionName(SafeFileHandle handle, UInt32 port) {
 			int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_NAME));
 			USB_NODE_CONNECTION_NAME nameConnection = new USB_NODE_CONNECTION_NAME();
 			nameConnection.ConnectionIndex = port;
@@ -37,7 +35,7 @@
 				throw new Win32Exception(Marshal.GetLastWin32Error());
 			return nameConnection.NodeName;
 		}
-		internal unsafe static int GetDescriptor(SafeFileHandle handle, UInt32 port, byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) {
+		protected unsafe static int GetDescriptor(SafeFileHandle handle, UInt32 port, byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) {
 			int szRequest = Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST));
 			USB_DESCRIPTOR_REQUEST request = new USB_DESCRIPTOR_REQUEST();
 			request.ConnectionIndex = port;
@@ -57,7 +55,7 @@
 			if (nBytes > 0) Buffer.BlockCopy(bigbuffer, szRequest, buffer, offset, nBytes);
 			return nBytes;
 		}
-		internal unsafe static String GetRootHubName(SafeFileHandle handle) {
+		protected internal unsafe static String GetRootHubName(SafeFileHandle handle) {
 			USB_ROOT_HUB_NAME rootHubName = new USB_ROOT_HUB_NAME();
 			int nBytesReturned;
 			if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, out rootHubName, Marshal.SizeOf(rootHubName), out nBytesReturned, IntPtr.Zero))
@@ -65,7 +63,7 @@
 			if (rootHubName.ActualLength <= 0) return null;
 			return rootHubName.RootHubName;
 		}
-		internal unsafe static String GetNodeConnectionDriverKey(SafeFileHandle handle, UInt32 port) {
+		protected unsafe static String GetNodeConnectionDriverKey(SafeFileHandle handle, UInt32 port) {
 			USB_NODE_CONNECTION_DRIVERKEY_NAME DriverKeyStruct = new USB_NODE_CONNECTION_DRIVERKEY_NAME();
 			int nBytes = Marshal.SizeOf(DriverKeyStruct);
 			DriverKeyStruct.ConnectionIndex = port;
@@ -75,10 +73,10 @@
 		}
 
 		public UsbDevice Parent { get; protected set; }
-		public string DevicePath { get; private set; }
-		public uint AdapterNumber { get; private set; }
+		public String DevicePath { get; private set; }
+		public UInt32 AdapterNumber { get; private set; }
 		internal USB_NODE_CONNECTION_INFORMATION_EX NodeConnectionInfo { get; set; }
-		internal USB_DEVICE_DESCRIPTOR DeviceDescriptor { get { return NodeConnectionInfo.DeviceDescriptor; } }
+		private USB_DEVICE_DESCRIPTOR DeviceDescriptor { get { return NodeConnectionInfo.DeviceDescriptor; } }
 
 		public bool IsHub { get { return NodeConnectionInfo.DeviceIsHub != 0; } }
 		public bool IsConnected { get { return NodeConnectionInfo.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected; } }
@@ -105,7 +103,7 @@
 		public string SerialNumber { get { return DeviceDescriptor == null ? null : GetStringSafe(DeviceDescriptor.iSerialNumber); } }
 		public virtual string DriverKey { get { using (SafeFileHandle handle = OpenHandle(DevicePath)) return UsbHub.GetNodeConnectionDriverKey(handle, AdapterNumber); } }
 
-		public virtual string DeviceDescription { get { return DeviceNode == null ? null : DeviceNode.GetPropertyString(CMRDP.DEVICEDESC); } }
+		public virtual string DeviceDescription { get { return DeviceNode == null ? null : DeviceNode.DeviceDescription; } }
 		public string DeviceID { get { return DeviceNode == null ? null : DeviceNode.DeviceID; } }
 
 		private DeviceNode mDeviceNode;
@@ -137,10 +135,10 @@
 		}
 
 		static UsbDevice GetUsbDevice(DeviceNode node, out Boolean isHostController) {
-			String[] hciinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_HOST_CONTROLLER);
-			if (hciinterface != null && hciinterface.Length > 0) {
+			UsbController controller = UsbController.GetControllerForDeviceNode(node);
+			if (controller != null) {
 				isHostController = true;
-				return (new UsbController(null, node, hciinterface[0])).RootHub;
+				return controller.RootHub;
 			}
 			isHostController = false;
 			DeviceNode parent = node.GetParent();
@@ -148,8 +146,12 @@
 			UsbDevice usbdev = GetUsbDevice(parent, out isHostControllerA);
 			if (isHostControllerA) return usbdev;
 			UsbHub usbhub = usbdev as UsbHub;
-			if (usbhub == null) return null;
+			if (usbhub == null) {
+				if (parent.Service == "usbccgp") return usbdev;
+				return null;
+			}
 			String driverkey = node.DriverKey;
+			if (driverkey == null) return null;
 			foreach (UsbDevice child in usbhub.Devices) {
 				if (driverkey.Equals(child.DriverKey, StringComparison.InvariantCultureIgnoreCase)) return child;
 			}
@@ -158,27 +160,6 @@
 		public static UsbDevice GetUsbDevice(DeviceNode node) {
 			Boolean isHostController;
 			return GetUsbDevice(node, out isHostController);
-			/*
-
-			String[] hubinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_HUB);
-			if (hubinterface != null && hubinterface.Length > 0) {
-				USB_NODE_CONNECTION_INFORMATION_EX nodeConnection;
-				using (SafeFileHandle handle = OpenHandle(hubinterface[0])) {
-					if (!GetNodeConnectionInformation(handle, 0, out nodeConnection)) return null;
-				}
-				return new UsbHub(null, nodeConnection, hubinterface[0], false);
-			}
-			String[] devinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_DEVICE);
-			if (devinterface == null || devinterface.Length == 0) return null;
-			DeviceNode parent = node.GetParent();
-			if (parent == null) return null;
-			UsbHub usbhub = GetUsbDevice(parent) as UsbHub;
-			if (usbhub == null) return null;
-			String driverkey = node.DriverKey;
-			foreach (UsbDevice usbdev in usbhub.Devices) {
-				if (driverkey.Equals(usbdev.DriverKey, StringComparison.InvariantCultureIgnoreCase)) return usbdev;
-			}
-			return null;*/
 		}
 
 		#region IUsbInterface Members
@@ -202,6 +183,12 @@
 		UsbPipeStream IUsbInterface.GetInterruptStream(byte endpoint) { throw new NotImplementedException(); }
 		void IDisposable.Dispose() { }
 		#endregion
+		#region IUsbDevice Members
+		byte IUsbDevice.Configuration { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } }
+		void IUsbDevice.ClaimInterface(int interfaceID) { }
+		void IUsbDevice.ReleaseInterface(int interfaceID) {	}
+		void IUsbDevice.ResetDevice() { throw new NotSupportedException(); }
+		IUsbDeviceRegistry IUsbDevice.Registry { get { throw new NotImplementedException(); } }
+		#endregion
 	}
-	
 }