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.

151 lines
3.3 KiB

  1. /*
  2. * UNIMODEM "Fakemodem" controllerless driver illustrative example
  3. *
  4. * (C) 2000 Microsoft Corporation
  5. * All Rights Reserved
  6. *
  7. */
  8. #include "fakemodem.h"
  9. NTSTATUS
  10. FakeModemOpen(
  11. IN PDEVICE_OBJECT DeviceObject,
  12. IN PIRP Irp
  13. )
  14. {
  15. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  16. NTSTATUS status=STATUS_UNSUCCESSFUL;
  17. KIRQL OldIrql;
  18. // make sure the device is ready for irp's
  19. status=CheckStateAndAddReference( DeviceObject, Irp);
  20. if (STATUS_SUCCESS != status) {
  21. // not accepting irp's. The irp has already been complted
  22. return status;
  23. }
  24. KeAcquireSpinLock(&deviceExtension->SpinLock, &OldIrql);
  25. deviceExtension->OpenCount++;
  26. if (deviceExtension->OpenCount != 1) {
  27. //
  28. // serial devices are exclusive
  29. //
  30. status=STATUS_ACCESS_DENIED;
  31. deviceExtension->OpenCount--;
  32. } else {
  33. //
  34. // ok to open, init some stuff
  35. //
  36. deviceExtension->ReadBufferBegin=0;
  37. deviceExtension->ReadBufferEnd=0;
  38. deviceExtension->BytesInReadBuffer=0;
  39. deviceExtension->CommandMatchState=COMMAND_MATCH_STATE_IDLE;
  40. deviceExtension->ModemStatus=SERIAL_DTR_STATE | SERIAL_DSR_STATE;
  41. deviceExtension->CurrentlyConnected=FALSE;
  42. deviceExtension->ConnectionStateChanged=FALSE;
  43. }
  44. KeReleaseSpinLock( &deviceExtension->SpinLock, OldIrql);
  45. Irp->IoStatus.Information = 0;
  46. RemoveReferenceAndCompleteRequest( DeviceObject, Irp, status);
  47. RemoveReferenceForDispatch(DeviceObject);
  48. return status;
  49. }
  50. NTSTATUS
  51. FakeModemClose(
  52. IN PDEVICE_OBJECT DeviceObject,
  53. IN PIRP Irp
  54. )
  55. {
  56. PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  57. NTSTATUS status=STATUS_SUCCESS;
  58. KIRQL OldIrql;
  59. KeAcquireSpinLock(&deviceExtension->SpinLock, &OldIrql);
  60. deviceExtension->OpenCount--;
  61. KeReleaseSpinLock(&deviceExtension->SpinLock, OldIrql);
  62. Irp->IoStatus.Status = status;
  63. Irp->IoStatus.Information = 0;
  64. IoCompleteRequest( Irp, IO_SERIAL_INCREMENT);
  65. return status;
  66. }
  67. NTSTATUS
  68. FakeModemCleanup(
  69. IN PDEVICE_OBJECT DeviceObject,
  70. IN PIRP Irp
  71. )
  72. {
  73. PDEVICE_EXTENSION extension = DeviceObject->DeviceExtension;
  74. NTSTATUS status=STATUS_SUCCESS;
  75. FakeModemKillPendingIrps(DeviceObject);
  76. Irp->IoStatus.Status = status;
  77. Irp->IoStatus.Information = 0;
  78. IoCompleteRequest( Irp, IO_SERIAL_INCREMENT);
  79. return status;
  80. }
  81. void
  82. FakeModemKillPendingIrps(
  83. PDEVICE_OBJECT DeviceObject
  84. )
  85. {
  86. PDEVICE_EXTENSION pDeviceExtension = DeviceObject->DeviceExtension;
  87. KIRQL oldIrql;
  88. // Kill all reads
  89. FakeModemKillAllReadsOrWrites(DeviceObject,
  90. &pDeviceExtension->ReadQueue, &pDeviceExtension->CurrentReadIrp);
  91. // Kill all writes
  92. FakeModemKillAllReadsOrWrites(DeviceObject,
  93. &pDeviceExtension->WriteQueue, &pDeviceExtension->CurrentWriteIrp);
  94. // Remove any mask operations
  95. FakeModemKillAllReadsOrWrites(DeviceObject,
  96. &pDeviceExtension->MaskQueue, &pDeviceExtension->CurrentMaskIrp);
  97. }