Source code of Windows XP (NT5)
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.

338 lines
5.9 KiB

  1. //***************************************************************************
  2. //
  3. // main.c
  4. //
  5. // Module: Windows HBA API implmentation
  6. //
  7. // Purpose: Contains DLL entry points and main HBA api functions
  8. //
  9. // Copyright (c) 2001 Microsoft Corporation
  10. //
  11. //***************************************************************************
  12. #include "hbaapip.h"
  13. #include <stdio.h>
  14. //***************************************************************************
  15. //
  16. // LibMain32
  17. //
  18. // Purpose: Entry point for DLL.
  19. //
  20. // Return: TRUE if OK.
  21. //
  22. //***************************************************************************
  23. HANDLE Module;
  24. BOOL WINAPI LibMain32(
  25. HINSTANCE Instance,
  26. ULONG Reason,
  27. LPVOID pvReserved
  28. )
  29. {
  30. if (DLL_PROCESS_ATTACH==Reason)
  31. {
  32. Mutex = CreateMutex(NULL,
  33. FALSE,
  34. NULL);
  35. if (Mutex == NULL)
  36. {
  37. return(FALSE);
  38. }
  39. Module = Instance;
  40. DisableThreadLibraryCalls(Module);
  41. }
  42. return(TRUE);
  43. }
  44. //
  45. // Main HBA Apis
  46. //
  47. HBA_STATUS HBA_API HBA_RegisterLibrary(
  48. PHBA_ENTRYPOINTS entrypoints
  49. )
  50. {
  51. return(HBA_STATUS_ERROR_NOT_SUPPORTED);
  52. }
  53. HBA_UINT32 HBA_API HBA_GetVersion(
  54. void
  55. )
  56. {
  57. return(HBA_VERSION);
  58. }
  59. HBA_STATUS HBA_API HBA_LoadLibrary(
  60. )
  61. {
  62. return(HBA_STATUS_OK);
  63. }
  64. HBA_STATUS HBA_API HBA_FreeLibrary(
  65. void
  66. )
  67. {
  68. return(HBA_STATUS_OK);
  69. }
  70. HBA_UINT32 HBA_API HBA_GetNumberOfAdapters(
  71. void
  72. )
  73. {
  74. HANDLE Handle;
  75. PWNODE_ALL_DATA Buffer;
  76. ULONG Count = 0;
  77. ULONG Status;
  78. Status = WmiOpenBlock((LPGUID)&MSFC_FCAdapterHBAAttributes_GUID,
  79. GENERIC_READ,
  80. &Handle);
  81. if (Status == ERROR_SUCCESS)
  82. {
  83. Status = QueryAllData(Handle,
  84. &Buffer);
  85. if (Status == ERROR_SUCCESS)
  86. {
  87. Status = ParseAllData(Buffer,
  88. &Count,
  89. NULL,
  90. NULL,
  91. NULL),
  92. FreeMemory(Buffer);
  93. }
  94. WmiCloseBlock(Handle);
  95. }
  96. return( Count );
  97. }
  98. HBA_STATUS HBA_GetAdapterNameW(
  99. IN HBA_UINT32 AdapterIndex,
  100. OUT PWCHAR AdapterName
  101. )
  102. {
  103. HANDLE Handle;
  104. PWNODE_ALL_DATA Buffer;
  105. HBA_STATUS HbaStatus = HBA_STATUS_ERROR;
  106. ULONG Count;
  107. ULONG Len;
  108. PWCHAR Name;
  109. ULONG Status;
  110. PUSHORT *InstanceNames;
  111. Status = WmiOpenBlock((LPGUID)&MSFC_FCAdapterHBAAttributes_GUID,
  112. GENERIC_READ,
  113. &Handle);
  114. if (Status == ERROR_SUCCESS)
  115. {
  116. Status = QueryAllData(Handle,
  117. &Buffer);
  118. if (Status == ERROR_SUCCESS)
  119. {
  120. Status = ParseAllData(Buffer,
  121. &Count,
  122. &InstanceNames,
  123. NULL,
  124. NULL);
  125. if (Status == ERROR_SUCCESS)
  126. {
  127. if (AdapterIndex < Count)
  128. {
  129. Name = InstanceNames[AdapterIndex];
  130. Len = *Name++;
  131. wcsncpy(AdapterName, Name, Len/sizeof(WCHAR));
  132. AdapterName[Len] = 0;
  133. HbaStatus = HBA_STATUS_OK;
  134. }
  135. FreeMemory(InstanceNames);
  136. }
  137. FreeMemory(Buffer);
  138. }
  139. WmiCloseBlock(Handle);
  140. }
  141. return(HbaStatus);
  142. }
  143. HBA_STATUS HBA_API HBA_GetAdapterName(
  144. IN HBA_UINT32 AdapterIndex,
  145. OUT PCHAR AdapterName
  146. )
  147. {
  148. WCHAR AdapterNameW[256];
  149. HBA_STATUS HbaStatus;
  150. ULONG Status;
  151. HbaStatus = HBA_GetAdapterNameW(AdapterIndex,
  152. AdapterNameW);
  153. if (HbaStatus == HBA_STATUS_OK)
  154. {
  155. Status = UnicodeToAnsi(AdapterNameW,
  156. AdapterName,
  157. 256);
  158. if (Status != ERROR_SUCCESS)
  159. {
  160. HbaStatus = HBA_STATUS_ERROR;
  161. }
  162. }
  163. return(HbaStatus);
  164. }
  165. HBA_HANDLE HBA_OpenAdapterW(
  166. PWCHAR AdapterName
  167. )
  168. {
  169. HBA_HANDLE HbaHandle = 0;
  170. HANDLE Handle;
  171. PADAPTER_HANDLE HandleData;
  172. ULONG Status;
  173. PWNODE_SINGLE_INSTANCE Buffer;
  174. Status = WmiOpenBlock((LPGUID)&MSFC_FCAdapterHBAAttributes_GUID,
  175. GENERIC_READ,
  176. &Handle);
  177. if (Status == ERROR_SUCCESS)
  178. {
  179. //
  180. // Validate adapter name by querying for that adapter name as a
  181. // WMI instance name
  182. //
  183. Status = QuerySingleInstance(Handle,
  184. AdapterName,
  185. &Buffer);
  186. if (Status == ERROR_SUCCESS)
  187. {
  188. //
  189. // If it exists then we allocate an adapter handle data
  190. // structure and insert it on the list of open adapter
  191. // handles
  192. //
  193. HandleData = AllocMemory(sizeof(ADAPTER_HANDLE));
  194. if (HandleData != NULL)
  195. {
  196. memset(HandleData, 0, sizeof(ADAPTER_HANDLE));
  197. HandleData->InstanceName = AllocMemory((wcslen(AdapterName) + 1) *
  198. sizeof(WCHAR));
  199. if (HandleData->InstanceName != NULL)
  200. {
  201. wcscpy(HandleData->InstanceName, AdapterName);
  202. EnterCritSection();
  203. HandleData->HbaHandle = HbaHandleCounter++;
  204. InsertHeadList(&HbaHandleList, &HandleData->List);
  205. LeaveCritSection();
  206. HbaHandle = HandleData->HbaHandle;
  207. } else {
  208. FreeMemory(HandleData);
  209. }
  210. }
  211. FreeMemory(Buffer);
  212. }
  213. WmiCloseBlock(Handle);
  214. }
  215. return(HbaHandle);
  216. }
  217. HBA_HANDLE HBA_API HBA_OpenAdapter(
  218. PCHAR AdapterName
  219. )
  220. {
  221. PWCHAR AdapterNameW;
  222. ULONG Len, AllocLen;
  223. ULONG Status;
  224. HBA_HANDLE HbaHandle;
  225. Len = strlen(AdapterName) + 1;
  226. AllocLen = Len * sizeof(WCHAR);
  227. AdapterNameW = AllocMemory(AllocLen);
  228. if (AdapterNameW != NULL)
  229. {
  230. Status = AnsiToUnicode(AdapterName,
  231. AdapterNameW,
  232. Len);
  233. HbaHandle = HBA_OpenAdapterW(AdapterNameW);
  234. FreeMemory(AdapterNameW);
  235. }
  236. return(HbaHandle);
  237. }
  238. void HBA_API HBA_CloseAdapter(
  239. HBA_HANDLE HbaHandle
  240. )
  241. {
  242. PADAPTER_HANDLE HandleData;
  243. HandleData = GetDataByHandle(HbaHandle);
  244. if (HandleData != NULL)
  245. {
  246. EnterCritSection();
  247. RemoveEntryList(&HandleData->List);
  248. LeaveCritSection();
  249. FreeMemory(HandleData->InstanceName);
  250. FreeMemory(HandleData);
  251. }
  252. }
  253. #if DBG
  254. ULONG HbaapiDebugSpew = 1;
  255. VOID
  256. HbaapiDebugPrint(
  257. PCHAR DebugMessage,
  258. ...
  259. )
  260. /*++
  261. Routine Description:
  262. Debug print for properties pages - stolen from classpnp\class.c
  263. Arguments:
  264. Debug print level between 0 and 3, with 3 being the most verbose.
  265. Return Value:
  266. None
  267. --*/
  268. {
  269. CHAR SpewBuffer[DEBUG_BUFFER_LENGTH];
  270. va_list ap;
  271. va_start(ap, DebugMessage);
  272. if (HbaapiDebugSpew)
  273. {
  274. _vsnprintf(SpewBuffer, DEBUG_BUFFER_LENGTH, DebugMessage, ap);
  275. OutputDebugStringA(SpewBuffer);
  276. }
  277. va_end(ap);
  278. } // end WmiDebugPrint()
  279. #endif