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.

237 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 1989-1993 Microsoft Corporation
  3. Module Name:
  4. spxdev.c
  5. Abstract:
  6. This module contains code which implements the DEVICE_CONTEXT object.
  7. Routines are provided to reference, and dereference transport device
  8. context objects.
  9. The transport device context object is a structure which contains a
  10. system-defined DEVICE_OBJECT followed by information which is maintained
  11. by the transport provider, called the context.
  12. Author:
  13. Nikhil Kamkolkar (nikhilk) 11-November-1993
  14. Environment:
  15. Kernel mode
  16. Revision History:
  17. --*/
  18. #include "precomp.h"
  19. #pragma hdrstop
  20. // Define module number for event logging entries
  21. #define FILENUM SPXDEV
  22. #ifdef ALLOC_PRAGMA
  23. #pragma alloc_text(INIT, SpxInitCreateDevice)
  24. #pragma alloc_text(PAGE, SpxDestroyDevice)
  25. #endif
  26. VOID
  27. SpxDerefDevice(
  28. IN PDEVICE Device
  29. )
  30. /*++
  31. Routine Description:
  32. This routine dereferences a device context by decrementing the
  33. reference count contained in the structure. Currently, we don't
  34. do anything special when the reference count drops to zero, but
  35. we could dynamically unload stuff then.
  36. Arguments:
  37. Device - Pointer to a transport device context object.
  38. Return Value:
  39. none.
  40. --*/
  41. {
  42. LONG result;
  43. result = InterlockedDecrement (&Device->dev_RefCount);
  44. CTEAssert (result >= 0);
  45. if (result == 0)
  46. {
  47. // Close binding to IPX
  48. SpxUnbindFromIpx();
  49. // Set unload event.
  50. KeSetEvent(&SpxUnloadEvent, IO_NETWORK_INCREMENT, FALSE);
  51. }
  52. } // SpxDerefDevice
  53. NTSTATUS
  54. SpxInitCreateDevice(
  55. IN PDRIVER_OBJECT DriverObject,
  56. IN PUNICODE_STRING DeviceName
  57. )
  58. /*++
  59. Routine Description:
  60. This routine creates and initializes a device context structure.
  61. Arguments:
  62. DriverObject - pointer to the IO subsystem supplied driver object.
  63. Device - Pointer to a pointer to a transport device context object.
  64. DeviceName - pointer to the name of the device this device object points to.
  65. Return Value:
  66. STATUS_SUCCESS if all is well; STATUS_INSUFFICIENT_RESOURCES otherwise.
  67. --*/
  68. {
  69. NTSTATUS status;
  70. PDEVICE Device;
  71. ULONG DeviceNameOffset;
  72. DBGPRINT(DEVICE, INFO,
  73. ("SpxInitCreateDevice - Create device %ws\n", DeviceName->Buffer));
  74. // Create the device object for the sample transport, allowing
  75. // room at the end for the device name to be stored (for use
  76. // in logging errors).
  77. SpxDevice = SpxAllocateMemory(sizeof (DEVICE) + DeviceName->Length + sizeof(UNICODE_NULL));
  78. if (!SpxDevice) {
  79. DbgPrint("SPX: FATAL Error: cant allocate Device Structure\n");
  80. return STATUS_INSUFFICIENT_RESOURCES;
  81. }
  82. Device = (PDEVICE)SpxDevice;
  83. RtlZeroMemory(SpxDevice, sizeof (DEVICE) + DeviceName->Length + sizeof(UNICODE_NULL) );
  84. //
  85. // This is the closest we can set the provider info [ShreeM]
  86. //
  87. SpxQueryInitProviderInfo(&Device->dev_ProviderInfo);
  88. DBGPRINT(DEVICE, INFO, ("IoCreateDevice succeeded %lx\n", Device));
  89. // Initialize our part of the device context.
  90. RtlZeroMemory(
  91. ((PUCHAR)Device) + sizeof(DEVICE_OBJECT),
  92. sizeof(DEVICE) - sizeof(DEVICE_OBJECT));
  93. DeviceNameOffset = sizeof(DEVICE);
  94. // Copy over the device name.
  95. Device->dev_DeviceNameLen = DeviceName->Length + sizeof(WCHAR);
  96. Device->dev_DeviceName = (PWCHAR)(((PUCHAR)Device) + DeviceNameOffset);
  97. RtlCopyMemory(
  98. Device->dev_DeviceName,
  99. DeviceName->Buffer,
  100. DeviceName->Length);
  101. Device->dev_DeviceName[DeviceName->Length/sizeof(WCHAR)] = UNICODE_NULL;
  102. // Initialize the reference count.
  103. Device->dev_RefCount = 1;
  104. #if DBG
  105. Device->dev_RefTypes[DREF_CREATE] = 1;
  106. #endif
  107. #if DBG
  108. RtlCopyMemory(Device->dev_Signature1, "IDC1", 4);
  109. RtlCopyMemory(Device->dev_Signature2, "IDC2", 4);
  110. #endif
  111. // Set next conn id to be used.
  112. Device->dev_NextConnId = (USHORT)SpxRandomNumber();
  113. if (Device->dev_NextConnId == 0xFFFF)
  114. {
  115. Device->dev_NextConnId = 1;
  116. }
  117. DBGPRINT(DEVICE, ERR,
  118. ("SpxInitCreateDevice: Start Conn Id %lx\n", Device->dev_NextConnId));
  119. // Initialize the resource that guards address ACLs.
  120. ExInitializeResourceLite (&Device->dev_AddrResource);
  121. // initialize the various fields in the device context
  122. CTEInitLock (&Device->dev_Interlock);
  123. CTEInitLock (&Device->dev_Lock);
  124. KeInitializeSpinLock (&Device->dev_StatInterlock);
  125. KeInitializeSpinLock (&Device->dev_StatSpinLock);
  126. Device->dev_State = DEVICE_STATE_CLOSED;
  127. Device->dev_Type = SPX_DEVICE_SIGNATURE;
  128. Device->dev_Size = sizeof (DEVICE);
  129. Device->dev_Stat.Version = 0x100;
  130. return STATUS_SUCCESS;
  131. } // SpxCreateDevice
  132. VOID
  133. SpxDestroyDevice(
  134. IN PDEVICE Device
  135. )
  136. /*++
  137. Routine Description:
  138. This routine destroys a device context structure.
  139. Arguments:
  140. Device - Pointer to a pointer to a transport device context object.
  141. Return Value:
  142. None.
  143. --*/
  144. {
  145. ExDeleteResourceLite (&Device->dev_AddrResource);
  146. IoDeleteDevice ((PDEVICE_OBJECT)SpxDevice->dev_DevObj);
  147. SpxFreeMemory(SpxDevice);
  148. } // SpxDestroyDevice