Mercurial > hg > ucis.core
annotate USBLib/Windows/USB/UsbController.cs @ 99:be17dd3f6927
Small fixes in HTTP server and libusb1 backend
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sun, 17 Aug 2014 15:35:23 +0200 |
parents | 309c705d7460 |
children |
rev | line source |
---|---|
21 | 1 using System; |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
2 using System.Collections.Generic; |
63
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
3 using System.ComponentModel; |
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
4 using System.Runtime.InteropServices; |
21 | 5 using Microsoft.Win32.SafeHandles; |
6 using UCIS.HWLib.Windows.Devices; | |
7 using UCIS.USBLib.Internal.Windows; | |
8 | |
9 namespace UCIS.HWLib.Windows.USB { | |
10 public class UsbController { | |
11 public String DevicePath { get; private set; } | |
12 public DeviceNode DeviceNode { get; private set; } | |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
13 public String DeviceDescription { get { return DeviceNode.DeviceDescription; } } |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
14 public String DriverKey { get { return DeviceNode.DriverKey; } } |
58
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
15 public UsbHub RootHub { |
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
16 get { |
63
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
17 USB_ROOT_HUB_NAME rootHubName = new USB_ROOT_HUB_NAME(); |
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
18 int nBytesReturned; |
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
19 using (SafeFileHandle handle = UsbHub.OpenHandle(DevicePath)) |
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
20 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, out rootHubName, Marshal.SizeOf(rootHubName), out nBytesReturned, IntPtr.Zero)) |
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
21 throw new Win32Exception(Marshal.GetLastWin32Error()); |
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
22 if (rootHubName.ActualLength <= 0) return null; |
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
23 return new UsbHub(null, @"\\?\" + rootHubName.RootHubName, 0); |
58
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
24 } |
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
25 } |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
26 private UsbController(DeviceNode di, String devicePath) { |
21 | 27 this.DeviceNode = di; |
28 this.DevicePath = devicePath; | |
29 } | |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
30 |
63
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
31 static readonly Guid IID_DEVINTERFACE_USB_HOST_CONTROLLER = new Guid(UsbApi.GUID_DEVINTERFACE_USB_HOST_CONTROLLER); |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
32 public static UsbController GetControllerForDeviceNode(DeviceNode node) { |
63
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
62
diff
changeset
|
33 if (node == null) return null; |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
34 String[] interfaces = node.GetInterfaces(IID_DEVINTERFACE_USB_HOST_CONTROLLER); |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
35 if (interfaces == null || interfaces.Length == 0) return null; |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
36 return new UsbController(node, interfaces[0]); |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
37 } |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
38 |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
39 public static IList<UsbController> GetControllers() { |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
40 IList<UsbController> devices = new List<UsbController>(); |
62
edc41c861d96
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
60
diff
changeset
|
41 foreach (DeviceNode dev in DeviceNode.GetDevices(IID_DEVINTERFACE_USB_HOST_CONTROLLER)) { |
60
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
42 UsbController controller = GetControllerForDeviceNode(dev); |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
43 if (controller != null) devices.Add(controller); |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
44 } |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
45 return devices; |
3424fa5a12c9
Updated Windows USB enumeration classes and VBoxUSB backend
Ivo Smits <Ivo@UCIS.nl>
parents:
58
diff
changeset
|
46 } |
21 | 47 } |
58
fd63c453ff65
Improved Windows USB enumeration classes
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
48 } |