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.

581 lines
11 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. initunlo.c
  5. Abstract:
  6. This module contains the code that is very specific to initialization
  7. and unload operations in the irenum driver
  8. Author:
  9. Brian Lieuallen, 7-13-2000
  10. Environment:
  11. Kernel mode
  12. Revision History :
  13. --*/
  14. #include "internal.h"
  15. #define OBJECT_DIRECTORY L"\\DosDevices\\"
  16. #pragma alloc_text(PAGE, WaitForLowerDriverToCompleteIrp)
  17. NTSTATUS
  18. IoCompletionSetEvent(
  19. IN PDEVICE_OBJECT DeviceObject,
  20. IN PIRP Irp,
  21. IN PKEVENT pdoIoCompletedEvent
  22. )
  23. {
  24. #if DBG
  25. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
  26. UCHAR *Pnp="PnP";
  27. UCHAR *Power="Power";
  28. UCHAR *Create="Create";
  29. UCHAR *Close="Close";
  30. UCHAR *Other="Other";
  31. PUCHAR IrpType;
  32. switch(irpSp->MajorFunction) {
  33. case IRP_MJ_PNP:
  34. IrpType=Pnp;
  35. break;
  36. case IRP_MJ_CREATE:
  37. IrpType=Create;
  38. break;
  39. case IRP_MJ_CLOSE:
  40. IrpType=Close;
  41. break;
  42. default:
  43. IrpType=Other;
  44. break;
  45. }
  46. D_PNP(DbgPrint("IRCOMM: Setting event for %s wait, completed with %08lx\n",IrpType,Irp->IoStatus.Status);)
  47. #endif
  48. KeSetEvent(pdoIoCompletedEvent, IO_NO_INCREMENT, FALSE);
  49. return STATUS_MORE_PROCESSING_REQUIRED;
  50. }
  51. NTSTATUS
  52. WaitForLowerDriverToCompleteIrp(
  53. PDEVICE_OBJECT TargetDeviceObject,
  54. PIRP Irp,
  55. BOOLEAN CopyCurrentToNext
  56. )
  57. {
  58. NTSTATUS Status;
  59. KEVENT Event;
  60. #if DBG
  61. PIO_STACK_LOCATION IrpSp=IoGetCurrentIrpStackLocation(Irp);
  62. #endif
  63. KeInitializeEvent(
  64. &Event,
  65. NotificationEvent,
  66. FALSE
  67. );
  68. if (CopyCurrentToNext) {
  69. IoCopyCurrentIrpStackLocationToNext(Irp);
  70. }
  71. IoSetCompletionRoutine(
  72. Irp,
  73. IoCompletionSetEvent,
  74. &Event,
  75. TRUE,
  76. TRUE,
  77. TRUE
  78. );
  79. Status = IoCallDriver(TargetDeviceObject, Irp);
  80. if (Status == STATUS_PENDING) {
  81. D_TRACE(DbgPrint("IRCOMM: Waiting for PDO\n");)
  82. KeWaitForSingleObject(
  83. &Event,
  84. Executive,
  85. KernelMode,
  86. FALSE,
  87. NULL
  88. );
  89. }
  90. #if DBG
  91. ASSERT(IrpSp == IoGetCurrentIrpStackLocation(Irp));
  92. RtlZeroMemory(&Event,sizeof(Event));
  93. #endif
  94. return Irp->IoStatus.Status;
  95. }
  96. #if DBG
  97. NTSTATUS
  98. UnhandledPnpIrpCompletion(
  99. IN PDEVICE_OBJECT DeviceObject,
  100. IN PIRP Irp,
  101. IN PVOID Context
  102. )
  103. {
  104. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
  105. D_PNP(DbgPrint("IRCOMM: Forwarded IRP, MN func=%d, completed with %08lx\n",irpSp->MinorFunction,Irp->IoStatus.Status);)
  106. return STATUS_SUCCESS;
  107. }
  108. #endif
  109. NTSTATUS
  110. ForwardIrp(
  111. PDEVICE_OBJECT NextDevice,
  112. PIRP Irp
  113. )
  114. {
  115. #if DBG
  116. IoMarkIrpPending(Irp);
  117. IoCopyCurrentIrpStackLocationToNext(Irp);
  118. IoSetCompletionRoutine(
  119. Irp,
  120. UnhandledPnpIrpCompletion,
  121. NULL,
  122. TRUE,
  123. TRUE,
  124. TRUE
  125. );
  126. IoCallDriver(NextDevice, Irp);
  127. return STATUS_PENDING;
  128. #else
  129. IoSkipCurrentIrpStackLocation(Irp);
  130. return IoCallDriver(NextDevice, Irp);
  131. #endif
  132. }
  133. NTSTATUS
  134. GetRegistryKeyValue (
  135. IN PDEVICE_OBJECT Pdo,
  136. IN ULONG DevInstKeyType,
  137. IN PWCHAR KeyNameString,
  138. IN PVOID Data,
  139. IN ULONG DataLength
  140. )
  141. /*++
  142. Routine Description:
  143. Reads a registry key value from an already opened registry key.
  144. Arguments:
  145. Handle Handle to the opened registry key
  146. KeyNameString ANSI string to the desired key
  147. KeyNameStringLength Length of the KeyNameString
  148. Data Buffer to place the key value in
  149. DataLength Length of the data buffer
  150. Return Value:
  151. STATUS_SUCCESS if all works, otherwise status of system call that
  152. went wrong.
  153. --*/
  154. {
  155. UNICODE_STRING keyName;
  156. ULONG length;
  157. PKEY_VALUE_PARTIAL_INFORMATION PartialInfo;
  158. NTSTATUS ntStatus = STATUS_INSUFFICIENT_RESOURCES;
  159. HANDLE Handle;
  160. PAGED_CODE();
  161. ntStatus = IoOpenDeviceRegistryKey(
  162. Pdo,
  163. DevInstKeyType,
  164. STANDARD_RIGHTS_READ,
  165. &Handle
  166. );
  167. if (NT_SUCCESS(ntStatus)) {
  168. RtlInitUnicodeString (&keyName, KeyNameString);
  169. length = sizeof(KEY_VALUE_FULL_INFORMATION) + DataLength;
  170. PartialInfo = ALLOCATE_PAGED_POOL(length);
  171. if (PartialInfo) {
  172. ntStatus = ZwQueryValueKey (Handle,
  173. &keyName,
  174. KeyValuePartialInformation,
  175. PartialInfo,
  176. length,
  177. &length);
  178. if (NT_SUCCESS(ntStatus)) {
  179. //
  180. // If there is enough room in the data buffer, copy the output
  181. //
  182. if (DataLength >= PartialInfo->DataLength) {
  183. RtlCopyMemory (Data,
  184. PartialInfo->Data,
  185. PartialInfo->DataLength);
  186. } else {
  187. ntStatus=STATUS_BUFFER_TOO_SMALL;
  188. }
  189. } else {
  190. D_ERROR(DbgPrint("MODEM: could not query value, %08lx\n",ntStatus);)
  191. }
  192. FREE_POOL(PartialInfo);
  193. }
  194. ZwClose(Handle);
  195. } else {
  196. D_ERROR(DbgPrint("MODEM: could open device reg key, %08lx\n",ntStatus);)
  197. }
  198. return ntStatus;
  199. }
  200. NTSTATUS
  201. IrCommHandleSymbolicLink(
  202. PDEVICE_OBJECT Pdo,
  203. PUNICODE_STRING InterfaceName,
  204. BOOLEAN Create
  205. )
  206. {
  207. UNICODE_STRING SymbolicLink;
  208. UNICODE_STRING PdoName;
  209. ULONG StringLength;
  210. NTSTATUS Status;
  211. PdoName.Buffer=NULL;
  212. SymbolicLink.Buffer=NULL;
  213. SymbolicLink.Length=0;
  214. SymbolicLink.MaximumLength=sizeof(WCHAR)*256;
  215. SymbolicLink.Buffer=ALLOCATE_PAGED_POOL(SymbolicLink.MaximumLength+sizeof(WCHAR));
  216. if (SymbolicLink.Buffer == NULL) {
  217. return STATUS_INSUFFICIENT_RESOURCES;
  218. }
  219. RtlZeroMemory(
  220. SymbolicLink.Buffer,
  221. SymbolicLink.MaximumLength
  222. );
  223. RtlAppendUnicodeToString(
  224. &SymbolicLink,
  225. OBJECT_DIRECTORY
  226. );
  227. Status=GetRegistryKeyValue(
  228. Pdo,
  229. PLUGPLAY_REGKEY_DEVICE,
  230. L"PortName",
  231. SymbolicLink.Buffer+(SymbolicLink.Length/sizeof(WCHAR)),
  232. (ULONG)SymbolicLink.MaximumLength-SymbolicLink.Length
  233. );
  234. if (!NT_SUCCESS(Status)) {
  235. goto Exit;
  236. }
  237. SymbolicLink.Length=(USHORT)wcslen(SymbolicLink.Buffer)*sizeof(WCHAR);
  238. //
  239. // Get the PDO name so we can point the symbolic to that
  240. //
  241. PdoName.Length=0;
  242. PdoName.MaximumLength=sizeof(WCHAR)*256;
  243. PdoName.Buffer=ALLOCATE_PAGED_POOL(PdoName.MaximumLength+sizeof(WCHAR));
  244. if (PdoName.Buffer == NULL) {
  245. Status=STATUS_INSUFFICIENT_RESOURCES;
  246. goto Exit;
  247. }
  248. Status=IoGetDeviceProperty(
  249. Pdo,
  250. DevicePropertyPhysicalDeviceObjectName,
  251. (ULONG)PdoName.MaximumLength,
  252. PdoName.Buffer,
  253. &StringLength
  254. );
  255. if (!NT_SUCCESS(Status)) {
  256. goto Exit;
  257. }
  258. PdoName.Length+=(USHORT)StringLength-sizeof(UNICODE_NULL);
  259. if (Create) {
  260. Status=IoCreateSymbolicLink(
  261. &SymbolicLink,
  262. &PdoName
  263. );
  264. if (!NT_SUCCESS(Status)) {
  265. D_ERROR(DbgPrint("IRCOMM: IoCreateSymbolicLink() failed %08lx\n",Status);)
  266. } else {
  267. Status = RtlWriteRegistryValue(
  268. RTL_REGISTRY_DEVICEMAP,
  269. L"SERIALCOMM",
  270. PdoName.Buffer,
  271. REG_SZ,
  272. SymbolicLink.Buffer+((sizeof(OBJECT_DIRECTORY)-sizeof(UNICODE_NULL))/sizeof(WCHAR)),
  273. SymbolicLink.Length - (sizeof(OBJECT_DIRECTORY) - sizeof(UNICODE_NULL))
  274. );
  275. }
  276. Status=IoRegisterDeviceInterface(
  277. Pdo,
  278. &GUID_CLASS_COMPORT,
  279. NULL,
  280. InterfaceName
  281. );
  282. if (NT_SUCCESS(Status)) {
  283. IoSetDeviceInterfaceState(
  284. InterfaceName,
  285. TRUE
  286. );
  287. } else {
  288. D_ERROR(DbgPrint("IRCOMM: IoRegisterDeviceInterface() failed %08lx\n",Status);)
  289. }
  290. } else {
  291. if (InterfaceName->Buffer != NULL) {
  292. IoSetDeviceInterfaceState(
  293. InterfaceName,
  294. FALSE
  295. );
  296. RtlFreeUnicodeString(
  297. InterfaceName
  298. );
  299. }
  300. RtlDeleteRegistryValue(
  301. RTL_REGISTRY_DEVICEMAP,
  302. L"SERIALCOMM",
  303. PdoName.Buffer
  304. );
  305. Status=IoDeleteSymbolicLink(
  306. &SymbolicLink
  307. );
  308. }
  309. Exit:
  310. if ( SymbolicLink.Buffer != NULL) {
  311. FREE_POOL(SymbolicLink.Buffer);
  312. }
  313. if (PdoName.Buffer != NULL) {
  314. FREE_POOL(PdoName.Buffer);
  315. }
  316. return Status;
  317. }
  318. NTSTATUS
  319. QueryPdoInformation(
  320. PDEVICE_OBJECT Pdo,
  321. ULONG InformationType,
  322. PVOID Buffer,
  323. ULONG BufferLength
  324. )
  325. {
  326. PDEVICE_OBJECT deviceObject=Pdo;
  327. PIRP irp;
  328. PIO_STACK_LOCATION irpSp;
  329. NTSTATUS Status;
  330. //
  331. // Get a pointer to the topmost device object in the stack of devices,
  332. // beginning with the deviceObject.
  333. //
  334. while (deviceObject->AttachedDevice) {
  335. deviceObject = deviceObject->AttachedDevice;
  336. }
  337. //
  338. // Begin by allocating the IRP for this request. Do not charge quota to
  339. // the current process for this IRP.
  340. //
  341. irp = IoAllocateIrp(deviceObject->StackSize+1, FALSE);
  342. if (irp == NULL){
  343. return STATUS_INSUFFICIENT_RESOURCES;
  344. }
  345. #if DBG
  346. IoSetNextIrpStackLocation(irp);
  347. irpSp = IoGetCurrentIrpStackLocation(irp);
  348. irpSp->MajorFunction=IRP_MJ_PNP;
  349. irpSp->MinorFunction=IRP_MN_READ_CONFIG;
  350. #endif
  351. irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
  352. irp->IoStatus.Information = 0;
  353. //
  354. // Get a pointer to the stack location of the first driver which will be
  355. // invoked. This is where the function codes and parameters are set.
  356. //
  357. irpSp = IoGetNextIrpStackLocation(irp);
  358. irpSp->MajorFunction=IRP_MJ_PNP;
  359. irpSp->MinorFunction=IRP_MN_READ_CONFIG;
  360. irpSp->Parameters.ReadWriteConfig.WhichSpace=InformationType;
  361. irpSp->Parameters.ReadWriteConfig.Offset=0;
  362. irpSp->Parameters.ReadWriteConfig.Buffer=Buffer;
  363. irpSp->Parameters.ReadWriteConfig.Length=BufferLength;
  364. Status=WaitForLowerDriverToCompleteIrp(
  365. deviceObject,
  366. irp,
  367. LEAVE_NEXT_AS_IS
  368. );
  369. IoFreeIrp(irp);
  370. return Status;
  371. }
  372. #if 0
  373. VOID
  374. DumpBuffer(
  375. PUCHAR Data,
  376. ULONG Length
  377. )
  378. {
  379. ULONG i;
  380. for (i=0; i<Length; i++) {
  381. DbgPrint("%02x ",*Data);
  382. Data++;
  383. if (((i & 0xf) == 0) && (i != 0)) {
  384. DbgPrint("\n");
  385. }
  386. }
  387. return;
  388. }
  389. #endif