📄 pnp.c
字号:
}
if (NT_SUCCESS(ntStatus)) {
deviceExtension->UsbDeviceDescriptor = deviceDescriptor;
} else if (deviceDescriptor) {
ExFreePool(deviceDescriptor);
}
ExFreePool(urb);
} else {
// if we got here we failed to allocate the urb
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
if (NT_SUCCESS(ntStatus)) {
ntStatus = UsbCom_ConfigureDevice(DeviceObject);
}
if (NT_SUCCESS(ntStatus)) {
deviceExtension->DeviceStarted = TRUE;
}
//
// Set up the default device control fields.
// Note that if the values are changed after
// the file is open, they do NOT revert back
// to the old value at file close.
//
deviceExtension->SpecialChars.XonChar = SERIAL_DEF_XON;
deviceExtension->SpecialChars.XoffChar = SERIAL_DEF_XOFF;
deviceExtension->HandFlow.ControlHandShake = SERIAL_DTR_CONTROL;
deviceExtension->HandFlow.FlowReplace = SERIAL_RTS_CONTROL;
//
// Default Line control protocol. 7E1
//
// Seven data bits.
// Even parity.
// 1 Stop bits.
//
deviceExtension->LineControl = SERIAL_7_DATA |
SERIAL_EVEN_PARITY |
SERIAL_NONE_PARITY;
deviceExtension->ValidDataMask = 0x7f;
deviceExtension->CurrentBaud = 1200;
//
// We set up the default xon/xoff limits.
//
// This may be a bogus value. It looks like the BufferSize
// is not set up until the device is actually opened.
//
deviceExtension->HandFlow.XoffLimit = deviceExtension->BufferSize >> 3;
deviceExtension->HandFlow.XonLimit = deviceExtension->BufferSize >> 1;
deviceExtension->BufferSizePt8 = ((3*(deviceExtension->BufferSize>>2))+
(deviceExtension->BufferSize>>4));
SerialDump(
SERDIAG1,
("SERIAL: The default interrupt read buffer size is: %d\n"
"------ The XoffLimit is : %d\n"
"------ The XonLimit is : %d\n"
"------ The pt 8 size is : %d\n",
deviceExtension->BufferSize,
deviceExtension->HandFlow.XoffLimit,
deviceExtension->HandFlow.XonLimit,
deviceExtension->BufferSizePt8)
);
//
// Go through all the "named" baud rates to find out which ones
// can be supported with this port.
//
deviceExtension->SupportedBauds = SERIAL_BAUD_USER;
deviceExtension->SupportedBauds = SERIAL_BAUD_110 |
SERIAL_BAUD_300 |
SERIAL_BAUD_1200 |
SERIAL_BAUD_2400 |
SERIAL_BAUD_4800 |
SERIAL_BAUD_9600 |
SERIAL_BAUD_19200 |
SERIAL_BAUD_38400 |
SERIAL_BAUD_57600 |
SERIAL_BAUD_115200;
//
// Mark this device as not being opened by anyone. We keep a
// variable around so that spurious interrupts are easily
// dismissed by the ISR.
//
deviceExtension->DeviceIsOpened = FALSE;
//
// Store values into the extension for interval timing.
//
//
// If the interval timer is less than a second then come
// in with a short "polling" loop.
//
// For large (> then 2 seconds) use a 1 second poller.
//
deviceExtension->ShortIntervalAmount.QuadPart = -1;
deviceExtension->LongIntervalAmount.QuadPart = -10000000;
deviceExtension->CutOverAmount.QuadPart = 200000000;
StartInterruptUrb(deviceExtension); // henry
StartReadUrb(deviceExtension); // henry
deviceExtension->HoldingEmpty = TRUE; //henry
//done:
DbgPrint("exit UsbCom_StartDevice (%x)\n", ntStatus);
return ntStatus;
}
NTSTATUS
UsbCom_RemoveDevice(
IN PDEVICE_OBJECT DeviceObject
)
/*++
Routine Description:
Called from UsbCom_ProcessPnPIrp() to
clean up our device instance's allocated buffers; free symbolic links
Arguments:
DeviceObject - pointer to the FDO
Return Value:
NT status code from free symbolic link operation
--*/
{
PDEVICE_EXTENSION deviceExtension;
NTSTATUS ntStatus = STATUS_SUCCESS;
UNICODE_STRING deviceLinkUnicodeString;
DbgPrint("enter UsbCom_RemoveDevice\n");
deviceExtension = DeviceObject->DeviceExtension;
RtlInitUnicodeString (&deviceLinkUnicodeString,
deviceExtension->DeviceLinkNameBuffer);
// remove the GUID-based symbolic link
ntStatus = IoSetDeviceInterfaceState(&deviceLinkUnicodeString, FALSE);
ASSERT( NT_SUCCESS( ntStatus ) );
if(deviceExtension->ReadingUrb)
ExFreePool(deviceExtension->ReadingUrb);
if(deviceExtension->ReadingIrp)
IoFreeIrp(deviceExtension->ReadingIrp);
if(deviceExtension->WritingUrb)
ExFreePool(deviceExtension->WritingUrb);
if(deviceExtension->WritingIrp)
IoFreeIrp(deviceExtension->WritingIrp);
if(deviceExtension->PollingUrb)
ExFreePool(deviceExtension->PollingUrb);
if(deviceExtension->PollingIrp)
IoFreeIrp(deviceExtension->PollingIrp);
if (deviceExtension->WorkItem != NULL) {
IoFreeWorkItem(deviceExtension->WorkItem);
}
//
// Free device descriptor structure
//
if (deviceExtension->UsbDeviceDescriptor) {
ExFreePool(deviceExtension->UsbDeviceDescriptor);
deviceExtension->UsbDeviceDescriptor = NULL;
}
//
// Free pipe info structs
//
if( deviceExtension->PipeInfo ) {
ExFreePool( deviceExtension->PipeInfo );
deviceExtension->PipeInfo = NULL;
}
//
// Free up the UsbInterface structure
//
if (deviceExtension->UsbInterface) {
ExFreePool(deviceExtension->UsbInterface);
deviceExtension->UsbInterface = NULL;
}
// free up the USB config discriptor
if (deviceExtension->UsbConfigurationDescriptor) {
ExFreePool(deviceExtension->UsbConfigurationDescriptor);
deviceExtension->UsbConfigurationDescriptor = NULL;
}
// free the pending irp list
if ( deviceExtension->PendingIoIrps ) {
ExFreePool(deviceExtension->PendingIoIrps );
deviceExtension->PendingIoIrps = NULL;
}
DbgPrint("exit UsbCom_RemoveDevice() \n");
DbgPrint("exit UsbCom_RemoveDevice() status = 0x%x\n", ntStatus );
return ntStatus;
}
NTSTATUS
UsbCom_StopDevice(
IN PDEVICE_OBJECT DeviceObject
)
/*++
Routine Description:
Stops a given instance of a 82930 device on the USB.
We basically just tell USB this device is now 'unconfigured'
Arguments:
DeviceObject - pointer to the device object for this instance of a 82930
Return Value:
NT status code
--*/
{
PDEVICE_EXTENSION deviceExtension;
NTSTATUS ntStatus = STATUS_SUCCESS;
PURB urb;
ULONG siz;
DbgPrint("enter UsbCom_StopDevice\n");
deviceExtension = DeviceObject->DeviceExtension;
//
// Send the select configuration urb with a NULL pointer for the configuration
// handle. This closes the configuration and puts the device in the 'unconfigured'
// state.
//
siz = sizeof(struct _URB_SELECT_CONFIGURATION);
urb = ExAllocatePool(NonPagedPool,
siz);
if (urb) {
UsbBuildSelectConfigurationRequest(urb,
(USHORT) siz,
NULL);
ntStatus = UsbCom_CallUSBD(DeviceObject, urb);
DbgPrint("Close the configuration, status -- %d", ntStatus);
ExFreePool(urb);
} else {
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
if (NT_SUCCESS(ntStatus)) {
deviceExtension->DeviceStarted = FALSE;
}
deviceExtension->StopDeviceRequested = FALSE;
DbgPrint("exit UsbCom_StopDevice() (%x)\n", ntStatus);
return ntStatus;
}
NTSTATUS
UsbCom_IrpCompletionRoutine(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID Context
)
/*++
Routine Description:
Used as a general purpose completion routine so it can signal an event,
passed as the Context, when the next lower driver is done with the input Irp.
This routine is used by both PnP and Power Management logic.
Even though this routine does nothing but set an event, it must be defined and
prototyped as a completetion routine for use as such
Arguments:
DeviceObject - Pointer to the device object for the class device.
Irp - Irp completed.
Context - Driver defined context, in this case a pointer to an event.
Return Value:
The function value is the final status from the operation.
--*/
{
PKEVENT event = Context;
// Set the input event
KeSetEvent(event,
1, // Priority increment for waiting thread.
FALSE); // Flag this call is not immediately followed by wait.
// This routine must return STATUS_MORE_PROCESSING_REQUIRED because we have not yet called
// IoFreeIrp() on this IRP.
return STATUS_MORE_PROCESSING_REQUIRED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -