2012-01-02 21:38:57 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS NDIS User I/O driver
|
|
|
|
* FILE: readwrite.c
|
|
|
|
* PURPOSE: Handles IRP_MJ_READ and IRP_MJ_WRITE
|
|
|
|
* PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ndisuio.h"
|
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-01-06 05:59:56 +00:00
|
|
|
static
|
2012-01-03 19:37:03 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ReadIrpCancel(PDEVICE_OBJECT DeviceObject, PIRP Irp)
|
|
|
|
{
|
2012-01-06 05:59:56 +00:00
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
2012-01-03 19:37:03 +00:00
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = IrpSp->FileObject->FsContext;
|
|
|
|
PNDISUIO_PACKET_ENTRY PacketEntry;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 19:37:03 +00:00
|
|
|
/* Release the cancel spin lock */
|
|
|
|
IoReleaseCancelSpinLock(Irp->CancelIrql);
|
|
|
|
|
|
|
|
/* Indicate a 0-byte packet on the queue to cancel the read */
|
2012-01-06 05:59:56 +00:00
|
|
|
PacketEntry = ExAllocatePool(NonPagedPool, sizeof(NDISUIO_PACKET_ENTRY));
|
2012-01-03 19:37:03 +00:00
|
|
|
if (PacketEntry)
|
|
|
|
{
|
|
|
|
PacketEntry->PacketLength = 0;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 19:37:03 +00:00
|
|
|
ExInterlockedInsertHeadList(&AdapterContext->PacketList,
|
|
|
|
&PacketEntry->ListEntry,
|
|
|
|
&AdapterContext->Spinlock);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 19:37:03 +00:00
|
|
|
KeSetEvent(&AdapterContext->PacketReadEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
NTSTATUS
|
2012-01-02 21:38:57 +00:00
|
|
|
NTAPI
|
|
|
|
NduDispatchRead(PDEVICE_OBJECT DeviceObject,
|
|
|
|
PIRP Irp)
|
|
|
|
{
|
2012-01-03 17:54:01 +00:00
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = IrpSp->FileObject->FsContext;
|
2012-01-03 18:12:29 +00:00
|
|
|
PNDISUIO_OPEN_ENTRY OpenEntry = IrpSp->FileObject->FsContext2;
|
2012-01-03 19:37:03 +00:00
|
|
|
KIRQL OldIrql, OldCancelIrql;
|
2012-01-03 17:54:01 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
PLIST_ENTRY ListEntry;
|
|
|
|
PNDISUIO_PACKET_ENTRY PacketEntry = NULL;
|
|
|
|
ULONG BytesCopied = 0;
|
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
ASSERT(DeviceObject == GlobalDeviceObject);
|
2012-01-03 16:08:24 +00:00
|
|
|
|
2012-01-03 18:12:29 +00:00
|
|
|
if (OpenEntry->WriteOnly)
|
|
|
|
{
|
|
|
|
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 18:12:29 +00:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2012-01-03 19:37:03 +00:00
|
|
|
/* Make the read cancellable */
|
|
|
|
IoAcquireCancelSpinLock(&OldCancelIrql);
|
|
|
|
IoSetCancelRoutine(Irp, ReadIrpCancel);
|
|
|
|
if (Irp->Cancel)
|
|
|
|
{
|
|
|
|
IoReleaseCancelSpinLock(OldCancelIrql);
|
|
|
|
|
|
|
|
/* Indicate a 0 byte read */
|
|
|
|
Irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 19:37:03 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
IoReleaseCancelSpinLock(OldCancelIrql);
|
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
KeAcquireSpinLock(&AdapterContext->Spinlock, &OldIrql);
|
|
|
|
|
|
|
|
/* Check if we have a packet */
|
|
|
|
if (IsListEmpty(&AdapterContext->PacketList))
|
|
|
|
{
|
|
|
|
KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
|
|
|
|
|
|
|
|
/* Wait for a packet (in the context of the calling user thread) */
|
|
|
|
Status = KeWaitForSingleObject(&AdapterContext->PacketReadEvent,
|
|
|
|
UserRequest,
|
|
|
|
UserMode,
|
|
|
|
TRUE,
|
|
|
|
NULL);
|
|
|
|
if (Status != STATUS_SUCCESS)
|
2012-01-03 19:37:03 +00:00
|
|
|
{
|
|
|
|
/* Remove the cancel routine */
|
|
|
|
IoAcquireCancelSpinLock(&OldCancelIrql);
|
|
|
|
IoSetCancelRoutine(Irp, NULL);
|
|
|
|
IoReleaseCancelSpinLock(OldCancelIrql);
|
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
break;
|
2012-01-03 19:37:03 +00:00
|
|
|
}
|
2012-01-03 17:54:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-03 19:37:03 +00:00
|
|
|
/* Remove the cancel routine */
|
|
|
|
IoAcquireCancelSpinLock(&OldCancelIrql);
|
|
|
|
IoSetCancelRoutine(Irp, NULL);
|
|
|
|
IoReleaseCancelSpinLock(OldCancelIrql);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Remove the first packet in the list */
|
|
|
|
ListEntry = RemoveHeadList(&AdapterContext->PacketList);
|
|
|
|
PacketEntry = CONTAINING_RECORD(ListEntry, NDISUIO_PACKET_ENTRY, ListEntry);
|
|
|
|
|
|
|
|
/* Release the adapter lock */
|
|
|
|
KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* And we're done with this loop */
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Check if we got a packet */
|
|
|
|
if (PacketEntry != NULL)
|
|
|
|
{
|
|
|
|
/* Find the right amount of bytes to copy */
|
|
|
|
BytesCopied = PacketEntry->PacketLength;
|
|
|
|
if (BytesCopied > IrpSp->Parameters.Read.Length)
|
|
|
|
BytesCopied = IrpSp->Parameters.Read.Length;
|
2012-01-03 16:08:24 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Copy the packet */
|
|
|
|
RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer,
|
2012-01-06 05:59:56 +00:00
|
|
|
&PacketEntry->PacketData[0],
|
2012-01-03 17:54:01 +00:00
|
|
|
BytesCopied);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Free the packet entry */
|
|
|
|
ExFreePool(PacketEntry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Something failed */
|
|
|
|
BytesCopied = 0;
|
|
|
|
}
|
2012-01-03 16:08:24 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Complete the IRP */
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
Irp->IoStatus.Information = BytesCopied;
|
|
|
|
IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
return Status;
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
NTSTATUS
|
2012-01-02 21:38:57 +00:00
|
|
|
NTAPI
|
|
|
|
NduDispatchWrite(PDEVICE_OBJECT DeviceObject,
|
|
|
|
PIRP Irp)
|
|
|
|
{
|
2012-01-03 17:54:01 +00:00
|
|
|
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = IrpSp->FileObject->FsContext;
|
|
|
|
PNDIS_PACKET Packet;
|
|
|
|
NDIS_STATUS Status;
|
|
|
|
ULONG BytesCopied = 0;
|
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
ASSERT(DeviceObject == GlobalDeviceObject);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Create a packet and buffer descriptor for this user buffer */
|
2012-01-06 19:43:15 +00:00
|
|
|
Packet = CreatePacketFromPoolBuffer(AdapterContext,
|
|
|
|
Irp->AssociatedIrp.SystemBuffer,
|
2012-01-03 17:54:01 +00:00
|
|
|
IrpSp->Parameters.Write.Length);
|
|
|
|
if (Packet)
|
|
|
|
{
|
|
|
|
/* Send it via NDIS */
|
|
|
|
NdisSend(&Status,
|
|
|
|
AdapterContext->BindingHandle,
|
|
|
|
Packet);
|
|
|
|
|
|
|
|
/* Wait for the send */
|
|
|
|
if (Status == NDIS_STATUS_PENDING)
|
|
|
|
{
|
|
|
|
KeWaitForSingleObject(&AdapterContext->AsyncEvent,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
Status = AdapterContext->AsyncStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if it succeeded */
|
|
|
|
if (Status == NDIS_STATUS_SUCCESS)
|
|
|
|
BytesCopied = IrpSp->Parameters.Write.Length;
|
2012-01-02 21:38:57 +00:00
|
|
|
|
2012-01-06 19:43:15 +00:00
|
|
|
CleanupAndFreePacket(Packet, FALSE);
|
2012-01-03 17:54:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No memory */
|
|
|
|
Status = STATUS_NO_MEMORY;
|
|
|
|
}
|
2012-01-03 16:08:24 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Complete the IRP */
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
Irp->IoStatus.Information = BytesCopied;
|
|
|
|
IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
|
2012-01-03 16:08:24 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
return Status;
|
2014-02-06 11:18:34 +00:00
|
|
|
}
|