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.

492 lines
13 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. UMGUSB.C
  5. Abstract:
  6. This module contains the code for the
  7. helper lib that talks to the generic USB driver
  8. Environment:
  9. Kernel & user mode
  10. Revision History:
  11. Sept-01 : created by Kenneth Ray
  12. --*/
  13. #include <stdlib.h>
  14. #include <wtypes.h>
  15. #include <winioctl.h>
  16. #include <initguid.h>
  17. #include "genusbio.h"
  18. #include "umgusb.h"
  19. //
  20. // __cdecl main (int argc, char *argv[])
  21. // {
  22. // return 0;
  23. // }
  24. //
  25. STDAPI_(BOOL)
  26. Entry32(HINSTANCE hinst, DWORD dwReason, LPVOID lpReserved)
  27. {
  28. switch (dwReason) {
  29. default: return TRUE;
  30. }
  31. }
  32. int GenUSB_Hello (char * buff, int len)
  33. {
  34. CHAR ret[] = "Hello\n";
  35. ULONG length = (sizeof (ret) < len) ? sizeof (ret) : len;
  36. CopyMemory (buff, ret, length);
  37. return sizeof (ret);
  38. }
  39. void __stdcall
  40. GenUSB_GetDeviceInterfaceGuid (
  41. OUT LPGUID Guid
  42. )
  43. /*++
  44. Routine Description:
  45. Please see hidsdi.h for explination
  46. Notes:
  47. --*/
  48. {
  49. *Guid = GUID_DEVINTERFACE_GENUSB;
  50. }
  51. BOOL __stdcall
  52. GenUSB_GetCapabilities (
  53. IN HANDLE GenUSBDeviceObject,
  54. OUT PGENUSB_CAPABILITIES Capabilities
  55. )
  56. /*++
  57. Routine Description:
  58. please see gusb.h for explination
  59. Notes:
  60. --*/
  61. {
  62. ULONG bytes;
  63. return DeviceIoControl (GenUSBDeviceObject,
  64. IOCTL_GENUSB_GET_CAPS,
  65. 0, 0,
  66. Capabilities, sizeof (GENUSB_CAPABILITIES),
  67. &bytes, NULL);
  68. }
  69. BOOL __stdcall
  70. GenUSB_GetDeviceDescriptor (
  71. IN HANDLE GenUSBDeviceObject,
  72. OUT PUSB_DEVICE_DESCRIPTOR Descriptor,
  73. IN ULONG DescriptorLength
  74. )
  75. /*++
  76. Routine Description:
  77. please see gusb.h for explination
  78. Notes:
  79. --*/
  80. {
  81. ULONG bytes;
  82. return DeviceIoControl (GenUSBDeviceObject,
  83. IOCTL_GENUSB_GET_DEVICE_DESCRIPTOR,
  84. 0, 0,
  85. Descriptor, DescriptorLength,
  86. &bytes, NULL);
  87. }
  88. BOOL __stdcall
  89. GenUSB_GetConfigurationInformation (
  90. IN HANDLE GenUSBDeviceObject,
  91. OUT PUSB_CONFIGURATION_DESCRIPTOR Descriptor,
  92. IN ULONG DescriptorLength
  93. )
  94. /*++
  95. Routine Description:
  96. please see gusb.h for explination
  97. Notes:
  98. --*/
  99. {
  100. ULONG bytes;
  101. return DeviceIoControl (GenUSBDeviceObject,
  102. IOCTL_GENUSB_GET_CONFIGURATION_DESCRIPTOR,
  103. 0, 0,
  104. Descriptor, DescriptorLength,
  105. &bytes, NULL);
  106. }
  107. BOOL __stdcall
  108. GenUSB_GetStringDescriptor (
  109. IN HANDLE GenUSBDeviceObject,
  110. IN UCHAR Recipient,
  111. IN UCHAR Index,
  112. IN USHORT LanguageId,
  113. OUT PUCHAR Descriptor,
  114. IN USHORT DescriptorLength
  115. )
  116. /*++
  117. Routine Description:
  118. please see gusb.h for explination
  119. Notes:
  120. --*/
  121. {
  122. ULONG bytes;
  123. GENUSB_GET_STRING_DESCRIPTOR getString;
  124. getString.Recipient = Recipient;
  125. getString.Index = Index;
  126. getString.LanguageId = LanguageId;
  127. return DeviceIoControl (GenUSBDeviceObject,
  128. IOCTL_GENUSB_GET_STRING_DESCRIPTOR,
  129. &getString, sizeof (GENUSB_GET_STRING_DESCRIPTOR),
  130. Descriptor, DescriptorLength,
  131. &bytes, NULL);
  132. }
  133. BOOL __stdcall
  134. GenUSB_DefaultControlRequest (
  135. IN HANDLE GenUSBDeviceObject,
  136. IN UCHAR RequestType,
  137. IN UCHAR Request,
  138. IN USHORT Value,
  139. IN USHORT Index,
  140. IN OUT PGENUSB_REQUEST_RESULTS Result,
  141. IN USHORT BufferLength
  142. )
  143. /*++
  144. Routine Description:
  145. please see gusb.h for explination
  146. Notes:
  147. --*/
  148. {
  149. ULONG bytes;
  150. GENUSB_GET_REQUEST getReq;
  151. BOOL result;
  152. getReq.RequestType = RequestType;
  153. getReq.Request = Request;
  154. getReq.Value = Value;
  155. getReq.Index = Index;
  156. if (BufferLength < sizeof (GENUSB_REQUEST_RESULTS))
  157. {
  158. SetLastError (ERROR_INSUFFICIENT_BUFFER);
  159. return FALSE;
  160. }
  161. return DeviceIoControl (GenUSBDeviceObject,
  162. IOCTL_GENUSB_GET_REQUEST,
  163. &getReq, sizeof (GENUSB_GET_REQUEST),
  164. Result, BufferLength,
  165. &bytes, NULL);
  166. }
  167. BOOL __stdcall
  168. GenUSB_SelectConfiguration (
  169. IN HANDLE GenUSBDeviceObject,
  170. IN UCHAR RequestedNumberInterfaces,
  171. IN USB_INTERFACE_DESCRIPTOR RequestedInterfaces[],
  172. OUT PUCHAR FoundNumberInterfaces,
  173. OUT USB_INTERFACE_DESCRIPTOR FoundInterfaces[]
  174. )
  175. {
  176. ULONG i;
  177. ULONG size;
  178. ULONG bytes;
  179. PGENUSB_SELECT_CONFIGURATION select;
  180. size = sizeof (GENUSB_SELECT_CONFIGURATION)
  181. + (sizeof (USB_INTERFACE_DESCRIPTOR) * RequestedNumberInterfaces);
  182. select = malloc (size);
  183. if (NULL == select)
  184. {
  185. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  186. return FALSE;
  187. }
  188. select->NumberInterfaces = RequestedNumberInterfaces;
  189. for (i=0; i < RequestedNumberInterfaces; i++)
  190. {
  191. select->Interfaces[i] = RequestedInterfaces[i];
  192. }
  193. if (!DeviceIoControl (GenUSBDeviceObject,
  194. IOCTL_GENUSB_SELECT_CONFIGURATION,
  195. select, size,
  196. select, size,
  197. &bytes, NULL))
  198. {
  199. free (select);
  200. return FALSE;
  201. }
  202. *FoundNumberInterfaces = select->NumberInterfaces;
  203. for (i=0; i < *FoundNumberInterfaces; i++)
  204. {
  205. FoundInterfaces[i] = select->Interfaces[i];
  206. }
  207. free (select);
  208. return TRUE;
  209. }
  210. BOOL __stdcall
  211. GenUSB_DeselectConfiguration (
  212. IN HANDLE GenUSBDeviceObject
  213. )
  214. {
  215. ULONG bytes;
  216. return DeviceIoControl (GenUSBDeviceObject,
  217. IOCTL_GENUSB_DESELECT_CONFIGURATION,
  218. NULL, 0,
  219. NULL, 0,
  220. &bytes, NULL);
  221. }
  222. BOOL __stdcall
  223. GenUSB_GetPipeInformation (
  224. IN HANDLE GenUSBDeviceObject,
  225. IN UCHAR InterfaceNumber,
  226. IN UCHAR EndpointAddress,
  227. OUT PUSBD_PIPE_INFORMATION PipeInformation
  228. )
  229. {
  230. ULONG bytes;
  231. BOOL result;
  232. GENUSB_PIPE_INFORMATION pipeInfo;
  233. GENUSB_PIPE_INFO_REQUEST pipeReq;
  234. pipeReq.InterfaceNumber = InterfaceNumber;
  235. pipeReq.EndpointAddress = EndpointAddress;
  236. RtlZeroMemory (&pipeInfo, sizeof (GENUSB_PIPE_INFORMATION));
  237. result = DeviceIoControl (GenUSBDeviceObject,
  238. IOCTL_GENUSB_GET_PIPE_INFO,
  239. &pipeReq, sizeof (GENUSB_PIPE_INFO_REQUEST),
  240. &pipeInfo, sizeof (GENUSB_PIPE_INFORMATION),
  241. &bytes, NULL);
  242. PipeInformation->MaximumPacketSize = pipeInfo.MaximumPacketSize;
  243. PipeInformation->EndpointAddress = pipeInfo.EndpointAddress;
  244. PipeInformation->Interval = pipeInfo.Interval;
  245. PipeInformation->PipeType = pipeInfo.PipeType;
  246. PipeInformation->MaximumTransferSize = pipeInfo.MaximumTransferSize;
  247. PipeInformation->PipeFlags = pipeInfo.PipeFlags;
  248. //
  249. // We are retrieving an ulong from kernel mode, but for simplicity
  250. // we pass back a USBD_PIPE_HANDLE, which happens to be a PVOID.
  251. //
  252. PipeInformation->PipeHandle = (PVOID) (ULONG_PTR) pipeInfo.PipeHandle;
  253. return result;
  254. }
  255. BOOL __stdcall
  256. GenUSB_GetPipeProperties (
  257. IN HANDLE GenUSBDeviceObject,
  258. IN USBD_PIPE_HANDLE PipeHandle,
  259. OUT PGENUSB_PIPE_PROPERTIES Properties
  260. )
  261. {
  262. ULONG bytes;
  263. ULONG ulongPipeHandle;
  264. ulongPipeHandle = (GENUSB_PIPE_HANDLE) (ULONG_PTR) PipeHandle;
  265. return DeviceIoControl (GenUSBDeviceObject,
  266. IOCTL_GENUSB_GET_PIPE_PROPERTIES,
  267. &ulongPipeHandle, sizeof (ULONG),
  268. Properties, sizeof (GENUSB_PIPE_PROPERTIES),
  269. &bytes, NULL);
  270. }
  271. BOOL __stdcall
  272. GenUSB_SetPipeProperties (
  273. IN HANDLE GenUSBDeviceObject,
  274. IN USBD_PIPE_HANDLE PipeHandle,
  275. IN PGENUSB_PIPE_PROPERTIES Properties
  276. )
  277. {
  278. ULONG bytes;
  279. struct {
  280. ULONG PipeHandle;
  281. GENUSB_PIPE_PROPERTIES Properties;
  282. } data;
  283. data.PipeHandle = (GENUSB_PIPE_HANDLE) (ULONG_PTR) PipeHandle;
  284. data.Properties = *Properties;
  285. return DeviceIoControl (GenUSBDeviceObject,
  286. IOCTL_GENUSB_SET_PIPE_PROPERTIES,
  287. &data, sizeof (data),
  288. NULL, 0,
  289. &bytes, NULL);
  290. }
  291. BOOL __stdcall
  292. GenUSB_ResetPipe (
  293. IN HANDLE GenUSBDeviceObject,
  294. IN USBD_PIPE_HANDLE PipeHandle,
  295. IN BOOL ResetPipe,
  296. IN BOOL ClearStall,
  297. IN BOOL FlushData
  298. )
  299. {
  300. GENUSB_RESET_PIPE reset;
  301. ULONG bytes;
  302. reset.Pipe = (GENUSB_PIPE_HANDLE) (ULONG_PTR) PipeHandle;
  303. reset.ResetPipe = (ResetPipe ? TRUE : FALSE);
  304. reset.ClearStall = (ClearStall ? TRUE : FALSE);
  305. reset.FlushData = (FlushData ? TRUE : FALSE);
  306. return DeviceIoControl (GenUSBDeviceObject,
  307. IOCTL_GENUSB_RESET_PIPE,
  308. &reset, sizeof (reset),
  309. NULL, 0,
  310. &bytes, NULL);
  311. }
  312. BOOL __stdcall
  313. GenUSB_SetReadWritePipes (
  314. IN HANDLE GenUSBDeviceObject,
  315. IN USBD_PIPE_HANDLE ReadPipe,
  316. IN USBD_PIPE_HANDLE WritePipe
  317. )
  318. {
  319. ULONG bytes;
  320. GENUSB_SET_READ_WRITE_PIPES pipes;
  321. pipes.ReadPipe = (GENUSB_PIPE_HANDLE) (ULONG_PTR) ReadPipe;
  322. pipes.WritePipe = (GENUSB_PIPE_HANDLE) (ULONG_PTR) WritePipe;
  323. return DeviceIoControl (GenUSBDeviceObject,
  324. IOCTL_GENUSB_SET_READ_WRITE_PIPES,
  325. &pipes, sizeof (GENUSB_SET_READ_WRITE_PIPES),
  326. NULL, 0,
  327. &bytes, NULL);
  328. }
  329. BOOL __stdcall
  330. GenUSB_ReadPipe (
  331. IN HANDLE GenUSBDeviceObject,
  332. IN USBD_PIPE_HANDLE Pipe,
  333. IN BOOL ShortTransferOk,
  334. IN PVOID Buffer,
  335. IN ULONG RequestedBufferLength,
  336. OUT PULONG ReturnedBufferLength,
  337. OUT USBD_STATUS * UrbStatus
  338. )
  339. {
  340. ULONG bytes;
  341. BOOL result;
  342. GENUSB_READ_WRITE_PIPE transfer;
  343. transfer.Pipe = (GENUSB_PIPE_HANDLE) (ULONG_PTR) Pipe;
  344. transfer.UsbdTransferFlags = USBD_TRANSFER_DIRECTION_IN
  345. | (ShortTransferOk ? USBD_SHORT_TRANSFER_OK : 0);
  346. transfer.UrbStatus = USBD_STATUS_SUCCESS;
  347. transfer.BufferLength = RequestedBufferLength;
  348. // Junk is a union with UserBuffer, which initializes the upper bits in
  349. // case we are calling a 64 bit kernel with a 32 bit user mode dll.
  350. transfer.Junk = 0;
  351. transfer.UserBuffer = Buffer;
  352. *ReturnedBufferLength = 0;
  353. *UrbStatus = 0;
  354. result = DeviceIoControl (GenUSBDeviceObject,
  355. IOCTL_GENUSB_READ_WRITE_PIPE,
  356. &transfer, sizeof (GENUSB_READ_WRITE_PIPE),
  357. NULL, 0,
  358. &bytes, NULL);
  359. *ReturnedBufferLength = transfer.BufferLength;
  360. *UrbStatus= transfer.UrbStatus;
  361. return result;
  362. }
  363. BOOL __stdcall
  364. GenUSB_WritePipe (
  365. IN HANDLE GenUSBDeviceObject,
  366. IN USBD_PIPE_HANDLE Pipe,
  367. IN BOOL ShortTransferOk,
  368. IN PVOID Buffer,
  369. IN ULONG RequestedBufferLength,
  370. OUT PULONG ReturnedBufferLength,
  371. OUT USBD_STATUS * UrbStatus
  372. )
  373. {
  374. ULONG bytes;
  375. BOOL result;
  376. GENUSB_READ_WRITE_PIPE transfer;
  377. transfer.Pipe = (GENUSB_PIPE_HANDLE) (ULONG_PTR) Pipe;
  378. transfer.UsbdTransferFlags = USBD_TRANSFER_DIRECTION_OUT
  379. | (ShortTransferOk ? USBD_SHORT_TRANSFER_OK : 0);
  380. transfer.UrbStatus = USBD_STATUS_SUCCESS;
  381. transfer.BufferLength = RequestedBufferLength;
  382. // Junk is a union with UserBuffer, which initializes the upper bits in
  383. // case we are calling a 64 bit kernel with a 32 bit user mode dll.
  384. transfer.Junk = 0;
  385. transfer.UserBuffer = Buffer;
  386. *ReturnedBufferLength = 0;
  387. *UrbStatus = 0;
  388. result = DeviceIoControl (GenUSBDeviceObject,
  389. IOCTL_GENUSB_READ_WRITE_PIPE,
  390. &transfer, sizeof (GENUSB_READ_WRITE_PIPE),
  391. NULL, 0,
  392. &bytes, NULL);
  393. *ReturnedBufferLength = transfer.BufferLength;
  394. *UrbStatus= transfer.UrbStatus;
  395. return result;
  396. }