|
|
1.1 ! root 1: /*++ ! 2: ! 3: Copyright (c) 1989-1993 Microsoft Corporation ! 4: ! 5: Module Name: ! 6: ! 7: devctx.c ! 8: ! 9: Abstract: ! 10: ! 11: This module contains code which implements the DEVICE_CONTEXT object. ! 12: Routines are provided to reference, and dereference transport device ! 13: context objects. ! 14: ! 15: The transport device context object is a structure which contains a ! 16: system-defined DEVICE_OBJECT followed by information which is maintained ! 17: by the transport provider, called the context. ! 18: ! 19: Environment: ! 20: ! 21: Kernel mode ! 22: ! 23: Revision History: ! 24: ! 25: --*/ ! 26: ! 27: #include "st.h" ! 28: ! 29: ! 30: VOID ! 31: StRefDeviceContext( ! 32: IN PDEVICE_CONTEXT DeviceContext ! 33: ) ! 34: ! 35: /*++ ! 36: ! 37: Routine Description: ! 38: ! 39: This routine increments the reference count on a device context. ! 40: ! 41: Arguments: ! 42: ! 43: DeviceContext - Pointer to a transport device context object. ! 44: ! 45: Return Value: ! 46: ! 47: none. ! 48: ! 49: --*/ ! 50: ! 51: { ! 52: ASSERT (DeviceContext->ReferenceCount > 0); // not perfect, but... ! 53: ! 54: (VOID)ExInterlockedIncrementLong ( ! 55: &DeviceContext->ReferenceCount, ! 56: &DeviceContext->Interlock); ! 57: ! 58: } /* StRefDeviceContext */ ! 59: ! 60: ! 61: VOID ! 62: StDerefDeviceContext( ! 63: IN PDEVICE_CONTEXT DeviceContext ! 64: ) ! 65: ! 66: /*++ ! 67: ! 68: Routine Description: ! 69: ! 70: This routine dereferences a device context by decrementing the ! 71: reference count contained in the structure. Currently, we don't ! 72: do anything special when the reference count drops to zero, but ! 73: we could dynamically unload stuff then. ! 74: ! 75: Arguments: ! 76: ! 77: DeviceContext - Pointer to a transport device context object. ! 78: ! 79: Return Value: ! 80: ! 81: none. ! 82: ! 83: --*/ ! 84: ! 85: { ! 86: INTERLOCKED_RESULT result; ! 87: ! 88: result = ExInterlockedDecrementLong ( ! 89: &DeviceContext->ReferenceCount, ! 90: &DeviceContext->Interlock); ! 91: ! 92: ASSERT (result != ResultNegative); ! 93: ! 94: if (result == ResultZero) { ! 95: StDestroyDeviceContext (DeviceContext); ! 96: } ! 97: ! 98: } /* StDerefDeviceContext */ ! 99: ! 100: ! 101: ! 102: NTSTATUS ! 103: StCreateDeviceContext( ! 104: IN PDRIVER_OBJECT DriverObject, ! 105: IN PUNICODE_STRING DeviceName, ! 106: IN OUT PDEVICE_CONTEXT *DeviceContext ! 107: ) ! 108: ! 109: /*++ ! 110: ! 111: Routine Description: ! 112: ! 113: This routine creates and initializes a device context structure. ! 114: ! 115: Arguments: ! 116: ! 117: ! 118: DriverObject - pointer to the IO subsystem supplied driver object. ! 119: ! 120: DeviceContext - Pointer to a pointer to a transport device context object. ! 121: ! 122: DeviceName - pointer to the name of the device this device object points to. ! 123: ! 124: Return Value: ! 125: ! 126: STATUS_SUCCESS if all is well; STATUS_INSUFFICIENT_RESOURCES otherwise. ! 127: ! 128: --*/ ! 129: ! 130: { ! 131: NTSTATUS status; ! 132: PDEVICE_OBJECT deviceObject; ! 133: PDEVICE_CONTEXT deviceContext; ! 134: USHORT i; ! 135: ! 136: ! 137: // ! 138: // Create the device object for the sample transport, allowing ! 139: // room at the end for the device name to be stored (for use ! 140: // in logging errors). ! 141: // ! 142: ! 143: status = IoCreateDevice( ! 144: DriverObject, ! 145: sizeof (DEVICE_CONTEXT) - sizeof (DEVICE_OBJECT) + ! 146: (DeviceName->Length + sizeof(UNICODE_NULL)), ! 147: DeviceName, ! 148: FILE_DEVICE_TRANSPORT, ! 149: 0, ! 150: FALSE, ! 151: &deviceObject); ! 152: ! 153: if (!NT_SUCCESS(status)) { ! 154: return status; ! 155: } ! 156: ! 157: deviceObject->Flags |= DO_DIRECT_IO; ! 158: ! 159: deviceContext = (PDEVICE_CONTEXT)deviceObject; ! 160: ! 161: // ! 162: // Initialize our part of the device context. ! 163: // ! 164: ! 165: RtlZeroMemory( ! 166: ((PUCHAR)deviceContext) + sizeof(DEVICE_OBJECT), ! 167: sizeof(DEVICE_CONTEXT) - sizeof(DEVICE_OBJECT)); ! 168: ! 169: // ! 170: // Copy over the device name. ! 171: // ! 172: ! 173: deviceContext->DeviceNameLength = DeviceName->Length + sizeof(WCHAR); ! 174: deviceContext->DeviceName = (PWCHAR)(deviceContext+1); ! 175: RtlCopyMemory( ! 176: deviceContext->DeviceName, ! 177: DeviceName->Buffer, ! 178: DeviceName->Length); ! 179: deviceContext->DeviceName[DeviceName->Length/sizeof(WCHAR)] = UNICODE_NULL; ! 180: ! 181: // ! 182: // Initialize the reference count. ! 183: // ! 184: ! 185: deviceContext->ReferenceCount = 1; ! 186: ! 187: // ! 188: // initialize the various fields in the device context ! 189: // ! 190: ! 191: KeInitializeSpinLock (&deviceContext->Interlock); ! 192: KeInitializeSpinLock (&deviceContext->SpinLock); ! 193: KeInitializeSpinLock (&deviceContext->StatisticsInterlock); ! 194: KeInitializeSpinLock (&deviceContext->StatisticsSpinLock); ! 195: ! 196: deviceContext->ControlChannelIdentifier = 1; ! 197: ! 198: InitializeListHead (&deviceContext->ConnectionPool); ! 199: InitializeListHead (&deviceContext->AddressPool); ! 200: InitializeListHead (&deviceContext->AddressFilePool); ! 201: InitializeListHead (&deviceContext->AddressDatabase); ! 202: InitializeListHead (&deviceContext->PacketWaitQueue); ! 203: InitializeListHead (&deviceContext->PacketizeQueue); ! 204: InitializeListHead (&deviceContext->RequestPool); ! 205: deviceContext->PacketPool.Next = NULL; ! 206: deviceContext->ReceivePacketPool.Next = NULL; ! 207: deviceContext->ReceiveBufferPool.Next = NULL; ! 208: InitializeListHead (&deviceContext->ReceiveInProgress); ! 209: InitializeListHead (&deviceContext->IrpCompletionQueue); ! 210: ! 211: ! 212: deviceContext->State = DEVICECONTEXT_STATE_CLOSED; ! 213: ! 214: // ! 215: // Initialize the resource that guards address ACLs. ! 216: // ! 217: ! 218: ExInitializeResource (&deviceContext->AddressResource); ! 219: ! 220: // ! 221: // set the netbios multicast address for this network type ! 222: // ! 223: ! 224: for (i=0; i<HARDWARE_ADDRESS_LENGTH; i++) { ! 225: deviceContext->LocalAddress.Address [i] = 0; // set later ! 226: deviceContext->MulticastAddress.Address [i] = 0; ! 227: } ! 228: ! 229: deviceContext->Type = ST_DEVICE_CONTEXT_SIGNATURE; ! 230: deviceContext->Size - sizeof (DEVICE_CONTEXT); ! 231: ! 232: *DeviceContext = deviceContext; ! 233: return STATUS_SUCCESS; ! 234: } ! 235: ! 236: ! 237: VOID ! 238: StDestroyDeviceContext( ! 239: IN PDEVICE_CONTEXT DeviceContext ! 240: ) ! 241: ! 242: /*++ ! 243: ! 244: Routine Description: ! 245: ! 246: This routine destroys a device context structure. ! 247: ! 248: Arguments: ! 249: ! 250: DeviceContext - Pointer to a pointer to a transport device context object. ! 251: ! 252: Return Value: ! 253: ! 254: None. ! 255: ! 256: --*/ ! 257: ! 258: { ! 259: ExDeleteResource (&DeviceContext->AddressResource); ! 260: IoDeleteDevice ((PDEVICE_OBJECT)DeviceContext); ! 261: return; ! 262: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.