第19章,虚拟串口驱动,求讲解。有意的加我qq:545039712.我出钱。
需要讲解的代码如下:
NTSTATUS HelloWDMRead(IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
KdPrint(("HelloWDMRead\n"));
NTSTATUS ntStatus = STATUS_SUCCESS;// Assume success
PDEVICE_EXTENSION pExtension = (PDEVICE_EXTENSION)fdo->DeviceExtension;
PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation( Irp );
ULONG BufLen = irpSp->Parameters.Read.Length;
PCHAR pBuf = (PCHAR)Irp->AssociatedIrp.SystemBuffer;
KIRQL OldIrql;
PDRIVER_CANCEL pOldCancelRoutine;
Irp->IoStatus.Information = 0;
DbgPrint("DeviceObject:%08X Read\n",fdo);
if (BufLen == 0)
{
ntStatus = STATUS_SUCCESS;
}
else
{
KeAcquireSpinLock(&pExtension->WriteSpinLock, &OldIrql);
RtlCopyMemory(pBuf,pExtension->Buffer,BufLen);
Irp->IoStatus.Information = BufLen;
if (BufLen==0 && pExtension->pReadIrp==NULL) // nothing, store
{
pExtension->pReadIrp = Irp;
Irp->IoStatus.Status = ntStatus = STATUS_PENDING;
IoSetCancelRoutine(Irp, DriverCancelCurrentReadIrp);
if (Irp->Cancel)
{
pOldCancelRoutine = IoSetCancelRoutine(Irp, NULL);
if (pOldCancelRoutine != NULL)
{
// Nein, also IRP hier abbrechen
Irp->IoStatus.Status = ntStatus = STATUS_CANCELLED;
pExtension->pReadIrp = NULL;
}
else
{
// Ja, Cancel-Routine wird Request beenden
IoMarkIrpPending(Irp);
}
}
else
{
IoMarkIrpPending(Irp);
}
}
KeReleaseSpinLock(&pExtension->WriteSpinLock, OldIrql);
}
Irp->IoStatus.Status = ntStatus;
if (ntStatus != STATUS_PENDING)
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return ntStatus;
}
NTSTATUS HelloWDMWrite(IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
KdPrint(("HelloWDMWrite\n"));
NTSTATUS ntStatus = STATUS_SUCCESS;// Assume success
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)fdo->DeviceExtension;
PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation( Irp );
ULONG DataLen = irpSp->Parameters.Write.Length;
PUCHAR pData = (PUCHAR)Irp->AssociatedIrp.SystemBuffer;
KIRQL OldIrql;
PIRP pOldReadIrp = NULL;
PDRIVER_CANCEL pOldCancelRoutine;
Irp->IoStatus.Information = 0;
ntStatus = STATUS_SUCCESS;
if (DataLen == 0)
{
ntStatus = STATUS_SUCCESS;
}else if (DataLen>COMBUFLEN)
{
ntStatus = STATUS_INVALID_PARAMETER;
}
else
{
KdPrint(("Write\n"));
KeAcquireSpinLock(&pdx->WriteSpinLock, &OldIrql);