NTSTATUS VolumeFilterAddDevice (PDRIVER_OBJECT driverObject, PDEVICE_OBJECT pdo) { VolumeFilterExtension *Extension; NTSTATUS status; PDEVICE_OBJECT filterDeviceObject = NULL; PDEVICE_OBJECT attachedDeviceObject; Dump ("VolumeFilterAddDevice pdo=%p\n", pdo); attachedDeviceObject = IoGetAttachedDeviceReference (pdo); status = IoCreateDevice (driverObject, sizeof (VolumeFilterExtension), NULL, attachedDeviceObject->DeviceType, 0, FALSE, &filterDeviceObject); ObDereferenceObject (attachedDeviceObject); if (!NT_SUCCESS (status)) { filterDeviceObject = NULL; goto err; } Extension = (VolumeFilterExtension *) filterDeviceObject->DeviceExtension; memset (Extension, 0, sizeof (VolumeFilterExtension)); status = IoAttachDeviceToDeviceStackSafe (filterDeviceObject, pdo, &(Extension->LowerDeviceObject)); if (status != STATUS_SUCCESS) { goto err; } if (!Extension->LowerDeviceObject) { status = STATUS_DEVICE_REMOVED; goto err; } Extension->IsVolumeFilterDevice = TRUE; Extension->DeviceObject = filterDeviceObject; Extension->Pdo = pdo; IoInitializeRemoveLock (&Extension->Queue.RemoveLock, 'LRCV', 0, 0); filterDeviceObject->Flags |= Extension->LowerDeviceObject->Flags & (DO_DIRECT_IO | DO_BUFFERED_IO | DO_POWER_PAGABLE); filterDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING; return status; err: if (filterDeviceObject) { if (Extension->LowerDeviceObject) IoDetachDevice (Extension->LowerDeviceObject); IoDeleteDevice (filterDeviceObject); } return status; }
NTSTATUS NTAPI ProcessorAddDevice( IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT Pdo) { PDEVICE_EXTENSION DeviceExtension = NULL; PDEVICE_OBJECT Fdo = NULL; NTSTATUS Status; DPRINT("ProcessorAddDevice()\n"); ASSERT(DriverObject); ASSERT(Pdo); /* Create functional device object */ Status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), NULL, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &Fdo); if (NT_SUCCESS(Status)) { DeviceExtension = (PDEVICE_EXTENSION)Fdo->DeviceExtension; RtlZeroMemory(DeviceExtension, sizeof(DEVICE_EXTENSION)); DeviceExtension->DeviceObject = Fdo; Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice); if (!NT_SUCCESS(Status)) { DPRINT1("IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status); IoDeleteDevice(Fdo); return Status; } Fdo->Flags |= DO_DIRECT_IO; Fdo->Flags |= DO_POWER_PAGABLE; Fdo->Flags &= ~DO_DEVICE_INITIALIZING; } return Status; }
static NTSTATUS NTAPI PortAddDevice( _In_ PDRIVER_OBJECT DriverObject, _In_ PDEVICE_OBJECT PhysicalDeviceObject) { PDRIVER_OBJECT_EXTENSION DriverObjectExtension; PFDO_DEVICE_EXTENSION DeviceExtension = NULL; WCHAR NameBuffer[80]; UNICODE_STRING DeviceName; PDEVICE_OBJECT Fdo = NULL; KLOCK_QUEUE_HANDLE LockHandle; NTSTATUS Status; DPRINT1("PortAddDevice(%p %p)\n", DriverObject, PhysicalDeviceObject); ASSERT(DriverObject); ASSERT(PhysicalDeviceObject); swprintf(NameBuffer, L"\\Device\\RaidPort%lu", PortNumber); RtlInitUnicodeString(&DeviceName, NameBuffer); PortNumber++; DPRINT1("Creating device: %wZ\n", &DeviceName); /* Create the port device */ Status = IoCreateDevice(DriverObject, sizeof(FDO_DEVICE_EXTENSION), &DeviceName, FILE_DEVICE_CONTROLLER, FILE_DEVICE_SECURE_OPEN, FALSE, &Fdo); if (!NT_SUCCESS(Status)) { DPRINT1("IoCreateDevice() failed (Status 0x%08lx)\n", Status); return Status; } DPRINT1("Created device: %wZ (%p)\n", &DeviceName, Fdo); /* Initialize the device */ Fdo->Flags |= DO_DIRECT_IO; Fdo->Flags |= DO_POWER_PAGABLE; /* Initialize the device extension */ DeviceExtension = (PFDO_DEVICE_EXTENSION)Fdo->DeviceExtension; RtlZeroMemory(DeviceExtension, sizeof(FDO_DEVICE_EXTENSION)); DeviceExtension->ExtensionType = FdoExtension; DeviceExtension->Device = Fdo; DeviceExtension->PhysicalDevice = PhysicalDeviceObject; DeviceExtension->PnpState = dsStopped; /* Attach the FDO to the device stack */ Status = IoAttachDeviceToDeviceStackSafe(Fdo, PhysicalDeviceObject, &DeviceExtension->LowerDevice); if (!NT_SUCCESS(Status)) { DPRINT1("IoAttachDeviceToDeviceStackSafe() failed (Status 0x%08lx)\n", Status); IoDeleteDevice(Fdo); return Status; } /* Insert the FDO to the drivers FDO list */ DriverObjectExtension = IoGetDriverObjectExtension(DriverObject, (PVOID)DriverEntry); ASSERT(DriverObjectExtension->ExtensionType == DriverExtension); DeviceExtension->DriverExtension = DriverObjectExtension; KeAcquireInStackQueuedSpinLock(&DriverObjectExtension->AdapterListLock, &LockHandle); InsertHeadList(&DriverObjectExtension->AdapterListHead, &DeviceExtension->AdapterListEntry); DriverObjectExtension->AdapterCount++; KeReleaseInStackQueuedSpinLock(&LockHandle); /* The device has been initialized */ Fdo->Flags &= ~DO_DEVICE_INITIALIZING; DPRINT1("PortAddDevice() done (Status 0x%08lx)\n", Status); return Status; }
NTSTATUS NTAPI SermouseAddDevice( IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT Pdo) { PSERMOUSE_DRIVER_EXTENSION DriverExtension; PDEVICE_OBJECT Fdo; PSERMOUSE_DEVICE_EXTENSION DeviceExtension = NULL; NTSTATUS Status; TRACE_(SERMOUSE, "SermouseAddDevice called. Pdo = 0x%p\n", Pdo); if (Pdo == NULL) return STATUS_SUCCESS; /* Create new device object */ DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject); Status = IoCreateDevice( DriverObject, sizeof(SERMOUSE_DEVICE_EXTENSION), NULL, FILE_DEVICE_SERIAL_MOUSE_PORT, FILE_DEVICE_SECURE_OPEN, TRUE, &Fdo); if (!NT_SUCCESS(Status)) { WARN_(SERMOUSE, "IoCreateDevice() failed with status 0x%08lx\n", Status); goto cleanup; } DeviceExtension = (PSERMOUSE_DEVICE_EXTENSION)Fdo->DeviceExtension; RtlZeroMemory(DeviceExtension, sizeof(SERMOUSE_DEVICE_EXTENSION)); DeviceExtension->MouseType = mtNone; DeviceExtension->PnpState = dsStopped; DeviceExtension->DriverExtension = DriverExtension; KeInitializeEvent(&DeviceExtension->StopWorkerThreadEvent, NotificationEvent, FALSE); Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice); if (!NT_SUCCESS(Status)) { WARN_(SERMOUSE, "IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status); goto cleanup; } if (DeviceExtension->LowerDevice->Flags & DO_POWER_PAGABLE) Fdo->Flags |= DO_POWER_PAGABLE; if (DeviceExtension->LowerDevice->Flags & DO_BUFFERED_IO) Fdo->Flags |= DO_BUFFERED_IO; if (DeviceExtension->LowerDevice->Flags & DO_DIRECT_IO) Fdo->Flags |= DO_DIRECT_IO; Fdo->Flags &= ~DO_DEVICE_INITIALIZING; return STATUS_SUCCESS; cleanup: if (DeviceExtension) { if (DeviceExtension->LowerDevice) IoDetachDevice(DeviceExtension->LowerDevice); } if (Fdo) { IoDeleteDevice(Fdo); } return Status; }
NTSTATUS NTAPI i8042AddDevice( IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT Pdo) { PI8042_DRIVER_EXTENSION DriverExtension; PFDO_DEVICE_EXTENSION DeviceExtension = NULL; PDEVICE_OBJECT Fdo = NULL; ULONG DeviceExtensionSize; NTSTATUS Status; TRACE_(I8042PRT, "i8042AddDevice(%p %p)\n", DriverObject, Pdo); DriverExtension = (PI8042_DRIVER_EXTENSION)IoGetDriverObjectExtension(DriverObject, DriverObject); if (Pdo == NULL) { /* We're getting a NULL Pdo at the first call as * we are a legacy driver. Ignore it */ return STATUS_SUCCESS; } /* Create new device object. As we don't know if the device would be a keyboard * or a mouse, we have to allocate the biggest device extension. */ DeviceExtensionSize = MAX(sizeof(I8042_KEYBOARD_EXTENSION), sizeof(I8042_MOUSE_EXTENSION)); Status = IoCreateDevice( DriverObject, DeviceExtensionSize, NULL, Pdo->DeviceType, FILE_DEVICE_SECURE_OPEN, TRUE, &Fdo); if (!NT_SUCCESS(Status)) { WARN_(I8042PRT, "IoCreateDevice() failed with status 0x%08lx\n", Status); goto cleanup; } DeviceExtension = (PFDO_DEVICE_EXTENSION)Fdo->DeviceExtension; RtlZeroMemory(DeviceExtension, DeviceExtensionSize); DeviceExtension->Type = Unknown; DeviceExtension->Fdo = Fdo; DeviceExtension->Pdo = Pdo; DeviceExtension->PortDeviceExtension = &DriverExtension->Port; Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice); if (!NT_SUCCESS(Status)) { WARN_(I8042PRT, "IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status); goto cleanup; } ExInterlockedInsertTailList( &DriverExtension->DeviceListHead, &DeviceExtension->ListEntry, &DriverExtension->DeviceListLock); Fdo->Flags &= ~DO_DEVICE_INITIALIZING; return STATUS_SUCCESS; cleanup: if (DeviceExtension && DeviceExtension->LowerDevice) IoDetachDevice(DeviceExtension->LowerDevice); if (Fdo) IoDeleteDevice(Fdo); return Status; }
NTSTATUS FsFilterAttachToDevice( __in PDEVICE_OBJECT DeviceObject, __out_opt PDEVICE_OBJECT* pFilterDeviceObject ) { NTSTATUS status = STATUS_SUCCESS; PDEVICE_OBJECT filterDeviceObject = NULL; PFSFILTER_DEVICE_EXTENSION pDevExt = NULL; ULONG i = 0; ASSERT(!FsFilterIsAttachedToDevice(DeviceObject)); // // Create a new device object we can attach with. // status = IoCreateDevice( g_fsFilterDriverObject, sizeof(FSFILTER_DEVICE_EXTENSION), NULL, DeviceObject->DeviceType, 0, FALSE, &filterDeviceObject); if (!NT_SUCCESS(status)) { return status; } pDevExt = (PFSFILTER_DEVICE_EXTENSION)filterDeviceObject->DeviceExtension; // // Propagate flags from Device Object we are trying to attach to. // if (FlagOn(DeviceObject->Flags, DO_BUFFERED_IO)) { SetFlag(filterDeviceObject->Flags, DO_BUFFERED_IO); } if (FlagOn(DeviceObject->Flags, DO_DIRECT_IO)) { SetFlag(filterDeviceObject->Flags, DO_DIRECT_IO); } if (FlagOn(DeviceObject->Characteristics, FILE_DEVICE_SECURE_OPEN)) { SetFlag(filterDeviceObject->Characteristics, FILE_DEVICE_SECURE_OPEN); } // // Do the attachment. // // It is possible for this attachment request to fail because this device // object has not finished initializing. This can occur if this filter // loaded just as this volume was being mounted. // for (i = 0; i < 8; ++i) { LARGE_INTEGER interval; status = IoAttachDeviceToDeviceStackSafe( filterDeviceObject, DeviceObject, &pDevExt->AttachedToDeviceObject); if (NT_SUCCESS(status)) { break; } // // Delay, giving the device object a chance to finish its // initialization so we can try again. // interval.QuadPart = (500 * DELAY_ONE_MILLISECOND); KeDelayExecutionThread(KernelMode, FALSE, &interval); } if (!NT_SUCCESS(status)) { // // Clean up. // IoDeleteDevice(filterDeviceObject); filterDeviceObject = NULL; } else { // // Mark we are done initializing. // ClearFlag(filterDeviceObject->Flags, DO_DEVICE_INITIALIZING); if (NULL != pFilterDeviceObject) { *pFilterDeviceObject = filterDeviceObject; } } return status; }
NTSTATUS CreateAndAttachDevice(PDRIVER_OBJECT pDriverObject) { NTSTATUS status = STATUS_SUCCESS; PDEVICE_OBJECT pDevObj = NULL, pLBKdev = NULL; PDEVICE_EXTENSION pDevExt = NULL; UNICODE_STRING devNameFlt = {0}, devName = {0}; UNICODE_STRING symLinkNameFlt = {0}; PFILE_OBJECT pFileObject = NULL; /* * Form the Device Name and symbolic name */ RtlInitUnicodeString(&devNameFlt, L"\\Device\\LOOPBACKFLT"); RtlInitUnicodeString(&symLinkNameFlt, L"\\DosDevices\\LBKFLT"); RtlInitUnicodeString(&devName, L"\\Device\\LOOPBACK"); /* * Now create the device */ status = IoCreateDevice( pDriverObject, sizeof(DEVICE_EXTENSION), &devNameFlt, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj ); if (!NT_SUCCESS(status)){ goto cleanup_failure; } /* * Retrieve device extension pointer from device object */ pDevExt=(PDEVICE_EXTENSION)pDevObj->DeviceExtension; /* * Attach device to loopback device stack */ /*---------------------- Alternatives Start---------------------------------------------------------*/ /*---------------------- Alternative 1-------------------------------------------------------------*/ /*status = IoAttachDevice(pDevObj, &devName, &(pDevExt->pTargetDeviceObject)); if(status != STATUS_SUCCESS){ DbgPrint("IoAttachDevice failed with error = 0x%0x\n", status); goto cleanup_failure; }*/ /*---------------------- Alternative 2-------------------------------------------------------------*/ /* * Get Device Pointer */ /*status = IoGetDeviceObjectPointer( &devName, FILE_ALL_ACCESS, &pFileObject, &(pLBKdev)); if(status != STATUS_SUCCESS){ DbgPrint("IoGetDeviceObjectPointer failed with error = 0x%0x\n", status); goto cleanup_failure; } ASSERT(pLBKdev); DbgPrint("%wZ Device Object Pointer 0x%0x\n", &devName, pLBKdev); ObReferenceObject(pLBKdev); pDevExt->pTargetDeviceObject = IoAttachDeviceToDeviceStack( pDevObj, pLBKdev); if(pDevExt->pTargetDeviceObject){ DbgPrint("Attached to loopback device successfully\n"); status = STATUS_UNSUCCESSFUL; goto cleanup_failure; }else{ DbgPrint("Attaching to loopback device failed\n"); }*/ /*---------------------- Alternative 3--------------------------------------------------------------*/ /* * Get Device Pointer */ status = IoGetDeviceObjectPointer( &devName, FILE_ALL_ACCESS, &pFileObject, &(pLBKdev) ); if(status != STATUS_SUCCESS){ DbgPrint("IoGetDeviceObjectPointer failed with error = 0x%0x\n", status); goto cleanup_failure; } ASSERT(pLBKdev); DbgPrint("%wZ Device Object Pointer 0x%0x\n", &devName, pLBKdev); ObReferenceObject(pLBKdev); status = IoAttachDeviceToDeviceStackSafe(pDevObj, pLBKdev, &pDevExt->pTargetDeviceObject); if(status != STATUS_SUCCESS){ DbgPrint("IoGetDeviceObjectPointer failed with error = 0x%0x\n", status); goto cleanup_failure; } /*---------------------- Way End--------------------------------------------------------------*/ /* * Propogate device flags */ pDevObj->Flags = pDevExt->pTargetDeviceObject->Flags; /* * Create the symbolic link name, this is not mandatory * but can be helpful for user mode apps to communicate */ status = IoCreateSymbolicLink( &symLinkNameFlt, &devNameFlt ); if (!NT_SUCCESS(status)) { // if it fails now, must delete Device object DbgPrint("IoCreateSymbolicLink failed with error = 0x%0x\n", status); goto cleanup_failure; } /* * Fill Device extension information */ pDevExt->pDevice = pDevObj; pDevExt->pFileObject = pFileObject; pDevExt->pLBKdev = pLBKdev; RtlInitUnicodeString(&pDevExt->devName, L"\\Device\\LOOPBACKFLT"); RtlInitUnicodeString(&pDevExt->symLinkName, L"\\DosDevices\\LBKFLT"); goto cleanup; cleanup_failure: if(pDevObj) IoDeleteDevice(pDevObj); if(pFileObject) ObDereferenceObject(pFileObject); if(pLBKdev) ObDereferenceObject(pDevObj); cleanup: return status; }
NTSTATUS NTAPI PciIdeXAddDevice( IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT Pdo) { PPCIIDEX_DRIVER_EXTENSION DriverExtension; PFDO_DEVICE_EXTENSION DeviceExtension; PDEVICE_OBJECT Fdo; ULONG BytesRead; PCI_COMMON_CONFIG PciConfig; NTSTATUS Status; DPRINT("PciIdeXAddDevice(%p %p)\n", DriverObject, Pdo); DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject); ASSERT(DriverExtension); Status = IoCreateDevice( DriverObject, sizeof(FDO_DEVICE_EXTENSION) + DriverExtension->MiniControllerExtensionSize, NULL, FILE_DEVICE_BUS_EXTENDER, FILE_DEVICE_SECURE_OPEN, TRUE, &Fdo); if (!NT_SUCCESS(Status)) { DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status); return Status; } DeviceExtension = (PFDO_DEVICE_EXTENSION)Fdo->DeviceExtension; RtlZeroMemory(DeviceExtension, sizeof(FDO_DEVICE_EXTENSION)); DeviceExtension->Common.IsFDO = TRUE; Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice); if (!NT_SUCCESS(Status)) { DPRINT("IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status); return Status; } Status = GetBusInterface(DeviceExtension); if (!NT_SUCCESS(Status)) { DPRINT("GetBusInterface() failed with status 0x%08lx\n", Status); IoDetachDevice(DeviceExtension->LowerDevice); return Status; } BytesRead = (*DeviceExtension->BusInterface->GetBusData)( DeviceExtension->BusInterface->Context, PCI_WHICHSPACE_CONFIG, &PciConfig, 0, PCI_COMMON_HDR_LENGTH); if (BytesRead != PCI_COMMON_HDR_LENGTH) { DPRINT("BusInterface->GetBusData() failed()\n"); ReleaseBusInterface(DeviceExtension); IoDetachDevice(DeviceExtension->LowerDevice); return STATUS_IO_DEVICE_ERROR; } DeviceExtension->VendorId = PciConfig.VendorID; DeviceExtension->DeviceId = PciConfig.DeviceID; Fdo->Flags &= ~DO_DEVICE_INITIALIZING; return STATUS_SUCCESS; }