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.

1058 lines
32 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. pnp.c
  5. Abstract:
  6. This module contains plug & play code for the HID Mouse Filter Driver,
  7. including code for the creation and removal of HID mouse device contexts.
  8. Environment:
  9. Kernel & user mode.
  10. Revision History:
  11. Jan-1997 : Initial writing, Dan Markarian
  12. --*/
  13. //
  14. // For this module only we set the INITGUID macro before including wdm.h and
  15. // hidclass.h. This not only declares the GUIDs but also initializes them.
  16. //
  17. #include "mouhid.h"
  18. #include "hidclass.h"
  19. #include <initguid.h>
  20. #include <wdmguid.h>
  21. #ifdef ALLOC_PRAGMA
  22. #pragma alloc_text(PAGE,MouHid_CallHidClass)
  23. #pragma alloc_text(PAGE,MouHid_AddDevice)
  24. #pragma alloc_text(PAGE,MouHid_StartDevice)
  25. #pragma alloc_text(PAGE,MouHid_PnP)
  26. #endif
  27. NTSTATUS
  28. MouHid_CallHidClass(
  29. IN PDEVICE_EXTENSION Data,
  30. IN ULONG Ioctl,
  31. IN PVOID InputBuffer,
  32. IN ULONG InputBufferLength,
  33. IN OUT PVOID OutputBuffer,
  34. IN ULONG OutputBufferLength
  35. )
  36. /*++
  37. Routine Description:
  38. Make a *synchronous* request of the HID class driver
  39. Arguments:
  40. Ioctl - Value of the IOCTL request.
  41. InputBuffer - Buffer to be sent to the HID class driver.
  42. InputBufferLength - Size of buffer to be sent to the HID class driver.
  43. OutputBuffer - Buffer for received data from the HID class driver.
  44. OutputBufferLength - Size of receive buffer from the HID class.
  45. Return Value:
  46. STATUS_SUCCESS if successful,
  47. STATUS_UNSUCCESSFUL otherwise
  48. --*/
  49. {
  50. KEVENT event;
  51. IO_STATUS_BLOCK ioStatus;
  52. PIRP irp;
  53. PIO_STACK_LOCATION nextStack;
  54. NTSTATUS status = STATUS_SUCCESS;
  55. PAGED_CODE ();
  56. Print (DBG_PNP_TRACE, ("PNP-CallHidClass: Enter." ));
  57. //
  58. // Prepare to issue a synchronous request.
  59. //
  60. KeInitializeEvent(&event, NotificationEvent, FALSE);
  61. irp = IoBuildDeviceIoControlRequest (
  62. Ioctl,
  63. Data->TopOfStack,
  64. InputBuffer,
  65. InputBufferLength,
  66. OutputBuffer,
  67. OutputBufferLength,
  68. FALSE, // external IOCTL
  69. &event,
  70. &ioStatus);
  71. if (irp == NULL) {
  72. return STATUS_UNSUCCESSFUL;
  73. }
  74. //
  75. // Call the class driver to perform the operation. If the returned status
  76. // is PENDING, wait for the request to complete.
  77. //
  78. nextStack = IoGetNextIrpStackLocation(irp);
  79. ASSERT(nextStack != NULL);
  80. status = IoCallDriver(Data->TopOfStack, irp);
  81. if (status == STATUS_PENDING) {
  82. status = KeWaitForSingleObject(
  83. &event,
  84. Executive, // wait reason
  85. KernelMode,
  86. FALSE, // not alertable
  87. NULL); // no time out
  88. }
  89. if (NT_SUCCESS (status)) {
  90. status = ioStatus.Status;
  91. }
  92. Print (DBG_PNP_TRACE, ("PNP-CallHidClass: Enter." ));
  93. return status;
  94. }
  95. NTSTATUS
  96. MouHid_QueryDeviceKey (
  97. IN HANDLE Handle,
  98. IN PWCHAR ValueNameString,
  99. OUT PVOID Data,
  100. IN ULONG DataLength
  101. )
  102. {
  103. NTSTATUS status;
  104. UNICODE_STRING valueName;
  105. ULONG length;
  106. PKEY_VALUE_FULL_INFORMATION fullInfo;
  107. RtlInitUnicodeString (&valueName, ValueNameString);
  108. length = sizeof (KEY_VALUE_FULL_INFORMATION)
  109. + valueName.MaximumLength
  110. + DataLength;
  111. fullInfo = ExAllocatePool (PagedPool, length);
  112. if (fullInfo) {
  113. status = ZwQueryValueKey (Handle,
  114. &valueName,
  115. KeyValueFullInformation,
  116. fullInfo,
  117. length,
  118. &length);
  119. if (NT_SUCCESS (status)) {
  120. ASSERT (DataLength == fullInfo->DataLength);
  121. RtlCopyMemory (Data,
  122. ((PUCHAR) fullInfo) + fullInfo->DataOffset,
  123. fullInfo->DataLength);
  124. }
  125. ExFreePool (fullInfo);
  126. } else {
  127. status = STATUS_NO_MEMORY;
  128. }
  129. return status;
  130. }
  131. NTSTATUS
  132. MouHid_AddDevice (
  133. IN PDRIVER_OBJECT Driver,
  134. IN PDEVICE_OBJECT PDO
  135. )
  136. /*++
  137. Routine Description:
  138. Arguments:
  139. Return Value:
  140. NTSTATUS result code.
  141. --*/
  142. {
  143. NTSTATUS status = STATUS_SUCCESS;
  144. PDEVICE_EXTENSION data;
  145. PDEVICE_OBJECT device;
  146. HANDLE devInstRegKey;
  147. ULONG tmp = 0;
  148. POWER_STATE state;
  149. PAGED_CODE ();
  150. Print (DBG_PNP_TRACE, ("enter Add Device \n"));
  151. status = IoCreateDevice(Driver,
  152. sizeof(DEVICE_EXTENSION),
  153. NULL, // no name for this Filter DO
  154. FILE_DEVICE_MOUSE,
  155. 0,
  156. FALSE,
  157. &device);
  158. if (!NT_SUCCESS (status)) {
  159. return( status );
  160. }
  161. data = (PDEVICE_EXTENSION) device->DeviceExtension;
  162. //
  163. // Initialize the fields.
  164. //
  165. data->TopOfStack = IoAttachDeviceToDeviceStack (device, PDO);
  166. if (data->TopOfStack == NULL) {
  167. PIO_ERROR_LOG_PACKET errorLogEntry;
  168. //
  169. // Not good; in only extreme cases will this fail
  170. //
  171. errorLogEntry = (PIO_ERROR_LOG_PACKET)
  172. IoAllocateErrorLogEntry(Driver,
  173. (UCHAR) sizeof(IO_ERROR_LOG_PACKET));
  174. if (errorLogEntry) {
  175. errorLogEntry->ErrorCode = MOUHID_ATTACH_DEVICE_FAILED;
  176. errorLogEntry->DumpDataSize = 0;
  177. errorLogEntry->SequenceNumber = 0;
  178. errorLogEntry->MajorFunctionCode = 0;
  179. errorLogEntry->IoControlCode = 0;
  180. errorLogEntry->RetryCount = 0;
  181. errorLogEntry->UniqueErrorValue = 0;
  182. errorLogEntry->FinalStatus = STATUS_DEVICE_NOT_CONNECTED;
  183. IoWriteErrorLogEntry(errorLogEntry);
  184. }
  185. IoDeleteDevice(device);
  186. return STATUS_DEVICE_NOT_CONNECTED;
  187. }
  188. ASSERT (data->TopOfStack);
  189. data->Self = device;
  190. data->Started = FALSE;
  191. data->Initialized = FALSE;
  192. data->UnitId = (USHORT) InterlockedIncrement (&Globals.UnitId);
  193. data->PDO = PDO;
  194. data->ReadIrp = IoAllocateIrp (data->TopOfStack->StackSize, FALSE);
  195. // Initializiation happens automatically.
  196. if (NULL == data->ReadIrp) {
  197. IoDetachDevice (data->TopOfStack);
  198. IoDeleteDevice (device);
  199. return STATUS_INSUFFICIENT_RESOURCES;
  200. }
  201. KeInitializeEvent (&data->ReadCompleteEvent, SynchronizationEvent, FALSE);
  202. KeInitializeEvent (&data->ReadSentEvent, NotificationEvent, TRUE);
  203. IoInitializeRemoveLock (&data->RemoveLock, MOUHID_POOL_TAG, 1, 10);
  204. data->ReadFile = NULL;
  205. ExInitializeFastMutex (&data->CreateCloseMutex);
  206. data->InputData.UnitId = data->UnitId;
  207. //
  208. // Initialize the mouse attributes.
  209. //
  210. data->Attributes.MouseIdentifier = MOUSE_HID_HARDWARE;
  211. data->Attributes.SampleRate = 0;
  212. data->Attributes.InputDataQueueLength = 2;
  213. //
  214. // Find device specific parameters for this hid mouse device.
  215. //
  216. if (NT_SUCCESS (status)) {
  217. status = IoOpenDeviceRegistryKey (PDO,
  218. PLUGPLAY_REGKEY_DEVICE,
  219. STANDARD_RIGHTS_ALL,
  220. &devInstRegKey);
  221. data->FlipFlop = FALSE;
  222. if (NT_SUCCESS (status)) {
  223. status = MouHid_QueryDeviceKey (devInstRegKey,
  224. FLIP_FLOP_WHEEL,
  225. &tmp,
  226. sizeof (tmp));
  227. if (NT_SUCCESS (status)) {
  228. data->FlipFlop = (BOOLEAN) tmp;
  229. }
  230. status = MouHid_QueryDeviceKey (devInstRegKey,
  231. SCALING_FACTOR_WHEEL,
  232. &tmp,
  233. sizeof (tmp));
  234. if (NT_SUCCESS (status)) {
  235. data->WheelScalingFactor = (ULONG) tmp;
  236. } else {
  237. data->WheelScalingFactor = 120;
  238. }
  239. ZwClose (devInstRegKey);
  240. }
  241. status = STATUS_SUCCESS;
  242. }
  243. state.DeviceState = PowerDeviceD0;
  244. PoSetPowerState (device, DevicePowerState, state);
  245. data->WmiLibInfo.GuidCount = sizeof (MouHid_WmiGuidList) /
  246. sizeof (WMIGUIDREGINFO);
  247. ASSERT (1 == data->WmiLibInfo.GuidCount);
  248. data->WmiLibInfo.GuidList = MouHid_WmiGuidList;
  249. data->WmiLibInfo.QueryWmiRegInfo = MouHid_QueryWmiRegInfo;
  250. data->WmiLibInfo.QueryWmiDataBlock = MouHid_QueryWmiDataBlock;
  251. data->WmiLibInfo.SetWmiDataBlock = MouHid_SetWmiDataBlock;
  252. data->WmiLibInfo.SetWmiDataItem = MouHid_SetWmiDataItem;
  253. data->WmiLibInfo.ExecuteWmiMethod = NULL;
  254. data->WmiLibInfo.WmiFunctionControl = NULL;
  255. device->Flags |= DO_POWER_PAGABLE;
  256. device->Flags &= ~DO_DEVICE_INITIALIZING;
  257. return status;
  258. }
  259. NTSTATUS
  260. MouHid_StartDevice (
  261. IN PDEVICE_EXTENSION Data
  262. )
  263. /*++
  264. Routine Description:
  265. Arguments:
  266. Return Value:
  267. NTSTATUS result code.
  268. --*/
  269. {
  270. HIDP_CAPS caps; // the capabilities of the found hid device
  271. HID_COLLECTION_INFORMATION info;
  272. NTSTATUS status = STATUS_SUCCESS;
  273. PHIDP_PREPARSED_DATA preparsedData = NULL;
  274. PHID_EXTENSION hid = NULL;
  275. ULONG length, inputBufferLength, usageListLength;
  276. USHORT maxUsages;
  277. PCHAR buffer;
  278. USHORT slength;
  279. HIDP_VALUE_CAPS valueCaps;
  280. PAGED_CODE ();
  281. Print (DBG_PNP_TRACE, ("enter START Device \n"));
  282. //
  283. // Retrieve the capabilities of this hid device
  284. // IOCTL_HID_GET_COLLECTION_INFORMATION fills in HID_COLLECTION_INFORMATION.
  285. // we are interested in the Descriptor Size, which tells us how big a
  286. // buffer to allocate for the preparsed data.
  287. //
  288. if (!NT_SUCCESS (status = MouHid_CallHidClass (
  289. Data,
  290. IOCTL_HID_GET_COLLECTION_INFORMATION,
  291. 0, 0, // no input
  292. &info, sizeof (info)))) {
  293. goto MouHid_StartDeviceReject;
  294. }
  295. //
  296. // Allocate memory to hold the preparsed data.
  297. //
  298. preparsedData = (PHIDP_PREPARSED_DATA)
  299. ExAllocatePool (NonPagedPool, info.DescriptorSize);
  300. if (!preparsedData) {
  301. status = STATUS_INSUFFICIENT_RESOURCES;
  302. goto MouHid_StartDeviceReject;
  303. }
  304. //
  305. // Retrieve that information.
  306. //
  307. if (!NT_SUCCESS (status = MouHid_CallHidClass (
  308. Data,
  309. IOCTL_HID_GET_COLLECTION_DESCRIPTOR,
  310. 0, 0, // no input
  311. preparsedData, info.DescriptorSize))) {
  312. goto MouHid_StartDeviceReject;
  313. }
  314. //
  315. // Call the parser to determine the capabilites of this HID device.
  316. //
  317. if (!NT_SUCCESS (status = HidP_GetCaps (preparsedData, &caps))) {
  318. goto MouHid_StartDeviceReject;
  319. }
  320. //
  321. // Is this the thing we want?
  322. //
  323. // In this particular case we are looking for a keyboard.
  324. //
  325. if ( (HID_USAGE_PAGE_GENERIC == caps.UsagePage) &&
  326. ( (HID_USAGE_GENERIC_MOUSE == caps.Usage) ||
  327. ( (HID_USAGE_GENERIC_POINTER == caps.Usage) &&
  328. (!Globals.UseOnlyMice)))) {
  329. ;
  330. } else {
  331. //
  332. // Someone made an INF blunder!
  333. //
  334. ASSERT ( (HID_USAGE_PAGE_GENERIC == caps.UsagePage) &&
  335. ( (HID_USAGE_GENERIC_MOUSE == caps.Usage) ||
  336. ( (HID_USAGE_GENERIC_POINTER == caps.Usage) &&
  337. (!Globals.UseOnlyMice))));
  338. status = STATUS_UNSUCCESSFUL;
  339. goto MouHid_StartDeviceReject;
  340. }
  341. //
  342. // Note: here we might also want to check the button and value capabilities
  343. // of the device as well.
  344. //
  345. // Then let's use it.
  346. //
  347. //
  348. // a buffer length to allow an Input buffer, output buffer, feature buffer,
  349. // and the total number of usages that can be returned from a read packet.
  350. //
  351. maxUsages = (USHORT) HidP_MaxUsageListLength (HidP_Input,
  352. HID_USAGE_PAGE_BUTTON,
  353. preparsedData);
  354. //
  355. // Create space in the device extension for the buffer storage when working
  356. // with this HID device.
  357. //
  358. // We need four buffers to hold the button codes (length returned from
  359. // HidP_MaxUsageListLength) this will hold the current list of usages,
  360. // the previous list of usages, the ``Make'' and the ``Break'' lists.
  361. // We also need a place to put the input, output, and feature report
  362. // buffers.
  363. //
  364. if (maxUsages > (MAXULONG / sizeof(USAGE) )) {
  365. status = STATUS_UNSUCCESSFUL;
  366. goto MouHid_StartDeviceReject;
  367. }
  368. usageListLength = ALIGNPTRLEN(maxUsages * sizeof (USAGE));
  369. inputBufferLength = ALIGNPTRLEN(caps.InputReportByteLength);
  370. if ((MAXULONG - inputBufferLength < sizeof(HID_EXTENSION)) ||
  371. ((MAXULONG - inputBufferLength - sizeof(HID_EXTENSION))/ 4 < usageListLength)) {
  372. status = STATUS_UNSUCCESSFUL;
  373. goto MouHid_StartDeviceReject;
  374. }
  375. length = (4 * usageListLength)
  376. + inputBufferLength
  377. + sizeof (HID_EXTENSION);
  378. Data->HidExtension = hid = ExAllocatePool (NonPagedPool, length);
  379. if (!hid) {
  380. status = STATUS_INSUFFICIENT_RESOURCES;
  381. goto MouHid_StartDeviceReject;
  382. }
  383. RtlZeroMemory (hid, length);
  384. //
  385. // Initialize the fields.
  386. //
  387. hid->Ppd = preparsedData;
  388. hid->Caps = caps;
  389. hid->MaxUsages = maxUsages;
  390. Data->Attributes.NumberOfButtons = (USHORT) maxUsages;
  391. hid->InputBuffer = buffer = hid->Buffer;
  392. hid->PreviousUsageList = (PUSAGE) (buffer += inputBufferLength);
  393. hid->CurrentUsageList = (PUSAGE) (buffer += usageListLength);
  394. hid->BreakUsageList = (PUSAGE) (buffer += usageListLength);
  395. hid->MakeUsageList = (PUSAGE) (buffer + usageListLength);
  396. //
  397. // Create the MDLs
  398. // HidClass uses direct IO so you need MDLs
  399. //
  400. hid->InputMdl = IoAllocateMdl (hid->InputBuffer, // The virtual address
  401. caps.InputReportByteLength, // length
  402. FALSE, // No associated IRP => not secondary
  403. FALSE, // No quota charge
  404. 0); // No associated IRP
  405. if (NULL == hid->InputMdl) {
  406. status = STATUS_INSUFFICIENT_RESOURCES;
  407. goto MouHid_StartDeviceReject;
  408. }
  409. MmBuildMdlForNonPagedPool (hid->InputMdl); // Build this MDL.
  410. //
  411. // Determine if X,Y,Z values are absolute or relative for this device.
  412. // Only check X axis (assume Y,Z are the same -- we have no choice but
  413. // to make this assumption since the MOUSE_INPUT_DATA structure does
  414. // not accomodate mixed absolute/relative position fields).
  415. //
  416. slength = 1;
  417. if (!NT_SUCCESS (status = HidP_GetSpecificValueCaps(
  418. HidP_Input,
  419. HID_USAGE_PAGE_GENERIC,
  420. 0,
  421. HID_USAGE_GENERIC_X,
  422. &valueCaps,
  423. &slength,
  424. preparsedData) ) ) {
  425. goto MouHid_StartDeviceReject;
  426. }
  427. ASSERT (1 == slength);
  428. if (valueCaps.IsAbsolute) {
  429. if ((HID_USAGE_GENERIC_POINTER == caps.Usage) &&
  430. (Globals.TreatAbsolutePointerAsAbsolute)) {
  431. //
  432. // All pointers that declare themselfs as Absolute should be
  433. // treated as such, regardless of the TreatAbsoluteAsRelative flag
  434. //
  435. Data->InputData.Flags = MOUSE_MOVE_ABSOLUTE;
  436. hid->IsAbsolute = TRUE;
  437. } else if (Globals.TreatAbsoluteAsRelative) {
  438. //
  439. // Here we have overriden the HID descriptors absolute flag.
  440. // We will treat this as a relative device even though it claims
  441. // to be an absolute device.
  442. //
  443. Data->InputData.Flags = MOUSE_MOVE_RELATIVE;
  444. hid->IsAbsolute = FALSE;
  445. //
  446. // Report the problem with this mouse's report descriptor and
  447. // report it to the user.
  448. //
  449. Data->ProblemFlags |= PROBLEM_BAD_ABSOLUTE_FLAG_X_Y;
  450. MouHid_LogError(Data->Self->DriverObject,
  451. MOUHID_INVALID_ABSOLUTE_AXES,
  452. NULL);
  453. } else {
  454. //
  455. // No switches with which to play. Do what seems natural
  456. //
  457. Data->InputData.Flags = MOUSE_MOVE_ABSOLUTE;
  458. hid->IsAbsolute = TRUE;
  459. }
  460. } else {
  461. Data->InputData.Flags = MOUSE_MOVE_RELATIVE;
  462. hid->IsAbsolute = FALSE;
  463. }
  464. //
  465. // Determine X axis usage value's bit size.
  466. //
  467. hid->BitSize.X = valueCaps.BitSize;
  468. hid->MaxX = valueCaps.PhysicalMax;
  469. hid->MaxX = (hid->MaxX) ? (hid->MaxX) : ((1 << (hid->BitSize.X - 1)) - 1);
  470. //
  471. // Determine Y axis usage value's bit size.
  472. //
  473. slength = 1;
  474. if (!NT_SUCCESS (status = HidP_GetSpecificValueCaps(
  475. HidP_Input,
  476. HID_USAGE_PAGE_GENERIC,
  477. 0,
  478. HID_USAGE_GENERIC_Y,
  479. &valueCaps,
  480. &slength,
  481. preparsedData) ) ) {
  482. goto MouHid_StartDeviceReject;
  483. }
  484. ASSERT (1 == slength);
  485. hid->BitSize.Y = valueCaps.BitSize;
  486. hid->MaxY = valueCaps.PhysicalMax;
  487. hid->MaxY = (hid->MaxY) ? (hid->MaxY) : ((1 << (hid->BitSize.Y - 1)) - 1);
  488. //
  489. // Initialize wheel usage not-detected flag to false (determined later).
  490. //
  491. hid->HasNoWheelUsage = FALSE;
  492. hid->HasNoZUsage = FALSE;
  493. //
  494. // Determine Z axis usage value's bit size (if this is a wheel mouse).
  495. // Note that a Z axis may not exist, so we handle this case differently.
  496. //
  497. slength = 1;
  498. if (NT_SUCCESS (HidP_GetSpecificValueCaps(
  499. HidP_Input,
  500. HID_USAGE_PAGE_GENERIC,
  501. 0,
  502. HID_USAGE_GENERIC_WHEEL,
  503. &valueCaps,
  504. &slength,
  505. preparsedData) ) && slength == 1) {
  506. hid->BitSize.Z = valueCaps.BitSize;
  507. Data->Attributes.MouseIdentifier = WHEELMOUSE_HID_HARDWARE;
  508. } else {
  509. // hid->HasNoWheelUsage = TRUE;
  510. slength = 1;
  511. if (NT_SUCCESS (HidP_GetSpecificValueCaps(
  512. HidP_Input,
  513. HID_USAGE_PAGE_GENERIC,
  514. 0,
  515. HID_USAGE_GENERIC_Z,
  516. &valueCaps,
  517. &slength,
  518. preparsedData) ) && slength == 1) {
  519. hid->BitSize.Z = valueCaps.BitSize;
  520. Data->Attributes.MouseIdentifier = WHEELMOUSE_HID_HARDWARE;
  521. } else {
  522. // hid->HasNoZUsage = TRUE;
  523. hid->BitSize.Z = 0;
  524. }
  525. }
  526. //
  527. // We are done. Return peacefully.
  528. //
  529. return status;
  530. MouHid_StartDeviceReject:
  531. if (preparsedData) {
  532. // no need to set hid->Ppd to NULL becuase we will be freeing it as well
  533. ExFreePool (preparsedData);
  534. }
  535. if (hid) {
  536. if (hid->InputMdl) {
  537. IoFreeMdl (hid->InputMdl);
  538. }
  539. ExFreePool (hid);
  540. Data->HidExtension = NULL;
  541. }
  542. return status;
  543. }
  544. NTSTATUS
  545. MouHid_PnP (
  546. IN PDEVICE_OBJECT DeviceObject,
  547. IN PIRP Irp
  548. )
  549. /*++
  550. Routine Description:
  551. The plug and play dispatch routines.
  552. Most of these this filter driver will completely ignore.
  553. In all cases it must pass on the IRP to the lower driver.
  554. Arguments:
  555. DeviceObject - pointer to a device object.
  556. Irp - pointer to an I/O Request Packet.
  557. Return Value:
  558. NT status code
  559. --*/
  560. {
  561. PDEVICE_EXTENSION data;
  562. PHID_EXTENSION hid;
  563. PIO_STACK_LOCATION stack;
  564. NTSTATUS status;
  565. ULONG i, j;
  566. LONG ioCount;
  567. PDEVICE_EXTENSION * classDataList;
  568. PAGED_CODE ();
  569. data = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  570. stack = IoGetCurrentIrpStackLocation (Irp);
  571. hid = data->HidExtension;
  572. status = IoAcquireRemoveLock (&data->RemoveLock, Irp);
  573. if (!NT_SUCCESS (status)) {
  574. //
  575. // Someone gave us a pnp irp after a remove. Unthinkable!
  576. //
  577. ASSERT (FALSE);
  578. Irp->IoStatus.Information = 0;
  579. Irp->IoStatus.Status = status;
  580. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  581. return status;
  582. }
  583. Print(DBG_PNP_TRACE, ("PNP: Minor code = %x.", stack->MinorFunction));
  584. switch (stack->MinorFunction) {
  585. case IRP_MN_START_DEVICE:
  586. if (data->Started) {
  587. Print(DBG_PNP_INFO, ("PNP: Device already started." ));
  588. status = STATUS_SUCCESS;
  589. Irp->IoStatus.Status = status;
  590. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  591. break;
  592. }
  593. //
  594. // The device is starting.
  595. //
  596. // We cannot touch the device (send it any non pnp irps) until a
  597. // start device has been passed down to the lower drivers.
  598. //
  599. IoCopyCurrentIrpStackLocationToNext (Irp);
  600. KeInitializeEvent(&data->StartEvent, NotificationEvent, FALSE);
  601. IoSetCompletionRoutine (Irp,
  602. MouHid_PnPComplete,
  603. data,
  604. TRUE,
  605. TRUE,
  606. TRUE); // No need for Cancel
  607. Irp->IoStatus.Status = STATUS_SUCCESS;
  608. status = IoCallDriver (data->TopOfStack, Irp);
  609. if (STATUS_PENDING == status) {
  610. KeWaitForSingleObject(
  611. &data->StartEvent,
  612. Executive, // Waiting for reason of a driver
  613. KernelMode, // Waiting in kernel mode
  614. FALSE, // No allert
  615. NULL); // No timeout
  616. }
  617. if (NT_SUCCESS (status) && NT_SUCCESS (Irp->IoStatus.Status)) {
  618. //
  619. // As we are successfully now back from our start device
  620. // we can do work.
  621. //
  622. if (!data->Initialized) {
  623. status = MouHid_StartDevice (data);
  624. if (NT_SUCCESS (status)) {
  625. IoWMIRegistrationControl(DeviceObject,
  626. WMIREG_ACTION_REGISTER
  627. );
  628. data->Started = TRUE;
  629. data->Initialized = TRUE;
  630. }
  631. } else {
  632. data->Started = TRUE;
  633. }
  634. }
  635. //
  636. // We must now complete the IRP, since we stopped it in the
  637. // completetion routine with MORE_PROCESSING_REQUIRED.
  638. //
  639. Irp->IoStatus.Status = status;
  640. Irp->IoStatus.Information = 0;
  641. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  642. break;
  643. case IRP_MN_STOP_DEVICE:
  644. //
  645. // After the start IRP has been sent to the lower driver object, the
  646. // bus may NOT send any more IRPS down ``touch'' until another START
  647. // has occured.
  648. // What ever access is required must be done before the Irp is passed
  649. // on.
  650. //
  651. if (data->Started) {
  652. //
  653. // Do what ever
  654. //
  655. }
  656. //
  657. // We don't need a completion routine so fire and forget.
  658. //
  659. // Set the current stack location to the next stack location and
  660. // call the next device object.
  661. //
  662. //
  663. // Stop Device touching the hardware MouStopDevice(data, TRUE);
  664. //
  665. data->Started = FALSE;
  666. Irp->IoStatus.Status = STATUS_SUCCESS;
  667. IoSkipCurrentIrpStackLocation (Irp);
  668. status = IoCallDriver (data->TopOfStack, Irp);
  669. break;
  670. case IRP_MN_REMOVE_DEVICE:
  671. //
  672. // The PlugPlay system has dictacted the removal of this device. We
  673. // have no choise but to detach and delete the device objecct.
  674. // (If we wanted to express and interest in preventing this removal,
  675. // we should have filtered the query remove and query stop routines.)
  676. //
  677. // Note! we might receive a remove WITHOUT first receiving a stop.
  678. // ASSERT (!usbData->Removed);
  679. Print (DBG_PNP_TRACE, ("enter RemoveDevice \n"));
  680. IoWMIRegistrationControl(data->Self,
  681. WMIREG_ACTION_DEREGISTER
  682. );
  683. if (data->Started) {
  684. // Stop the device without touching the hardware.
  685. // MouStopDevice(data, FALSE);
  686. }
  687. //
  688. // Here if we had any outstanding requests in a personal queue we should
  689. // complete them all now.
  690. //
  691. // Note, the device could be GONE so we cannot send it any non-
  692. // PNP IRPS.
  693. //
  694. //
  695. // Cancel our read IRP. [DAN]
  696. // Note - waiting is only really necessary on 98, where pnp doesn't
  697. // make sure all handles are closed before sending the remove.
  698. //
  699. data->ShuttingDown = TRUE;
  700. KeWaitForSingleObject (&data->ReadSentEvent,
  701. Executive,
  702. KernelMode,
  703. FALSE,
  704. NULL
  705. );
  706. IoCancelIrp(data->ReadIrp);
  707. //
  708. // Send on the remove IRP
  709. //
  710. Irp->IoStatus.Status = STATUS_SUCCESS;
  711. IoSkipCurrentIrpStackLocation (Irp);
  712. status = IoCallDriver (data->TopOfStack, Irp);
  713. //
  714. // Wait for the remove lock to free.
  715. //
  716. IoReleaseRemoveLockAndWait (&data->RemoveLock, Irp);
  717. //
  718. // Free the associated memory.
  719. //
  720. IoFreeIrp (data->ReadIrp);
  721. if (hid) {
  722. //
  723. // If we are removed without being started then we will have
  724. // no hid extension
  725. //
  726. ExFreePool (hid->Ppd);
  727. IoFreeMdl (hid->InputMdl);
  728. ExFreePool (hid);
  729. }
  730. IoDetachDevice (data->TopOfStack);
  731. IoDeleteDevice (data->Self);
  732. return status;
  733. case IRP_MN_SURPRISE_REMOVAL:
  734. case IRP_MN_QUERY_REMOVE_DEVICE:
  735. case IRP_MN_CANCEL_REMOVE_DEVICE:
  736. case IRP_MN_QUERY_STOP_DEVICE:
  737. case IRP_MN_CANCEL_STOP_DEVICE:
  738. //
  739. // These IRPs have to have their status changed from
  740. // STATUS_NOT_SUPPORTED b4 passing them down.
  741. //
  742. Irp->IoStatus.Status = STATUS_SUCCESS;
  743. case IRP_MN_QUERY_DEVICE_RELATIONS:
  744. case IRP_MN_QUERY_INTERFACE:
  745. case IRP_MN_QUERY_CAPABILITIES:
  746. case IRP_MN_QUERY_RESOURCES:
  747. case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
  748. case IRP_MN_READ_CONFIG:
  749. case IRP_MN_WRITE_CONFIG:
  750. case IRP_MN_EJECT:
  751. case IRP_MN_SET_LOCK:
  752. case IRP_MN_QUERY_ID:
  753. case IRP_MN_QUERY_PNP_DEVICE_STATE:
  754. default:
  755. //
  756. // Here the filter driver might modify the behavior of these IRPS
  757. // Please see PlugPlay documentation for use of these IRPs.
  758. //
  759. IoSkipCurrentIrpStackLocation (Irp);
  760. status = IoCallDriver (data->TopOfStack, Irp);
  761. break;
  762. }
  763. IoReleaseRemoveLock (&data->RemoveLock, Irp);
  764. return status;
  765. }
  766. NTSTATUS
  767. MouHid_PnPComplete (
  768. IN PDEVICE_OBJECT DeviceObject,
  769. IN PIRP Irp,
  770. IN PVOID Context
  771. )
  772. /*++
  773. Routine Description:
  774. The pnp IRP is in the process of completing.
  775. signal
  776. Arguments:
  777. Context set to the device object in question.
  778. --*/
  779. {
  780. PIO_STACK_LOCATION stack;
  781. PDEVICE_EXTENSION data;
  782. NTSTATUS status;
  783. UNREFERENCED_PARAMETER (DeviceObject);
  784. status = STATUS_SUCCESS;
  785. data = (PDEVICE_EXTENSION) Context;
  786. stack = IoGetCurrentIrpStackLocation (Irp);
  787. if (Irp->PendingReturned) {
  788. IoMarkIrpPending( Irp );
  789. }
  790. switch (stack->MajorFunction) {
  791. case IRP_MJ_PNP:
  792. switch (stack->MinorFunction) {
  793. case IRP_MN_START_DEVICE:
  794. KeSetEvent (&data->StartEvent, 0, FALSE);
  795. //
  796. // Take the IRP back so that we can continue using it during
  797. // the IRP_MN_START_DEVICE dispatch routine.
  798. // NB: we will have to call IoCompleteRequest
  799. //
  800. return STATUS_MORE_PROCESSING_REQUIRED;
  801. default:
  802. break;
  803. }
  804. break;
  805. case IRP_MJ_POWER:
  806. default:
  807. break;
  808. }
  809. return status;
  810. }
  811. NTSTATUS
  812. MouHid_Power (
  813. IN PDEVICE_OBJECT DeviceObject,
  814. IN PIRP Irp
  815. )
  816. {
  817. PIO_STACK_LOCATION stack;
  818. NTSTATUS status;
  819. PDEVICE_EXTENSION data;
  820. POWER_STATE powerState;
  821. POWER_STATE_TYPE powerType;
  822. Print(DBG_POWER_TRACE, ("Power Enter." ));
  823. data = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  824. stack = IoGetCurrentIrpStackLocation (Irp);
  825. powerType = stack->Parameters.Power.Type;
  826. powerState = stack->Parameters.Power.State;
  827. status = IoAcquireRemoveLock (&data->RemoveLock, Irp);
  828. if (!NT_SUCCESS (status)) {
  829. PoStartNextPowerIrp (Irp);
  830. Irp->IoStatus.Status = status;
  831. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  832. return status;
  833. }
  834. switch (stack->MinorFunction) {
  835. case IRP_MN_SET_POWER:
  836. Print(DBG_POWER_INFO, ("Power Setting %s state to %d\n",
  837. ((powerType == SystemPowerState) ? "System"
  838. : "Device"),
  839. powerState.SystemState));
  840. break;
  841. case IRP_MN_QUERY_POWER:
  842. Print (DBG_POWER_INFO, ("Power query %s status to %d\n",
  843. ((powerType == SystemPowerState) ? "System"
  844. : "Device"),
  845. powerState.SystemState));
  846. break;
  847. default:
  848. Print (DBG_POWER_ERROR, ("Power minor (0x%x) no known\n",
  849. stack->MinorFunction));
  850. }
  851. PoStartNextPowerIrp (Irp);
  852. IoSkipCurrentIrpStackLocation (Irp);
  853. status = PoCallDriver (data->TopOfStack, Irp);
  854. IoReleaseRemoveLock (&data->RemoveLock, Irp);
  855. return status;
  856. }