Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
8.7 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1995
  4. * TITLE: USBITEM.H
  5. * VERSION: 1.0
  6. * AUTHOR: jsenior
  7. * DATE: 10/28/1998
  8. *
  9. ********************************************************************************
  10. *
  11. * CHANGE LOG:
  12. *
  13. * DATE REV DESCRIPTION
  14. * ---------- ------- ----------------------------------------------------------
  15. * 10/28/1998 jsenior Original implementation.
  16. *
  17. *******************************************************************************/
  18. #ifndef _USBITEM_H
  19. #define _USBITEM_H
  20. #include <windows.h>
  21. //#include <windowsx.h>
  22. #include <objbase.h>
  23. #include <setupapi.h>
  24. #include <devioctl.h>
  25. #pragma warning(disable : 4200)
  26. #include <usbioctl.h>
  27. #include <usb.h>
  28. #include <wdmguid.h>
  29. #include <tchar.h>
  30. #include "str.h"
  31. #include "vec.h"
  32. extern "C" {
  33. #include <cfgmgr32.h>
  34. }
  35. #include <stdio.h> // Used for sprintf
  36. HANDLE
  37. UsbCreateFileA(
  38. IN LPCWSTR lpFileName,
  39. IN DWORD dwDesiredAccess,
  40. IN DWORD dwShareMode,
  41. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  42. IN DWORD dwCreationDisposition,
  43. IN DWORD dwFlagsAndAttributes,
  44. IN HANDLE hTemplateFile);
  45. #ifndef WINNT
  46. #define USBHID TEXT("HID")
  47. #define MyComputerClass TEXT("System")
  48. #define UsbSprintf sprintf
  49. #define UsbCreateFile UsbCreateFileA
  50. #define USBPROC DLGPROC
  51. // Win64 stuff
  52. #define UsbSetWindowLongPtr SetWindowLong
  53. #define UsbGetWindowLongPtr GetWindowLong
  54. #define USBDWLP_MSGRESULT DWL_MSGRESULT
  55. #define USBDWLP_USER DWL_USER
  56. #define USBULONG_PTR ULONG
  57. #define USBLONG_PTR LONG
  58. #define USBINT_PTR BOOL
  59. #else
  60. #define USBHID TEXT("HIDClass")
  61. #define MyComputerClass TEXT("Computer")
  62. #define UsbSprintf wsprintf
  63. #define UsbCreateFile CreateFile
  64. #define USBPROC WNDPROC
  65. // Win64 stuff
  66. #define UsbSetWindowLongPtr SetWindowLongPtr
  67. #define UsbGetWindowLongPtr GetWindowLongPtr
  68. #define USBDWLP_MSGRESULT DWLP_MSGRESULT
  69. #define USBDWLP_USER DWLP_USER
  70. #define USBULONG_PTR ULONG_PTR
  71. #define USBLONG_PTR LONG_PTR
  72. #define USBINT_PTR INT_PTR
  73. #endif
  74. typedef _Str <WCHAR> String;
  75. typedef _Str <TCHAR> UsbString;
  76. class UsbConfigInfo {
  77. public:
  78. UsbConfigInfo();
  79. UsbConfigInfo(const UsbString& Desc, const UsbString& Class, DWORD Failure =0, ULONG Status =0, ULONG Problem =0);
  80. UsbString deviceDesc, deviceClass;
  81. String driverName;
  82. DEVINST devInst;
  83. DWORD usbFailure;
  84. ULONG status;
  85. ULONG problemNumber;
  86. };
  87. //#define CONNECTION_INFO_SIZE (sizeof(USB_NODE_CONNECTION_INFORMATION) + sizeof(USB_PIPE_INFO) * 16)
  88. class UsbDeviceInfo {
  89. public:
  90. UsbDeviceInfo();
  91. // UsbDeviceInfo(const UsbDeviceInfo& UDI);
  92. ~UsbDeviceInfo();
  93. String hubName; // guid if a hub
  94. BOOL isHub;
  95. USB_NODE_INFORMATION hubInfo; // filled if a hub
  96. PUSB_NODE_CONNECTION_INFORMATION connectionInfo; // NULL if root HUB
  97. PUSB_CONFIGURATION_DESCRIPTOR configDesc;
  98. PUSB_DESCRIPTOR_REQUEST configDescReq;
  99. };
  100. typedef struct
  101. {
  102. LPTSTR szClassName;
  103. INT imageIndex;
  104. } IconItem;
  105. typedef _Vec<IconItem> IconTable;
  106. class UsbImageList {
  107. private:
  108. IconTable iconTable;
  109. SP_CLASSIMAGELIST_DATA ClassImageList;
  110. BOOL GetClassImageList();
  111. public:
  112. UsbImageList() : iconTable() { GetClassImageList(); }
  113. ~UsbImageList() { SetupDiDestroyClassImageList(&ClassImageList); }
  114. HIMAGELIST ImageList() { return ClassImageList.ImageList; }
  115. BOOL GetClassImageIndex(LPCTSTR DeviceClass, PINT ImageIndex);
  116. };
  117. class UsbItem;
  118. typedef
  119. BOOL
  120. (*PUsbItemActionIsValid) (
  121. UsbItem *Item);
  122. class UsbItem {
  123. public:
  124. UsbItem() : configInfo(0), deviceInfo(0), bandwidth(0), itemType(None),
  125. imageIndex(0) { UnusedPort=FALSE; child=0; parent=0; sibling=0;
  126. #ifdef HUB_CAPS
  127. ZeroMemory(&hubCaps, sizeof(USB_HUB_CAPABILITIES));
  128. #endif
  129. }
  130. // UsbItem(const UsbItem& Other, UsbItem *Parent);
  131. ~UsbItem();
  132. enum UsbItemType {
  133. Root = 1,
  134. HCD,
  135. RootHub,
  136. Hub,
  137. Device,
  138. Empty,
  139. None
  140. };
  141. struct UsbItemAction {
  142. virtual BOOL operator()(UsbItem* item) {return TRUE;}
  143. };
  144. BOOL IsUnusedPort() { return UnusedPort; }
  145. UsbItem* AddLeaf(UsbItem* Parent, UsbDeviceInfo* DeviceInfo,
  146. UsbItemType Type, UsbConfigInfo* ConfigInfo,
  147. UsbImageList* ImageList);
  148. BOOL EnumerateAll(UsbImageList* ClassImageList);
  149. BOOL EnumerateController(UsbItem *Parent,
  150. const String &RootName,
  151. UsbImageList* ClassImageList,
  152. DEVINST DevInst);
  153. BOOL EnumerateHub(const String &HubName,
  154. UsbImageList* ClassImageList,
  155. DEVINST DevInst,
  156. UsbItem *Parent,
  157. UsbItem::UsbItemType itemType);
  158. // BOOL EnumerateDevice(DEVINST DevInst);
  159. BOOL ComputeBandwidth();
  160. BOOL ComputePower();
  161. UINT TotalTreeBandwidth();
  162. ULONG PortPower();
  163. ULONG NumPorts();
  164. ULONG NumChildren();
  165. ULONG UsbVersion();
  166. ULONG DistanceFromController() { return IsController() ? 0 :
  167. (parent ? 1+parent->DistanceFromController() : 0); }
  168. BOOL IsHub();
  169. BOOL IsController();
  170. BOOL Walk(UsbItemAction& Action);
  171. BOOL ShallowWalk(UsbItemAction& Action);
  172. BOOL IsDescriptionValidDevice();
  173. BOOL GetDeviceInfo( String &HubName, ULONG index);
  174. static ULONG CalculateBWPercent(ULONG bw);
  175. static BOOL InsertTreeItem (HWND hWndTree,
  176. UsbItem *usbItem,
  177. HTREEITEM hParent,
  178. LPTV_INSERTSTRUCT item,
  179. PUsbItemActionIsValid IsValid,
  180. PUsbItemActionIsValid IsBold,
  181. PUsbItemActionIsValid IsExpanded);
  182. static UINT EndpointBandwidth(ULONG MaxPacketSize,
  183. UCHAR EndpointType,
  184. BOOLEAN LowSpeed);
  185. UsbItem *parent, *sibling, *child;
  186. UsbDeviceInfo* deviceInfo;
  187. UsbConfigInfo* configInfo;
  188. #ifdef HUB_CAPS
  189. USB_HUB_CAPABILITIES hubCaps;
  190. #endif
  191. UsbItemType itemType;
  192. int imageIndex;
  193. UINT bandwidth;
  194. UINT power;
  195. USB_NODE_CONNECTION_ATTRIBUTES cxnAttributes;
  196. protected:
  197. BOOL GetHubInfo(HANDLE hHubDevice);
  198. static int CalculateTotalBandwidth(ULONG NumPipes,
  199. BOOLEAN LowSpeed,
  200. USB_PIPE_INFO *PipeInfo);
  201. static ULONG CalculateUsbBandwidth(ULONG MaxPacketSize,
  202. UCHAR EndpointType,
  203. UCHAR Interval,
  204. BOOLEAN LowSpeed);
  205. // enum functions
  206. /* BOOL Enumerate(HANDLE Controller,
  207. UsbImageList* ClassImageList,
  208. DEVINST RootDevInst);*/
  209. void EnumerateHubPorts(HANDLE HHubDevice,
  210. ULONG NPorts,
  211. UsbImageList* ClassImageList);
  212. // wrappers around IOCTL and cfgmgr
  213. static String GetHCDDriverKeyName(HANDLE HController);
  214. static String GetExternalHubName (HANDLE Hub, ULONG ConnectionIndex);
  215. static String GetRootHubName(HANDLE HostController);
  216. PUSB_DESCRIPTOR_REQUEST GetConfigDescriptor(HANDLE hHubDevice,
  217. ULONG ConnectionIndex);
  218. static BOOL SearchAndReplace (LPCWSTR FindThis,
  219. LPCWSTR FindWithin,
  220. LPCWSTR ReplaceWith,
  221. String &NewString);
  222. private:
  223. void GetClassImageIndex(UsbImageList *ClassImageList);
  224. PUSB_NODE_CONNECTION_INFORMATION GetConnectionInformation(HANDLE HHubDevice,
  225. ULONG index);
  226. BOOL GetPortAttributes(HANDLE HHubDevice,
  227. PUSB_NODE_CONNECTION_ATTRIBUTES connectionAttributes,
  228. ULONG index);
  229. BOOL UnusedPort;
  230. };
  231. //
  232. // Helper functions
  233. //
  234. BOOL UsbTreeView_DeleteAllItems(HWND hTreeDevices);
  235. HTREEITEM TreeView_FindItem(HWND hWndTree, LPCTSTR text);
  236. void GetConfigMgrInfo(const String& DriverName, UsbConfigInfo* ConfigInfo);
  237. String GetDriverKeyName(HANDLE Hub, ULONG ConnectionIndex);
  238. HANDLE GetHandleForDevice(const String &DeviceName);
  239. #endif // _USBITEM_H