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.

1979 lines
57 KiB

  1. /*++
  2. Module Name:
  3. PNP.C
  4. Abstract:
  5. This module contains contains the plugplay calls
  6. PNP / WDM BUS driver.
  7. Environment:
  8. kernel mode only
  9. Notes:
  10. Revision History:
  11. --*/
  12. #include <ntddk.h>
  13. #include <wdmguid.h>
  14. #include <ntddser.h>
  15. #include <initguid.h>
  16. #include "mxenum.h"
  17. #include "mxlog.h"
  18. static const PHYSICAL_ADDRESS SerialPhysicalZero = {0};
  19. #ifdef ALLOC_PRAGMA
  20. #pragma alloc_text (PAGE, MxenumAddDevice)
  21. #pragma alloc_text (PAGE, MxenumPnPDispatch)
  22. #pragma alloc_text (PAGE, MxenumFdoPnP)
  23. #pragma alloc_text (PAGE, MxenumPdoPnP)
  24. #pragma alloc_text (PAGE, MxenumPnPRemovePDOs)
  25. #pragma alloc_text (PAGE, MxenumGetBoardType)
  26. #endif
  27. NTSTATUS
  28. MxenumGetBoardType(IN PDEVICE_OBJECT devObject,OUT PULONG boardType)
  29. {
  30. ULONG i,j;
  31. UNICODE_STRING buffer;
  32. NTSTATUS status;
  33. PAGED_CODE();
  34. status = IoGetDeviceProperty (devObject,
  35. DevicePropertyHardwareID,
  36. 0,
  37. NULL,
  38. (PLONG)&buffer.Length
  39. );
  40. if (buffer.Length < sizeof(L"mx1000")) {
  41. return (STATUS_FAIL_CHECK);
  42. }
  43. buffer.Buffer = NULL;
  44. buffer.MaximumLength = buffer.Length + sizeof(WCHAR);
  45. buffer.Buffer = ExAllocatePool (PagedPool, buffer.MaximumLength);
  46. if (NULL == buffer.Buffer) {
  47. return (STATUS_INSUFFICIENT_RESOURCES);
  48. }
  49. status = IoGetDeviceProperty (devObject,
  50. DevicePropertyHardwareID,
  51. buffer.Length,
  52. buffer.Buffer,
  53. (PULONG)&buffer.Length
  54. );
  55. if (!(NT_SUCCESS(status))) {
  56. ExFreePool(buffer.Buffer);
  57. return (status);
  58. }
  59. if (buffer.Length < sizeof(L"mx1000")) {
  60. ExFreePool(buffer.Buffer);
  61. return (STATUS_FAIL_CHECK);
  62. }
  63. status = STATUS_FAIL_CHECK;
  64. if (((buffer.Buffer[0] == 'm')||(buffer.Buffer[0] == 'M'))&&
  65. ((buffer.Buffer[1] == 'x')||(buffer.Buffer[1] == 'X'))&&
  66. (buffer.Buffer[2] == '1')&&
  67. (buffer.Buffer[3] == '0')&&
  68. (buffer.Buffer[4] == '0')) {
  69. switch ((UCHAR)buffer.Buffer[5]) {
  70. case '0' :
  71. *boardType = C218ISA;
  72. status = STATUS_SUCCESS;
  73. break;
  74. case '1' :
  75. *boardType = C320ISA;
  76. status = STATUS_SUCCESS;
  77. break;
  78. case '2' :
  79. *boardType = C320ISA;
  80. status = STATUS_SUCCESS;
  81. break;
  82. case '3' :
  83. *boardType = C320ISA;
  84. status = STATUS_SUCCESS;
  85. break;
  86. case '4' :
  87. *boardType = C320ISA;
  88. status = STATUS_SUCCESS;
  89. break;
  90. }
  91. }
  92. else if (buffer.Length >= sizeof(L"pci\\ven_1393&dev_2180")) {
  93. if (((buffer.Buffer[0] == 'p')||(buffer.Buffer[0] == 'P'))&&
  94. ((buffer.Buffer[1] == 'c')||(buffer.Buffer[1] == 'C'))&&
  95. ((buffer.Buffer[2] == 'i')||(buffer.Buffer[2] == 'I'))) {
  96. if ((buffer.Buffer[17] == '2')&&
  97. (buffer.Buffer[18] == '1')&&
  98. (buffer.Buffer[19] == '8')&&
  99. (buffer.Buffer[20] == '0') ) {
  100. *boardType = C218PCI;
  101. status = STATUS_SUCCESS;
  102. } else if ((buffer.Buffer[17] == '3')&&
  103. (buffer.Buffer[18] == '2')&&
  104. (buffer.Buffer[19] == '0')&&
  105. (buffer.Buffer[20] == '0') ) {
  106. *boardType = C320PCI;
  107. status = STATUS_SUCCESS;
  108. } else if ((buffer.Buffer[17] == '2')&&
  109. (buffer.Buffer[18] == '0')&&
  110. (buffer.Buffer[19] == '4')&&
  111. (buffer.Buffer[20] == '0') ) {
  112. *boardType = CP204J;
  113. status = STATUS_SUCCESS;
  114. }
  115. }
  116. }
  117. MxenumKdPrint (MXENUM_DBG_TRACE, ("BoardType: %x\n", *boardType));
  118. ExFreePool(buffer.Buffer);
  119. return (status);
  120. }
  121. NTSTATUS
  122. MxenumUpdateNumberPortRegistry(IN PDEVICE_OBJECT DeviceObject)
  123. {
  124. UNICODE_STRING buffer,registryPath;
  125. NTSTATUS status;
  126. status = IoGetDeviceProperty (((PFDO_DEVICE_DATA)(DeviceObject->DeviceExtension))->UnderlyingPDO,
  127. DevicePropertyDriverKeyName,
  128. 0,
  129. NULL,
  130. (PULONG)&buffer.Length);
  131. buffer.Buffer = NULL;
  132. buffer.MaximumLength = buffer.Length + sizeof(WCHAR);
  133. buffer.Buffer = ExAllocatePool (PagedPool, buffer.MaximumLength);
  134. registryPath.Buffer = NULL;
  135. registryPath.Length = 0;
  136. registryPath.MaximumLength = sizeof(REGISTRY_CLASS) +
  137. buffer.Length +
  138. sizeof(L"\\Parameters") +
  139. sizeof(WCHAR);
  140. registryPath.Buffer = ExAllocatePool (PagedPool, registryPath.MaximumLength);
  141. if ((registryPath.Buffer == NULL)||(buffer.Buffer == NULL)) {
  142. if (registryPath.Buffer != NULL)
  143. ExFreePool(registryPath.Buffer);
  144. if (buffer.Buffer == NULL)
  145. ExFreePool(buffer.Buffer);
  146. status = STATUS_INSUFFICIENT_RESOURCES;
  147. return(status);
  148. }
  149. status = IoGetDeviceProperty (((PFDO_DEVICE_DATA)(DeviceObject->DeviceExtension))->UnderlyingPDO,
  150. DevicePropertyDriverKeyName,
  151. buffer.Length,
  152. buffer.Buffer,
  153. (PULONG)&buffer.Length);
  154. if (!(NT_SUCCESS(status))) {
  155. if (registryPath.Buffer != NULL)
  156. ExFreePool(registryPath.Buffer);
  157. if (buffer.Buffer == NULL)
  158. ExFreePool(buffer.Buffer);
  159. return(status);
  160. }
  161. buffer.Length -= sizeof(WCHAR); // remove the null char.
  162. RtlZeroMemory(
  163. registryPath.Buffer,
  164. registryPath.MaximumLength
  165. );
  166. RtlAppendUnicodeToString(
  167. &registryPath,
  168. REGISTRY_CLASS
  169. );
  170. RtlAppendUnicodeStringToString(
  171. &registryPath,
  172. &buffer
  173. );
  174. RtlAppendUnicodeToString(
  175. &registryPath,
  176. L"\\Parameters"
  177. );
  178. registryPath.Buffer[(registryPath.Length >> 1)] = (WCHAR)0;
  179. status = RtlWriteRegistryValue( RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL,
  180. registryPath.Buffer,
  181. L"NumPorts",
  182. REG_DWORD,
  183. &((PFDO_DEVICE_DATA)(DeviceObject->DeviceExtension))->NumPorts,
  184. sizeof(ULONG));
  185. ExFreePool(buffer.Buffer);
  186. ExFreePool(registryPath.Buffer);
  187. return (status);
  188. }
  189. NTSTATUS
  190. MxenumAddDevice(
  191. IN PDRIVER_OBJECT DriverObject,
  192. IN PDEVICE_OBJECT BusPhysicalDeviceObject
  193. )
  194. /*++
  195. Routine Description.
  196. A bus has been found. Attach our FDO to it.
  197. Allocate any required resources. Set things up. And be prepared for the
  198. first ``start device.''
  199. Arguments:
  200. BusPhysicalDeviceObject - Device object representing the bus. That to which
  201. we attach a new FDO.
  202. DriverObject - This very self referenced driver.
  203. --*/
  204. {
  205. NTSTATUS status;
  206. PDEVICE_OBJECT deviceObject;
  207. PFDO_DEVICE_DATA DeviceData;
  208. ULONG nameLength,i;
  209. ULONG boardIndex = 0;
  210. PCM_RESOURCE_LIST resourceList = NULL;
  211. UNICODE_STRING buffer,registryPath;
  212. RTL_QUERY_REGISTRY_TABLE paramTable[2];
  213. PAGED_CODE ();
  214. MxenumKdPrint (MXENUM_DBG_TRACE,("Add Device,cnt = %d\n",
  215. NumBoardInstalled + 1));
  216. if (NumBoardInstalled >= MAX_BOARD) {
  217. MxenumKdPrint (MXENUM_DBG_TRACE,("Too many board installed,abort this\n"));
  218. status = STATUS_INSUFFICIENT_RESOURCES;
  219. return(status);
  220. }
  221. //
  222. //First, Create our FDO
  223. //
  224. status = IoCreateDevice (
  225. DriverObject, // our driver object
  226. sizeof (FDO_DEVICE_DATA), // device object extension size
  227. NULL, // FDOs do not have names
  228. FILE_DEVICE_BUS_EXTENDER,
  229. 0, // No special characteristics
  230. TRUE, // our FDO is exclusive
  231. &deviceObject); // The device object created
  232. if (!(NT_SUCCESS(status))) {
  233. MxenumLogError(
  234. DriverObject,
  235. NULL,
  236. SerialPhysicalZero,
  237. SerialPhysicalZero,
  238. 0,
  239. 0,
  240. 0,
  241. 19,
  242. STATUS_SUCCESS,
  243. MXENUM_INSUFFICIENT_RESOURCES,
  244. 0,
  245. NULL,
  246. 0,
  247. NULL
  248. );
  249. MxenumKdPrint (MXENUM_DBG_TRACE,("Unable to create device status=%x\n",status));
  250. return status;
  251. }
  252. DeviceData = (PFDO_DEVICE_DATA) deviceObject->DeviceExtension;
  253. RtlFillMemory (DeviceData, sizeof (FDO_DEVICE_DATA), 0);
  254. status=
  255. MxenumGetBoardType(BusPhysicalDeviceObject,
  256. &DeviceData->BoardType);
  257. if (!NT_SUCCESS(status) || (DeviceData->BoardType > MOXA_MAX_BOARD_TYPE)) {
  258. MxenumLogError(
  259. DriverObject,
  260. NULL,
  261. SerialPhysicalZero,
  262. SerialPhysicalZero,
  263. 0,
  264. 0,
  265. 0,
  266. 19,
  267. STATUS_SUCCESS,
  268. MXENUM_NOT_INTELLIO_BOARDS,
  269. 0,
  270. NULL,
  271. 0,
  272. NULL
  273. );
  274. IoDeleteDevice (deviceObject);
  275. status = STATUS_INSUFFICIENT_RESOURCES;
  276. return(status);
  277. }
  278. status = IoGetDeviceProperty (BusPhysicalDeviceObject,
  279. DevicePropertyDriverKeyName,
  280. 0,
  281. NULL,
  282. (PULONG)&buffer.Length);
  283. buffer.Buffer = NULL;
  284. buffer.MaximumLength = buffer.Length + sizeof(WCHAR);
  285. buffer.Buffer = ExAllocatePool (PagedPool, buffer.MaximumLength);
  286. registryPath.Buffer = NULL;
  287. registryPath.Length = 0;
  288. registryPath.MaximumLength = sizeof(REGISTRY_CLASS) +
  289. buffer.Length +
  290. sizeof(L"\\Parameters") +
  291. sizeof(WCHAR);
  292. registryPath.Buffer = ExAllocatePool (PagedPool, registryPath.MaximumLength);
  293. if ((registryPath.Buffer == NULL)||(buffer.Buffer == NULL)) {
  294. IoDeleteDevice (deviceObject);
  295. MxenumLogError(
  296. DriverObject,
  297. NULL,
  298. SerialPhysicalZero,
  299. SerialPhysicalZero,
  300. 0,
  301. 0,
  302. 0,
  303. 19,
  304. STATUS_SUCCESS,
  305. MXENUM_INSUFFICIENT_RESOURCES,
  306. 0,
  307. NULL,
  308. 0,
  309. NULL
  310. );
  311. if (registryPath.Buffer != NULL)
  312. ExFreePool(registryPath.Buffer);
  313. if (buffer.Buffer == NULL)
  314. ExFreePool(buffer.Buffer);
  315. status = STATUS_INSUFFICIENT_RESOURCES;
  316. return(status);
  317. }
  318. status = IoGetDeviceProperty (BusPhysicalDeviceObject,
  319. DevicePropertyDriverKeyName,
  320. buffer.Length,
  321. buffer.Buffer,
  322. (PULONG)&buffer.Length);
  323. if (!(NT_SUCCESS(status))) {
  324. IoDeleteDevice (deviceObject);
  325. MxenumLogError(
  326. DriverObject,
  327. NULL,
  328. SerialPhysicalZero,
  329. SerialPhysicalZero,
  330. 0,
  331. 0,
  332. 0,
  333. 19,
  334. STATUS_SUCCESS,
  335. MXENUM_INSUFFICIENT_RESOURCES,
  336. 0,
  337. NULL,
  338. 0,
  339. NULL
  340. );
  341. if (registryPath.Buffer != NULL)
  342. ExFreePool(registryPath.Buffer);
  343. if (buffer.Buffer == NULL)
  344. ExFreePool(buffer.Buffer);
  345. return(status);
  346. }
  347. buffer.Length -= sizeof(WCHAR); // remove the null char.
  348. buffer.Length >>= 1;
  349. boardIndex = (buffer.Buffer[buffer.Length-1] - '0') +
  350. (buffer.Buffer[buffer.Length-2] - '0')*10 +
  351. (buffer.Buffer[buffer.Length-3] - '0')*100 +
  352. (buffer.Buffer[buffer.Length-4] - '0')*1000;
  353. buffer.Length <<= 1;
  354. RtlZeroMemory(
  355. registryPath.Buffer,
  356. registryPath.MaximumLength
  357. );
  358. RtlAppendUnicodeToString(
  359. &registryPath,
  360. REGISTRY_CLASS
  361. );
  362. RtlAppendUnicodeStringToString(
  363. &registryPath,
  364. &buffer
  365. );
  366. RtlAppendUnicodeToString(
  367. &registryPath,
  368. L"\\Parameters"
  369. );
  370. registryPath.Buffer[(registryPath.Length >> 1)] = (WCHAR)0;
  371. RtlZeroMemory (&paramTable[0], sizeof(paramTable));
  372. paramTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  373. paramTable[0].Name = L"NumPorts";
  374. paramTable[0].EntryContext = &DeviceData->NumPorts;
  375. paramTable[0].DefaultType = REG_DWORD;
  376. paramTable[0].DefaultData = &DeviceData->NumPorts;
  377. paramTable[0].DefaultLength = sizeof(ULONG);
  378. status = RtlQueryRegistryValues( RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL,
  379. registryPath.Buffer,
  380. &paramTable[0],
  381. NULL,
  382. NULL);
  383. MxenumKdPrint (MXENUM_DBG_TRACE,("parameter path -> %ws\n",
  384. registryPath.Buffer));
  385. if (!NT_SUCCESS(status) || (DeviceData->NumPorts <= 0) || (DeviceData->NumPorts > MAXPORT_PER_CARD) ) {
  386. MxenumKdPrint (MXENUM_DBG_TRACE,("parameter path -> %ws\n",
  387. registryPath.Buffer));
  388. MxenumKdPrint (MXENUM_DBG_TRACE,("NumPorts= %d,status=%x\n",
  389. DeviceData->NumPorts,status));
  390. MxenumKdPrint (MXENUM_DBG_TRACE,("This is the first time installation\n"));
  391. switch (DeviceData->BoardType) {
  392. case C218ISA:
  393. case C218PCI:
  394. DeviceData->NumPorts = 8;
  395. break;
  396. case CP204J:
  397. DeviceData->NumPorts = 4;
  398. break;
  399. case C320ISA:
  400. case C320PCI:
  401. default :
  402. DeviceData->NumPorts = 0;
  403. break;
  404. }
  405. }
  406. ExFreePool(buffer.Buffer);
  407. ExFreePool(registryPath.Buffer);
  408. status = IoGetDeviceProperty (BusPhysicalDeviceObject,
  409. DevicePropertyLegacyBusType,
  410. sizeof(ULONG),
  411. &DeviceData->InterfaceType,
  412. (PULONG)&buffer.Length);
  413. if (!(NT_SUCCESS(status))) {
  414. DeviceData->InterfaceType = (ULONG)Isa;
  415. DeviceData->BusNumber = 0;
  416. }
  417. DeviceData->IsFDO = TRUE;
  418. DeviceData->Self = deviceObject;
  419. DeviceData->AttachedPDO = NULL;
  420. DeviceData->NumPDOs = 0;
  421. DeviceData->PDOWasExposed = FALSE;
  422. DeviceData->DeviceState = PowerDeviceD0;
  423. DeviceData->SystemState = PowerSystemWorking;
  424. DeviceData->LastSetPowerState = PowerDeviceD0;
  425. DeviceData->PDOForcedRemove = FALSE;
  426. DeviceData->BoardIndex = boardIndex;
  427. DeviceData->Removed = FALSE;
  428. // Set the PDO for use with PlugPlay functions
  429. DeviceData->UnderlyingPDO = BusPhysicalDeviceObject;
  430. //
  431. // Attach our filter driver to the device stack.
  432. // the return value of IoAttachDeviceToDeviceStack is the top of the
  433. // attachment chain. This is where all the IRPs should be routed.
  434. //
  435. // Our filter will send IRPs to the top of the stack and use the PDO
  436. // for all PlugPlay functions.
  437. //
  438. DeviceData->TopOfStack = IoAttachDeviceToDeviceStack (
  439. deviceObject,
  440. BusPhysicalDeviceObject);
  441. // Bias outstanding request to 1 so that we can look for a
  442. // transition to zero when processing the remove device PlugPlay IRP.
  443. DeviceData->OutstandingIO = 1;
  444. KeInitializeEvent(&DeviceData->RemoveEvent,
  445. SynchronizationEvent,
  446. FALSE); // initialized to not signalled
  447. deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
  448. deviceObject->Flags |= DO_POWER_PAGABLE;
  449. //
  450. // Tell the PlugPlay system that this device will need an interface
  451. // device class shingle.
  452. //
  453. // It may be that the driver cannot hang the shingle until it starts
  454. // the device itself, so that it can query some of its properties.
  455. // (Aka the shingles guid (or ref string) is based on the properties
  456. // of the device.)
  457. //
  458. status = IoRegisterDeviceInterface (
  459. BusPhysicalDeviceObject,
  460. (LPGUID) &GUID_SERENUM_BUS_ENUMERATOR,
  461. NULL, // No ref string
  462. &DeviceData->DevClassAssocName);
  463. if (!NT_SUCCESS (status)) {
  464. MxenumKdPrint (MXENUM_DBG_TRACE,
  465. ("AddDevice: IoRegisterDCA failed (%x)", status));
  466. IoDetachDevice (DeviceData->TopOfStack);
  467. IoDeleteDevice (deviceObject);
  468. return status;
  469. }
  470. //
  471. // If for any reason you need to save values in a safe location that
  472. // clients of this DeviceClassAssociate might be interested in reading
  473. // here is the time to do so, with the function
  474. // IoOpenDeviceClassRegistryKey
  475. // the symbolic link name used is was returned in
  476. // DeviceData->DevClassAssocName (the same name which is returned by
  477. // IoGetDeviceClassAssociations and the SetupAPI equivs.
  478. //
  479. //
  480. // Turn on the shingle and point it to the given device object.
  481. //
  482. status = IoSetDeviceInterfaceState (
  483. &DeviceData->DevClassAssocName,
  484. TRUE);
  485. if (!NT_SUCCESS (status)) {
  486. IoDetachDevice (DeviceData->TopOfStack);
  487. IoDeleteDevice (deviceObject);
  488. return status;
  489. }
  490. else
  491. NumBoardInstalled++;
  492. return status;
  493. }
  494. NTSTATUS
  495. MxenumFdoPnPComplete (
  496. IN PDEVICE_OBJECT DeviceObject,
  497. IN PIRP Pirp,
  498. IN PVOID Context
  499. );
  500. NTSTATUS
  501. MxenumPnPDispatch (
  502. IN PDEVICE_OBJECT DeviceObject,
  503. IN PIRP Irp
  504. )
  505. /*++
  506. Routine Description:
  507. Answer the plethora of Irp Major PnP IRPS.
  508. --*/
  509. {
  510. PIO_STACK_LOCATION irpStack;
  511. NTSTATUS status;
  512. PCOMMON_DEVICE_DATA commonData;
  513. KIRQL oldIrq;
  514. PAGED_CODE ();
  515. irpStack = IoGetCurrentIrpStackLocation (Irp);
  516. // ASSERT (IRP_MJ_PNP == irpStack->MajorFunction);
  517. commonData = (PCOMMON_DEVICE_DATA) DeviceObject->DeviceExtension;
  518. if (commonData->IsFDO) {
  519. // MxenumKdPrint (MXENUM_DBG_TRACE,
  520. // ("PNP: Functional DO: %x IRP: %x\n", DeviceObject, Irp));
  521. status = MxenumFdoPnP (
  522. DeviceObject,
  523. Irp,
  524. irpStack,
  525. (PFDO_DEVICE_DATA) commonData);
  526. } else {
  527. // MxenumKdPrint (MXENUM_DBG_TRACE,
  528. // ("PNP: Physical DO: %x IRP: %x\n", DeviceObject, Irp));
  529. status = MxenumPdoPnP (
  530. DeviceObject,
  531. Irp,
  532. irpStack,
  533. (PPDO_DEVICE_DATA) commonData);
  534. }
  535. return status;
  536. }
  537. NTSTATUS
  538. MxenumFdoPnP (
  539. IN PDEVICE_OBJECT DeviceObject,
  540. IN PIRP Irp,
  541. IN PIO_STACK_LOCATION IrpStack,
  542. IN PFDO_DEVICE_DATA DeviceData
  543. )
  544. /*++
  545. Routine Description:
  546. Handle requests from the PlugPlay system for the BUS itself
  547. NB: the various Minor functions of the PlugPlay system will not be
  548. overlapped and do not have to be reentrant
  549. --*/
  550. {
  551. NTSTATUS status;
  552. KIRQL oldIrq;
  553. KEVENT event;
  554. ULONG length;
  555. ULONG i;
  556. PLIST_ENTRY entry;
  557. PPDO_DEVICE_DATA pdoData;
  558. PDEVICE_RELATIONS relations;
  559. PRTL_QUERY_REGISTRY_TABLE QueryTable = NULL;
  560. PAGED_CODE ();
  561. status = MxenumIncIoCount (DeviceData);
  562. if (!NT_SUCCESS (status)) {
  563. Irp->IoStatus.Information = 0;
  564. Irp->IoStatus.Status = status;
  565. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  566. return status;
  567. }
  568. switch (IrpStack->MinorFunction) {
  569. case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
  570. {
  571. HANDLE pnpKey;
  572. PKEVENT pResFiltEvent;
  573. PIO_RESOURCE_REQUIREMENTS_LIST pReqList;
  574. PIO_RESOURCE_LIST pResList;
  575. PIO_RESOURCE_DESCRIPTOR pResDesc;
  576. ULONG reqCnt;
  577. ULONG listNum;
  578. MxenumKdPrint (MXENUM_DBG_TRACE,("FDO:Query Resource Requirement\n"));
  579. pResFiltEvent = ExAllocatePool(NonPagedPool, sizeof(KEVENT));
  580. if (pResFiltEvent == NULL) {
  581. Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
  582. IoCompleteRequest( Irp, IO_NO_INCREMENT);
  583. MxenumDecIoCount (DeviceData);
  584. return STATUS_INSUFFICIENT_RESOURCES;
  585. }
  586. KeInitializeEvent(pResFiltEvent, SynchronizationEvent, FALSE);
  587. IoCopyCurrentIrpStackLocationToNext(Irp);
  588. IoSetCompletionRoutine(Irp, MxenumFdoPnPComplete, pResFiltEvent,
  589. TRUE, TRUE, TRUE);
  590. status = IoCallDriver(DeviceData->TopOfStack, Irp);
  591. //
  592. // Wait for lower drivers to be done with the Irp
  593. //
  594. if (status == STATUS_PENDING) {
  595. KeWaitForSingleObject (pResFiltEvent, Executive, KernelMode, FALSE,
  596. NULL);
  597. }
  598. ExFreePool(pResFiltEvent);
  599. if (NT_SUCCESS(Irp->IoStatus.Status)&&
  600. ((pReqList=(PIO_RESOURCE_REQUIREMENTS_LIST)Irp->IoStatus.Information) != NULL)) {
  601. MxenumKdPrint (MXENUM_DBG_TRACE,
  602. ("ResourceRequireList:%x,%x,%x,%x,%x\n",
  603. pReqList->ListSize,
  604. pReqList->InterfaceType,
  605. pReqList->BusNumber,
  606. pReqList->SlotNumber,
  607. pReqList->AlternativeLists)
  608. );
  609. pResList = &pReqList->List[0];
  610. MxenumKdPrint (MXENUM_DBG_TRACE,
  611. ("ResourceList:%x,%x,%x\n",
  612. pResList->Version,
  613. pResList->Revision,
  614. pResList->Count)
  615. );
  616. for (i =0; i < pResList->Count; i++) {
  617. pResDesc = &pResList->Descriptors[i];
  618. MxenumKdPrint (MXENUM_DBG_TRACE,
  619. ("ResourceDesc:%x,%x,%x,%x\n",
  620. pResDesc->Option,
  621. pResDesc->Type,
  622. pResDesc->ShareDisposition,
  623. pResDesc->Flags)
  624. );
  625. switch (pResDesc->Type) {
  626. case CmResourceTypePort :
  627. MxenumKdPrint (MXENUM_DBG_TRACE,
  628. ("Port:%x,%x,%x,%x,%x\n",
  629. pResDesc->u.Port.Length,
  630. pResDesc->u.Port.MinimumAddress.HighPart,
  631. pResDesc->u.Port.MinimumAddress.LowPart,
  632. pResDesc->u.Port.MaximumAddress.HighPart,
  633. pResDesc->u.Port.MaximumAddress.LowPart)
  634. );
  635. break;
  636. case CmResourceTypeInterrupt :
  637. MxenumKdPrint (MXENUM_DBG_TRACE,
  638. ("Interrupt:%x,%x\n",
  639. pResDesc->u.Interrupt.MinimumVector,
  640. pResDesc->u.Interrupt.MaximumVector)
  641. );
  642. break;
  643. case CmResourceTypeMemory :
  644. MxenumKdPrint (MXENUM_DBG_TRACE,
  645. ("Memory:%x,%x,%x,%x,%x\n",
  646. pResDesc->u.Memory.Length,
  647. pResDesc->u.Memory.MinimumAddress.HighPart,
  648. pResDesc->u.Memory.MinimumAddress.LowPart,
  649. pResDesc->u.Memory.MaximumAddress.HighPart,
  650. pResDesc->u.Memory.MaximumAddress.LowPart)
  651. );
  652. break;
  653. case CmResourceTypeBusNumber :
  654. MxenumKdPrint (MXENUM_DBG_TRACE,
  655. ("BusNumber:%x,%x,%x\n",
  656. pResDesc->u.BusNumber.Length,
  657. pResDesc->u.BusNumber.MinBusNumber,
  658. pResDesc->u.BusNumber.MaxBusNumber)
  659. );
  660. break;
  661. default :
  662. break;
  663. }
  664. }
  665. }
  666. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  667. MxenumDecIoCount (DeviceData);
  668. return status;
  669. }
  670. case IRP_MN_START_DEVICE:
  671. {
  672. ULONG addressSpace = 0;
  673. PHYSICAL_ADDRESS translatedAddress;
  674. BOOLEAN NumPortDefined;
  675. MxenumKdPrint (MXENUM_DBG_TRACE,("FDO:Start Device\n"));
  676. if (DeviceData->Started) {
  677. MxenumKdPrint (MXENUM_DBG_TRACE,
  678. ("Device already started\n"));
  679. status = STATUS_SUCCESS;
  680. break;
  681. }
  682. //
  683. // BEFORE you are allowed to ``touch'' the device object to which
  684. // the FDO is attached (that send an irp from the bus to the Device
  685. // object to which the bus is attached). You must first pass down
  686. // the start IRP. It might not be powered on, or able to access or
  687. // something.
  688. //
  689. KeInitializeEvent (&event, NotificationEvent, FALSE);
  690. IoCopyCurrentIrpStackLocationToNext (Irp);
  691. IoSetCompletionRoutine (Irp,
  692. MxenumFdoPnPComplete,
  693. &event,
  694. TRUE,
  695. TRUE,
  696. TRUE);
  697. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  698. if (STATUS_PENDING == status) {
  699. // wait for it...
  700. status = KeWaitForSingleObject (&event,
  701. Executive,
  702. KernelMode,
  703. FALSE, // Not allertable
  704. NULL); // No timeout structure
  705. // ASSERT (STATUS_SUCCESS == status);
  706. status = Irp->IoStatus.Status;
  707. }
  708. if (NT_SUCCESS(status)) {
  709. PCM_PARTIAL_RESOURCE_LIST list;
  710. ULONG nres;
  711. PCM_PARTIAL_RESOURCE_DESCRIPTOR resource;
  712. if (!IrpStack->Parameters.StartDevice.AllocatedResources) {
  713. DeviceData->Started = FALSE;
  714. status = STATUS_UNSUCCESSFUL;
  715. break;
  716. }
  717. list = &IrpStack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList;
  718. nres = list->Count;
  719. resource = list->PartialDescriptors;
  720. DeviceData->InterfaceType = IrpStack->Parameters.StartDevice.AllocatedResources->List[0].InterfaceType;
  721. DeviceData->BusNumber = IrpStack->Parameters.StartDevice.AllocatedResources->List[0].BusNumber;
  722. for (i = 0; i < nres; ++i,++resource) {
  723. switch(resource->Type) {
  724. case CmResourceTypePort:
  725. {
  726. DeviceData->OriginalAckPort = resource->u.Port.Start;
  727. DeviceData->OriginalAckPort.LowPart += 0x4D;
  728. addressSpace = CM_RESOURCE_PORT_IO;
  729. status = HalTranslateBusAddress(
  730. DeviceData->InterfaceType,
  731. DeviceData->BusNumber,
  732. DeviceData->OriginalAckPort,
  733. &addressSpace,
  734. &translatedAddress
  735. );
  736. if (!NT_SUCCESS(status)) {
  737. MxenumKdPrint (MXENUM_DBG_TRACE,("FDO:translate io error!\n"));
  738. }
  739. DeviceData->AckPort = (PUCHAR)translatedAddress.LowPart;
  740. break;
  741. }
  742. case CmResourceTypeMemory:
  743. {
  744. DeviceData->OriginalBaseAddress = resource->u.Memory.Start;
  745. MxenumKdPrint (MXENUM_DBG_TRACE,("BaseAddr=%x\n",DeviceData->OriginalBaseAddress.LowPart));
  746. addressSpace = CM_RESOURCE_PORT_MEMORY;
  747. HalTranslateBusAddress (
  748. DeviceData->InterfaceType,
  749. DeviceData->BusNumber,
  750. DeviceData->OriginalBaseAddress,
  751. &addressSpace,
  752. &translatedAddress
  753. );
  754. DeviceData->BaseAddress = MmMapIoSpace(
  755. translatedAddress,
  756. 0x4000L,
  757. FALSE
  758. );
  759. break;
  760. }
  761. case CmResourceTypeInterrupt:
  762. {
  763. DeviceData->Interrupt.Level = resource->u.Interrupt.Level;
  764. DeviceData->Interrupt.Vector = resource->u.Interrupt.Vector;
  765. DeviceData->Interrupt.Affinity = resource->u.Interrupt.Affinity;
  766. MxenumKdPrint (MXENUM_DBG_TRACE,("Irq=%x\n",DeviceData->Interrupt.Vector));
  767. break;
  768. }
  769. default :
  770. break;
  771. }
  772. }
  773. //
  774. // Now we can touch the lower device object as it is now started.
  775. //
  776. DeviceObject->Flags |= DO_BUFFERED_IO;
  777. MxenumKdPrint (MXENUM_DBG_TRACE,
  778. ("Start Device: Start to download\n"));
  779. i = 0;
  780. while (BoardDesc[DeviceData->BoardType-1][i])
  781. i++;
  782. i <<= 1;
  783. if (DeviceData->NumPorts == 0) // Port not installed
  784. NumPortDefined = FALSE;
  785. else
  786. NumPortDefined = TRUE;
  787. status = MxenumDownloadFirmware(DeviceData,NumPortDefined);
  788. MxenumKdPrint (MXENUM_DBG_TRACE,
  789. ("BoardDesc(%d)->%ws\n",i,BoardDesc[DeviceData->BoardType-1]));
  790. if (status != 0) {
  791. ULONG j;
  792. j = 0;
  793. while (DownloadErrMsg[status-1][j])
  794. j++;
  795. j <<= 1;
  796. MxenumKdPrint (MXENUM_DBG_TRACE,
  797. ("Start Device: Device started failure\n"));
  798. MxenumLogError(
  799. DeviceObject->DriverObject,
  800. NULL,
  801. SerialPhysicalZero,
  802. SerialPhysicalZero,
  803. 0,
  804. 0,
  805. 0,
  806. 19,
  807. STATUS_SUCCESS,
  808. MXENUM_DOWNLOAD_FAIL,
  809. i + sizeof (WCHAR),
  810. BoardDesc[DeviceData->BoardType -1],
  811. j + sizeof (WCHAR),
  812. DownloadErrMsg[status -1]
  813. );
  814. DeviceData->Started = FALSE;
  815. status = STATUS_UNSUCCESSFUL;
  816. }
  817. else {
  818. MxenumKdPrint (MXENUM_DBG_TRACE,
  819. ("Start Device: Device started successfully\n"));
  820. MxenumLogError(
  821. DeviceObject->DriverObject,
  822. NULL,
  823. SerialPhysicalZero,
  824. SerialPhysicalZero,
  825. 0,
  826. 0,
  827. 0,
  828. 19,
  829. STATUS_SUCCESS,
  830. MXENUM_DOWNLOAD_OK,
  831. i + sizeof (WCHAR),
  832. BoardDesc[DeviceData->BoardType -1],
  833. 0,
  834. NULL
  835. );
  836. DeviceData->Started = TRUE;
  837. if (NumPortDefined == FALSE) // Port not installed
  838. MxenumUpdateNumberPortRegistry(DeviceObject);
  839. }
  840. }
  841. //
  842. // We must now complete the IRP, since we stopped it in the
  843. // completetion routine with MORE_PROCESSING_REQUIRED.
  844. //
  845. Irp->IoStatus.Information = 0;
  846. break;
  847. }
  848. case IRP_MN_QUERY_STOP_DEVICE:
  849. MxenumKdPrint (MXENUM_DBG_TRACE,
  850. ("FDO:Query Stop Device\n"));
  851. //
  852. // Test to see if there are any PDO created as children of this FDO
  853. // If there are then conclude the device is busy and fail the
  854. // query stop.
  855. //
  856. // BUGBUG
  857. // We could do better, by seing if the children PDOs are actually
  858. // currently open. If they are not then we could stop, get new
  859. // resouces, fill in the new resouce values, and then when a new client
  860. // opens the PDO use the new resources. But this works for now.
  861. //
  862. if (DeviceData->AttachedPDO) {
  863. // status = STATUS_UNSUCCESSFUL;
  864. break;
  865. } else {
  866. // status = STATUS_SUCCESS;
  867. }
  868. // Irp->IoStatus.Status = status;
  869. IoSkipCurrentIrpStackLocation (Irp);
  870. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  871. MxenumDecIoCount (DeviceData);
  872. return status;
  873. case IRP_MN_STOP_DEVICE:
  874. MxenumKdPrint (MXENUM_DBG_TRACE,("FDO:Stop Device\n"));
  875. //
  876. // After the start IRP has been sent to the lower driver object, the
  877. // bus may NOT send any more IRPS down ``touch'' until another START
  878. // has occured.
  879. // What ever access is required must be done before the Irp is passed
  880. // on.
  881. //
  882. // Stop device means that the resources given durring Start device
  883. // are no revoked. So we need to stop using them
  884. //
  885. DeviceData->Started = FALSE;
  886. //
  887. // We don't need a completion routine so fire and forget.
  888. //
  889. // Set the current stack location to the next stack location and
  890. // call the next device object.
  891. //
  892. // Irp->IoStatus.Status = STATUS_SUCCESS;
  893. IoSkipCurrentIrpStackLocation (Irp);
  894. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  895. MxenumDecIoCount (DeviceData);
  896. return status;
  897. case IRP_MN_REMOVE_DEVICE:
  898. MxenumKdPrint (MXENUM_DBG_TRACE,("FDO:Remove Device\n"));
  899. //
  900. // The PlugPlay system has detected the removal of this device. We
  901. // have no choice but to detach and delete the device object.
  902. // (If we wanted to express and interest in preventing this removal,
  903. // we should have filtered the query remove and query stop routines.)
  904. //
  905. // Note! we might receive a remove WITHOUT first receiving a stop.
  906. // ASSERT (!DeviceData->Removed);
  907. // We will accept no new requests
  908. //
  909. DeviceData->Removed = TRUE;
  910. //
  911. // Complete any outstanding IRPs queued by the driver here.
  912. //
  913. //
  914. // Make the DCA go away. Some drivers may choose to remove the DCA
  915. // when they receive a stop or even a query stop. We just don't care.
  916. //
  917. IoSetDeviceInterfaceState (&DeviceData->DevClassAssocName, FALSE);
  918. //
  919. // Here if we had any outstanding requests in a personal queue we should
  920. // complete them all now.
  921. //
  922. // Note, the device is guarenteed stopped, so we cannot send it any non-
  923. // PNP IRPS.
  924. //
  925. //
  926. // Fire and forget
  927. //
  928. Irp->IoStatus.Status = STATUS_SUCCESS;
  929. IoSkipCurrentIrpStackLocation (Irp);
  930. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  931. //
  932. // Wait for all outstanding requests to complete
  933. //
  934. MxenumKdPrint (MXENUM_DBG_TRACE,
  935. ("Waiting for outstanding requests\n"));
  936. i = InterlockedDecrement (&DeviceData->OutstandingIO);
  937. // ASSERT (0 < i);
  938. if (0 != InterlockedDecrement (&DeviceData->OutstandingIO)) {
  939. MxenumKdPrint (MXENUM_DBG_TRACE,
  940. ("Remove Device waiting for request to complete\n"));
  941. KeWaitForSingleObject (&DeviceData->RemoveEvent,
  942. Suspended,
  943. KernelMode,
  944. FALSE, // Not Alertable
  945. NULL); // No timeout
  946. }
  947. //
  948. // Free the associated resources
  949. //
  950. if (DeviceData->AddressMapped)
  951. MmUnmapIoSpace(DeviceData->AckPort,0x80);
  952. if (DeviceData->BaseAddress)
  953. MmUnmapIoSpace(DeviceData->BaseAddress,0x4000L);
  954. //
  955. // Detach from the underlying devices.
  956. //
  957. IoDetachDevice (DeviceData->TopOfStack);
  958. //
  959. // Clean up any resources here
  960. ExFreePool (DeviceData->DevClassAssocName.Buffer);
  961. //
  962. // Remove any PDO's we ejected
  963. //
  964. if (DeviceData->AttachedPDO != NULL) {
  965. MxenumPnPRemovePDOs(DeviceObject);
  966. }
  967. IoDeleteDevice(DeviceObject);
  968. NumBoardInstalled--;
  969. return status;
  970. case IRP_MN_QUERY_DEVICE_RELATIONS:
  971. {
  972. PDEVICE_OBJECT currentObj;
  973. MxenumKdPrint (MXENUM_DBG_TRACE,("FDO:Query Device Relation\n"));
  974. if (BusRelations != IrpStack->Parameters.QueryDeviceRelations.Type) {
  975. //
  976. // We don't support this
  977. //
  978. MxenumKdPrint (MXENUM_DBG_TRACE,
  979. ("Query Device Relations - Non bus\n"));
  980. goto SER_FDO_PNP_DEFAULT;
  981. }
  982. if (DeviceData->AttachedPDO == NULL)
  983. MxenumCreatePDO( DeviceData );
  984. MxenumKdPrint (MXENUM_DBG_TRACE,
  985. ("Query Bus Relations\n"));
  986. DeviceData->PDOForcedRemove = FALSE;
  987. //
  988. // Tell the plug and play system about all the PDOs.
  989. //
  990. // There might also be device relations below and above this FDO,
  991. // so, be sure to propagate the relations from the upper drivers.
  992. //
  993. // No Completion routine is needed so long as the status is preset
  994. // to success. (PDOs complete plug and play irps with the current
  995. // IoStatus.Status and IoStatus.Information as the default.)
  996. //
  997. //KeAcquireSpinLock (&DeviceData->Spin, &oldIrq);
  998. i = (0 == Irp->IoStatus.Information) ? 0 :
  999. ((PDEVICE_RELATIONS) Irp->IoStatus.Information)->Count;
  1000. // The current number of PDOs in the device relations structure
  1001. MxenumKdPrint (MXENUM_DBG_TRACE,
  1002. ("#PDOS = %d + %d\n", i, DeviceData->NumPDOs));
  1003. length = sizeof(DEVICE_RELATIONS) +
  1004. ((DeviceData->NumPDOs + i) * sizeof (PDEVICE_OBJECT));
  1005. if ((DeviceData->NumPDOs + i) <= 0) {
  1006. //
  1007. // Set up and pass the IRP further down the stack
  1008. //
  1009. Irp->IoStatus.Status = STATUS_SUCCESS;
  1010. IoSkipCurrentIrpStackLocation (Irp);
  1011. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  1012. MxenumDecIoCount (DeviceData);
  1013. return status;
  1014. }
  1015. relations = (PDEVICE_RELATIONS) ExAllocatePool (PagedPool, length);
  1016. if (NULL == relations) {
  1017. MxenumKdPrint (MXENUM_DBG_TRACE,
  1018. ("Insufficient resources\n"));
  1019. status = STATUS_INSUFFICIENT_RESOURCES;
  1020. break;
  1021. }
  1022. //
  1023. // Copy in the device objects so far
  1024. //
  1025. if (i) {
  1026. RtlCopyMemory (
  1027. relations->Objects,
  1028. ((PDEVICE_RELATIONS) Irp->IoStatus.Information)->Objects,
  1029. i * sizeof (PDEVICE_OBJECT));
  1030. }
  1031. relations->Count = DeviceData->NumPDOs + i;
  1032. //
  1033. // For each PDO on this bus add a pointer to the device relations
  1034. // buffer, being sure to take out a reference to that object.
  1035. // The PlugPlay system will dereference the object when it is done with
  1036. // it and free the device relations buffer.
  1037. //
  1038. currentObj = DeviceData->AttachedPDO;
  1039. while ((i < relations->Count)&& (currentObj != NULL)) {
  1040. relations->Objects[i] = currentObj;
  1041. ObReferenceObject (currentObj);
  1042. currentObj = ((PPDO_DEVICE_DATA)(currentObj->DeviceExtension))->Next;
  1043. i++;
  1044. }
  1045. //
  1046. // Set up and pass the IRP further down the stack
  1047. //
  1048. Irp->IoStatus.Status = STATUS_SUCCESS;
  1049. if (0 != Irp->IoStatus.Information) {
  1050. ExFreePool ((PVOID) Irp->IoStatus.Information);
  1051. }
  1052. Irp->IoStatus.Information = (ULONG_PTR)relations;
  1053. IoSkipCurrentIrpStackLocation (Irp);
  1054. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  1055. MxenumDecIoCount (DeviceData);
  1056. return status;
  1057. }
  1058. case IRP_MN_QUERY_REMOVE_DEVICE:
  1059. MxenumKdPrint (MXENUM_DBG_TRACE,
  1060. ("FDO:Query Remove Device\n"));
  1061. //
  1062. // If we were to fail this call then we would need to complete the
  1063. // IRP here. Since we are not, set the status to SUCCESS and
  1064. // call the next driver.
  1065. //
  1066. Irp->IoStatus.Status = STATUS_SUCCESS;
  1067. IoSkipCurrentIrpStackLocation (Irp);
  1068. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  1069. MxenumDecIoCount (DeviceData);
  1070. return status;
  1071. case IRP_MN_CANCEL_STOP_DEVICE:
  1072. MxenumKdPrint(MXENUM_DBG_TRACE, ("FDO:Cancel Stop Device\n"));
  1073. // Irp->IoStatus.Status = STATUS_SUCCESS;
  1074. IoSkipCurrentIrpStackLocation (Irp);
  1075. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  1076. MxenumDecIoCount (DeviceData);
  1077. return status;
  1078. case IRP_MN_QUERY_BUS_INFORMATION: {
  1079. PPNP_BUS_INFORMATION pBusInfo;
  1080. // ASSERTMSG("Serenum appears not to be the sole bus?!?",
  1081. // Irp->IoStatus.Information == (ULONG_PTR)NULL);
  1082. pBusInfo = ExAllocatePool(PagedPool, sizeof(PNP_BUS_INFORMATION));
  1083. if (pBusInfo == NULL) {
  1084. status = STATUS_INSUFFICIENT_RESOURCES;
  1085. break;
  1086. }
  1087. pBusInfo->BusTypeGuid = GUID_BUS_TYPE_SERENUM;
  1088. pBusInfo->LegacyBusType = DeviceData->InterfaceType;
  1089. //
  1090. // We really can't track our bus number since we can be torn
  1091. // down with our bus
  1092. //
  1093. pBusInfo->BusNumber = DeviceData->BusNumber;
  1094. Irp->IoStatus.Information = (ULONG_PTR)pBusInfo;
  1095. status = STATUS_SUCCESS;
  1096. break;
  1097. }
  1098. SER_FDO_PNP_DEFAULT:
  1099. default:
  1100. //
  1101. // In the default case we merely call the next driver since
  1102. // we don't know what to do.
  1103. //
  1104. MxenumKdPrint (MXENUM_DBG_TRACE,("FDO:Default Case(%x)\n",IrpStack->MinorFunction));
  1105. //
  1106. // Fire and Forget
  1107. //
  1108. IoSkipCurrentIrpStackLocation (Irp);
  1109. //
  1110. // Done, do NOT complete the IRP, it will be processed by the lower
  1111. // device object, which will complete the IRP
  1112. //
  1113. status = IoCallDriver (DeviceData->TopOfStack, Irp);
  1114. MxenumDecIoCount (DeviceData);
  1115. return status;
  1116. }
  1117. Irp->IoStatus.Status = status;
  1118. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  1119. MxenumDecIoCount (DeviceData);
  1120. return status;
  1121. }
  1122. NTSTATUS
  1123. MxenumFdoPnPComplete (
  1124. IN PDEVICE_OBJECT DeviceObject,
  1125. IN PIRP Irp,
  1126. IN PVOID Context
  1127. )
  1128. /*++
  1129. Routine Description:
  1130. A completion routine for use when calling the lower device objects to
  1131. which our bus (FDO) is attached.
  1132. --*/
  1133. {
  1134. UNREFERENCED_PARAMETER (DeviceObject);
  1135. if (Irp->PendingReturned) {
  1136. IoMarkIrpPending( Irp );
  1137. }
  1138. KeSetEvent ((PKEVENT) Context, 1, FALSE);
  1139. // No special priority
  1140. // No Wait
  1141. return STATUS_MORE_PROCESSING_REQUIRED; // Keep this IRP
  1142. }
  1143. NTSTATUS
  1144. MxenumPdoPnP (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp,
  1145. IN PIO_STACK_LOCATION IrpStack, IN PPDO_DEVICE_DATA DeviceData)
  1146. /*++
  1147. Routine Description:
  1148. Handle requests from the PlugPlay system for the devices on the BUS
  1149. --*/
  1150. {
  1151. PDEVICE_CAPABILITIES deviceCapabilities;
  1152. ULONG information;
  1153. PWCHAR buffer;
  1154. ULONG length, i, j;
  1155. NTSTATUS status;
  1156. KIRQL oldIrq;
  1157. HANDLE keyHandle;
  1158. PWCHAR returnBuffer = NULL;
  1159. PAGED_CODE();
  1160. status = Irp->IoStatus.Status;
  1161. //
  1162. // NB: since we are a bus enumerator, we have no one to whom we could
  1163. // defer these irps. Therefore we do not pass them down but merely
  1164. // return them.
  1165. //
  1166. switch (IrpStack->MinorFunction) {
  1167. case IRP_MN_QUERY_CAPABILITIES:
  1168. MxenumKdPrint (MXENUM_DBG_TRACE,("PDO:Query Caps \n"));
  1169. //
  1170. // Get the packet.
  1171. //
  1172. deviceCapabilities=IrpStack->Parameters.DeviceCapabilities.Capabilities;
  1173. //
  1174. // Set the capabilities.
  1175. //
  1176. deviceCapabilities->Version = 1;
  1177. deviceCapabilities->Size = sizeof (DEVICE_CAPABILITIES);
  1178. //
  1179. // We support only Power state D0 and D3
  1180. //
  1181. deviceCapabilities->DeviceState[PowerSystemWorking] = PowerDeviceD0;
  1182. deviceCapabilities->DeviceState[PowerSystemSleeping1] = PowerSystemUnspecified;
  1183. deviceCapabilities->DeviceState[PowerSystemSleeping2] = PowerSystemUnspecified;
  1184. deviceCapabilities->DeviceState[PowerSystemSleeping3] = PowerDeviceD3;
  1185. deviceCapabilities->DeviceState[PowerSystemHibernate] = PowerDeviceD3;
  1186. deviceCapabilities->DeviceState[PowerSystemShutdown] = PowerDeviceD3;
  1187. //
  1188. // We cannot wake the system.
  1189. //
  1190. deviceCapabilities->SystemWake = PowerSystemUnspecified;
  1191. deviceCapabilities->DeviceWake = PowerDeviceUnspecified;
  1192. //
  1193. // We have no latencies
  1194. //
  1195. deviceCapabilities->D1Latency = 0;
  1196. deviceCapabilities->D2Latency = 0;
  1197. deviceCapabilities->D3Latency = 0;
  1198. //
  1199. // No locking or ejection
  1200. //
  1201. deviceCapabilities->LockSupported = FALSE;
  1202. deviceCapabilities->EjectSupported = FALSE;
  1203. //
  1204. // Device can be physically removed.
  1205. // Technically there is no physical device to remove, but this bus
  1206. // driver can yank the PDO from the PlugPlay system, when ever it
  1207. // receives an IOCTL_SERENUM_REMOVE_PORT device control command.
  1208. //
  1209. deviceCapabilities->Removable = FALSE;
  1210. //
  1211. // not Docking device
  1212. //
  1213. deviceCapabilities->DockDevice = FALSE;
  1214. deviceCapabilities->UniqueID = FALSE;
  1215. deviceCapabilities->SilentInstall = TRUE;
  1216. deviceCapabilities->RawDeviceOK = FALSE;
  1217. deviceCapabilities->SurpriseRemovalOK = TRUE;
  1218. status = STATUS_SUCCESS;
  1219. break;
  1220. case IRP_MN_QUERY_DEVICE_TEXT: {
  1221. MxenumKdPrint (MXENUM_DBG_TRACE,("PDO:Query Device Text \n"));
  1222. if (IrpStack->Parameters.QueryDeviceText.DeviceTextType
  1223. != DeviceTextDescription) {
  1224. break;
  1225. }
  1226. returnBuffer = ExAllocatePool(PagedPool,
  1227. sizeof(MXENUM_PDO_DEVICE_TEXT)+ sizeof(UNICODE_NULL));
  1228. if (returnBuffer == NULL) {
  1229. status = STATUS_INSUFFICIENT_RESOURCES;
  1230. break;
  1231. }
  1232. status = STATUS_SUCCESS;
  1233. RtlZeroMemory(returnBuffer,sizeof(MXENUM_PDO_DEVICE_TEXT)+ sizeof(UNICODE_NULL));
  1234. RtlCopyMemory(returnBuffer, MXENUM_PDO_DEVICE_TEXT,
  1235. sizeof(MXENUM_PDO_DEVICE_TEXT));
  1236. MxenumKdPrint(MXENUM_DBG_TRACE,
  1237. ("TextID: %ws\n", returnBuffer));
  1238. Irp->IoStatus.Information = (ULONG_PTR)returnBuffer;
  1239. break;
  1240. }
  1241. case IRP_MN_QUERY_ID:
  1242. //
  1243. // Query the IDs of the device
  1244. //
  1245. MxenumKdPrint(MXENUM_DBG_TRACE,
  1246. ("PDO:QueryID: 0x%x\n", IrpStack->Parameters.QueryId.IdType));
  1247. switch (IrpStack->Parameters.QueryId.IdType) {
  1248. case BusQueryInstanceID:
  1249. //
  1250. // Build an instance ID. This is what PnP uses to tell if it has
  1251. // seen this thing before or not. Build it from the first hardware
  1252. // id and the port number.
  1253. //
  1254. // NB since we do not incorperate the port number
  1255. // this method does not produce unique ids;
  1256. //
  1257. // return 0000 for all devices and have the flag set to not unique
  1258. //
  1259. length = MXENUM_INSTANCE_IDS_LENGTH * sizeof(WCHAR);
  1260. returnBuffer = ExAllocatePool(PagedPool, length);
  1261. if (returnBuffer != NULL) {
  1262. RtlCopyMemory(returnBuffer, MXENUM_INSTANCE_IDS, length);
  1263. status = STATUS_SUCCESS;
  1264. } else {
  1265. status = STATUS_INSUFFICIENT_RESOURCES;
  1266. }
  1267. MxenumKdPrint(MXENUM_DBG_TRACE,
  1268. ("InstanceID: %ws\n", returnBuffer));
  1269. Irp->IoStatus.Information = (ULONG_PTR)returnBuffer;
  1270. break;
  1271. //
  1272. // The other ID's we just copy from the buffers and are done.
  1273. //
  1274. case BusQueryDeviceID:
  1275. case BusQueryHardwareIDs:
  1276. case BusQueryCompatibleIDs:
  1277. {
  1278. PUNICODE_STRING pId;
  1279. status = STATUS_SUCCESS;
  1280. switch (IrpStack->Parameters.QueryId.IdType) {
  1281. case BusQueryDeviceID:
  1282. pId = &DeviceData->DeviceIDs;
  1283. break;
  1284. case BusQueryHardwareIDs:
  1285. pId = &DeviceData->HardwareIDs;
  1286. break;
  1287. case BusQueryCompatibleIDs:
  1288. pId = &DeviceData->CompIDs;
  1289. break;
  1290. }
  1291. buffer = pId->Buffer;
  1292. if (buffer != NULL) {
  1293. length = pId->Length;
  1294. returnBuffer = ExAllocatePool(PagedPool, length);
  1295. if (returnBuffer != NULL) {
  1296. RtlCopyMemory(returnBuffer, buffer, pId->Length);
  1297. } else {
  1298. status = STATUS_INSUFFICIENT_RESOURCES;
  1299. }
  1300. }
  1301. MxenumKdPrint(MXENUM_DBG_TRACE,
  1302. ("ID:%ws\n", returnBuffer));
  1303. Irp->IoStatus.Information = (ULONG_PTR)returnBuffer;
  1304. }
  1305. break;
  1306. }
  1307. break;
  1308. case IRP_MN_QUERY_DEVICE_RELATIONS:
  1309. // MxenumKdPrint (MXENUM_DBG_TRACE, ("PDO:Query Device Relation (type=%x) \n",IrpStack->Parameters.QueryDeviceRelations.Type));
  1310. switch (IrpStack->Parameters.QueryDeviceRelations.Type) {
  1311. case TargetDeviceRelation: {
  1312. PDEVICE_RELATIONS pDevRel;
  1313. //
  1314. // No one else should respond to this since we are the PDO
  1315. //
  1316. // ASSERT(Irp->IoStatus.Information == 0);
  1317. if (Irp->IoStatus.Information != 0) {
  1318. break;
  1319. }
  1320. pDevRel = ExAllocatePool(PagedPool, sizeof(DEVICE_RELATIONS));
  1321. if (pDevRel == NULL) {
  1322. status = STATUS_INSUFFICIENT_RESOURCES;
  1323. break;
  1324. }
  1325. pDevRel->Count = 1;
  1326. pDevRel->Objects[0] = DeviceObject;
  1327. ObReferenceObject(DeviceObject);
  1328. status = STATUS_SUCCESS;
  1329. Irp->IoStatus.Information = (ULONG_PTR)pDevRel;
  1330. break;
  1331. }
  1332. default:
  1333. break;
  1334. }
  1335. break;
  1336. case IRP_MN_START_DEVICE: {
  1337. MxenumKdPrint(MXENUM_DBG_TRACE, ("PDO:Start Device\n"));
  1338. //
  1339. // Here we do what ever initialization and ``turning on'' that is
  1340. // required to allow others to access this device.
  1341. //
  1342. DeviceData->Started = TRUE;
  1343. status = STATUS_SUCCESS;
  1344. break;
  1345. }
  1346. case IRP_MN_STOP_DEVICE:
  1347. MxenumKdPrint(MXENUM_DBG_TRACE,("PDO:Stop Device\n"));
  1348. //
  1349. // Here we shut down the device. The opposite of start.
  1350. //
  1351. DeviceData->Started = FALSE;
  1352. status = STATUS_SUCCESS;
  1353. break;
  1354. case IRP_MN_REMOVE_DEVICE:
  1355. MxenumKdPrint(MXENUM_DBG_TRACE,("PDO:Remove Device\n"));
  1356. status = STATUS_SUCCESS;
  1357. break;
  1358. case IRP_MN_QUERY_STOP_DEVICE:
  1359. MxenumKdPrint(MXENUM_DBG_TRACE,("PDO:Query Stop Device\n"));
  1360. //
  1361. // No reason here why we can't stop the device.
  1362. // If there were a reason we should speak now for answering success
  1363. // here may result in a stop device irp.
  1364. //
  1365. status = STATUS_SUCCESS;
  1366. break;
  1367. case IRP_MN_CANCEL_STOP_DEVICE:
  1368. MxenumKdPrint(MXENUM_DBG_TRACE, ("PDO:Cancel Stop Device\n"));
  1369. //
  1370. // The stop was canceled. Whatever state we set, or resources we put
  1371. // on hold in anticipation of the forcoming STOP device IRP should be
  1372. // put back to normal. Someone, in the long list of concerned parties,
  1373. // has failed the stop device query.
  1374. //
  1375. status = STATUS_SUCCESS;
  1376. break;
  1377. case IRP_MN_QUERY_REMOVE_DEVICE:
  1378. MxenumKdPrint(MXENUM_DBG_TRACE,("PDO:Query Remove Device\n"));
  1379. //
  1380. // Just like Query Stop only now the impending doom is the remove irp
  1381. //
  1382. status = STATUS_SUCCESS;
  1383. break;
  1384. case IRP_MN_CANCEL_REMOVE_DEVICE:
  1385. case IRP_MN_READ_CONFIG:
  1386. case IRP_MN_WRITE_CONFIG: // we have no config space
  1387. case IRP_MN_EJECT:
  1388. case IRP_MN_SET_LOCK:
  1389. case IRP_MN_QUERY_INTERFACE: // We do not have any non IRP based interfaces.
  1390. default:
  1391. MxenumKdPrint(MXENUM_DBG_TRACE,("PDO:PNP Not handled 0x%x\n",
  1392. IrpStack->MinorFunction));
  1393. // this is a leaf node
  1394. // status = STATUS_NOT_IMPLEMENTED
  1395. // For PnP requests to the PDO that we do not understand we should
  1396. // return the IRP WITHOUT setting the status or information fields.
  1397. // They may have already been set by a filter (eg acpi).
  1398. break;
  1399. }
  1400. Irp->IoStatus.Status = status;
  1401. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  1402. return status;
  1403. }
  1404. NTSTATUS
  1405. MxenumPnPRemovePDOs (PDEVICE_OBJECT PFdo)
  1406. /*++
  1407. Routine Description:
  1408. The PlugPlay subsystem has instructed that this PDO should be removed.
  1409. We should therefore
  1410. - Complete any requests queued in the driver
  1411. - If the device is still attached to the system,
  1412. then complete the request and return.
  1413. - Otherwise, cleanup device specific allocations, memory, events...
  1414. - Call IoDeleteDevice
  1415. - Return from the dispatch routine.
  1416. Note that if the device is still connected to the bus (IE in this case
  1417. the control panel has not yet told us that the serial device has
  1418. disappeared) then the PDO must remain around, and must be returned during
  1419. any query Device relaions IRPS.
  1420. --*/
  1421. {
  1422. PPDO_DEVICE_DATA PdoData;
  1423. PDEVICE_OBJECT nextDevice;
  1424. PDEVICE_OBJECT currentDevice = ((PFDO_DEVICE_DATA)PFdo->DeviceExtension)->AttachedPDO;
  1425. ULONG i = 0;
  1426. PAGED_CODE();
  1427. while (currentDevice ) {
  1428. PdoData = currentDevice->DeviceExtension;
  1429. PdoData->Removed = TRUE;
  1430. //
  1431. // Complete any outstanding requests with STATUS_DELETE_PENDING.
  1432. //
  1433. // Serenum does not queue any irps at this time so we have nothing to do.
  1434. //
  1435. //
  1436. // Free any resources.
  1437. //
  1438. if (PdoData->HardwareIDs.Buffer)
  1439. ExFreePool(PdoData->HardwareIDs.Buffer);
  1440. if (PdoData->CompIDs.Buffer)
  1441. ExFreePool(PdoData->CompIDs.Buffer);
  1442. if (PdoData->DeviceIDs.Buffer)
  1443. ExFreePool(PdoData->DeviceIDs.Buffer);
  1444. MxenumKdPrint(MXENUM_DBG_TRACE,("PDO:MxenumPnPRemovePDOs = %x\n",currentDevice));
  1445. nextDevice = PdoData->Next;
  1446. IoDeleteDevice(currentDevice);
  1447. currentDevice = nextDevice;
  1448. }
  1449. ((PFDO_DEVICE_DATA)PFdo->DeviceExtension)->NumPDOs = 0;
  1450. ((PFDO_DEVICE_DATA)PFdo->DeviceExtension)->AttachedPDO = NULL;
  1451. return STATUS_SUCCESS;
  1452. }
  1453. NTSTATUS
  1454. MxenumPnPRemovePDO (PDEVICE_OBJECT PPdo)
  1455. /*++
  1456. Routine Description:
  1457. The PlugPlay subsystem has instructed that this PDO should be removed.
  1458. --*/
  1459. {
  1460. PPDO_DEVICE_DATA PdoData;
  1461. PDEVICE_OBJECT nextDevice,previousDevice;
  1462. PDEVICE_OBJECT currentDevice = ((PFDO_DEVICE_DATA)((PPDO_DEVICE_DATA)PPdo->DeviceExtension)->ParentFdo->DeviceExtension)->AttachedPDO;
  1463. nextDevice = previousDevice = NULL;
  1464. while (currentDevice ) {
  1465. PdoData = currentDevice->DeviceExtension;
  1466. if (currentDevice != PPdo) {
  1467. previousDevice = currentDevice;
  1468. currentDevice = PdoData->Next;
  1469. continue;
  1470. }
  1471. PdoData->Removed = TRUE;
  1472. //
  1473. // Complete any outstanding requests with STATUS_DELETE_PENDING.
  1474. //
  1475. // Serenum does not queue any irps at this time so we have nothing to do.
  1476. //
  1477. //
  1478. // Free any resources.
  1479. //
  1480. if (PdoData->HardwareIDs.Buffer)
  1481. ExFreePool(PdoData->HardwareIDs.Buffer);
  1482. if (PdoData->CompIDs.Buffer)
  1483. ExFreePool(PdoData->CompIDs.Buffer);
  1484. if (PdoData->DeviceIDs.Buffer)
  1485. ExFreePool(PdoData->DeviceIDs.Buffer);
  1486. MxenumKdPrint(MXENUM_DBG_TRACE,("PDO:MxenumPnPRemovePDOs = %x\n",currentDevice));
  1487. nextDevice = PdoData->Next;
  1488. IoDeleteDevice(currentDevice);
  1489. if (previousDevice != NULL)
  1490. ((PPDO_DEVICE_DATA)previousDevice->DeviceExtension)->Next = nextDevice;
  1491. else
  1492. ((PFDO_DEVICE_DATA)((PPDO_DEVICE_DATA)PPdo->DeviceExtension)->ParentFdo->DeviceExtension)->AttachedPDO = nextDevice;
  1493. return STATUS_SUCCESS;
  1494. }
  1495. return STATUS_SUCCESS;
  1496. }
  1497. //
  1498. // Unit of t is 1ms
  1499. //
  1500. VOID
  1501. MxenumDelay(IN ULONG t)
  1502. {
  1503. LARGE_INTEGER delay;
  1504. t *= 10000; /* delay unit = 100 ns */
  1505. delay = RtlConvertUlongToLargeInteger(t);
  1506. delay = RtlLargeIntegerNegate(delay);
  1507. KeDelayExecutionThread(
  1508. KernelMode,
  1509. FALSE,
  1510. &delay
  1511. );
  1512. }