- UID
- 104868
- 性别
- 男
|
UsbStore_ReadWrite这一函数不做任何实际有用的读写操作。
NTSTATUS
UsbStore_ReadWrite(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
{
#if DBG
DbgPrint("Enter ReadWrite Dispatch!\n");
#endif
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp,IO_NO_INCREMENT);
return STATUS_INVALID_PARAMETER;
}
UsbStore_Scsi将上层DRIVER的IRP_MJ_SCSI的请求排队,
NTSTATUS
UsbStore_Scsi(IN PDEVICE_OBJECT UsbStorePdo,
IN PIRP Irp)
{
PUSBSTORE_DEVICE_EXTENSION pUsbStoreDeviceExtension = UsbStorePdo->DeviceExtension;
PIO_STACK_LOCATION irpStackLocation = IoGetCurrentIrpStackLocation(Irp);
PSCSI_REQUEST_BLOCK Srb = irpStackLocation->Parameters.Scsi.Srb;
NTSTATUS status;
KIRQL Irql;
if(pUsbStoreDeviceExtension->DeviceType == USBSTORE_PDO)
{
if(Srb->Function == SRB_FUNCTION_EXECUTE_SCSI)
{
if(Srb->DataTransferLength <=0x10000)
{
Srb->SrbStatus = SRB_STATUS_PENDING;
IoMarkIrpPending(Irp);
IoStartPacket(UsbStorePdo, Irp, &Srb->QueueSortKey, (PDRIVER_CANCEL)UsbStore_CancelRoutine);
status = STATUS_PENDING;
}
else
{
status = STATUS_INVALID_PARAMETER;
}
}
else if(Srb->Function == SRB_FUNCTION_CLAIM_DEVICE)
{
KeAcquireSpinLock(&pUsbStoreDeviceExtension->SpinLock, &Irql);
if(pUsbStoreDeviceExtension->DeviceFlags&0x01)
{
status = STATUS_END_OF_FILE;
Srb->SrbStatus = SRB_STATUS_BUSY;
}
else
{
pUsbStoreDeviceExtension->DeviceFlags|=0x01;
Srb->DataBuffer = UsbStorePdo;
Srb->SrbStatus = SRB_STATUS_SUCCESS;
}
KeReleaseSpinLock(&pUsbStoreDeviceExtension->SpinLock, Irql);
}
else if(Srb->Function == SRB_FUNCTION_RELEASE_DEVICE)
{
KeAcquireSpinLock(&pUsbStoreDeviceExtension->SpinLock,&Irql);
pUsbStoreDeviceExtension->DeviceFlags &= 0xFFFFFFFE;
KeReleaseSpinLock(&pUsbStoreDeviceExtension->SpinLock, Irql);
Srb->SrbStatus = SRB_STATUS_SUCCESS;
status = STATUS_SUCCESS;
}
else if(Srb->Function == SRB_FUNCTION_FLUSH)
{
Srb->SrbStatus = SRB_STATUS_SUCCESS;
status = STATUS_SUCCESS;
}
else
{
status = STATUS_NOT_SUPPORTED;
}
}
else
{
status = STATUS_NOT_SUPPORTED;
}
if(status != STATUS_PENDING)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
}
return status;
}
而UsbStore_StartIo函数将排队的IRP通过CBI规范或者BULK ONLY规范发送给设备,
VOID
UsbStore_StartIo(
IN PDEVICE_OBJECT UsbStorePdo,
IN PIRP Irp
)
{
ULONG TempFlags;
PSCSI_REQUEST_BLOCK Srb;
UCHAR DeviceErrorFlags ;
ULONG NeedSenseRequestFlags;
PUSBSTORE_DEVICE_EXTENSION pUsbStoreDeviceExtension = UsbStorePdo->DeviceExtension;
PIO_STACK_LOCATION irpStackLocation;
KIRQL Irql;
irpStackLocation = IoGetCurrentIrpStackLocation(Irp);
if(irpStackLocation->MajorFunction == IRP_MJ_POWER)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
PoStartNextPowerIrp(Irp);
IoCompleteRequest(Irp,IO_NO_INCREMENT);
return;
}
else
{
IoAcquireCancelSpinLock(&Irql);
InterlockedExchange((PULONG)&Irp->CancelRoutine,0);
if(Irp->Cancel != 0)
{
KeAcquireSpinLockAtDpcLevel(&pUsbStoreDeviceExtension->SpinLock);
pUsbStoreDeviceExtension->DeviceFlags = pUsbStoreDeviceExtension->DeviceFlags>>3;
pUsbStoreDeviceExtension->DeviceFlags = ~pUsbStoreDeviceExtension->DeviceFlags;
pUsbStoreDeviceExtension->DeviceFlags = pUsbStoreDeviceExtension->DeviceFlags&1;
TempFlags = pUsbStoreDeviceExtension->DeviceFlags;
KeReleaseSpinLockFromDpcLevel(&pUsbStoreDeviceExtension->SpinLock);
IoReleaseCancelSpinLock(Irql);
Irp->IoStatus.Status = STATUS_CANCELLED;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp,IO_NO_INCREMENT);
if(TempFlags==0)
return;
else
IoStartNextPacket(UsbStorePdo, TRUE);
return;
}
else
{
IoReleaseCancelSpinLock(Irql);
DeviceErrorFlags = 0;
NeedSenseRequestFlags = 0;
Srb = irpStackLocation->Parameters.Scsi.Srb;
pUsbStoreDeviceExtension->Srb = Srb;
KeAcquireSpinLockAtDpcLevel(&pUsbStoreDeviceExtension->SpinLock);
if(pUsbStoreDeviceExtension->DeviceFlags&DEVICE_ERROR)
{
DeviceErrorFlags = 1;
}
else
{
pUsbStoreDeviceExtension->SrbTimeOutValue = Srb->TimeOutValue;
if(pUsbStoreDeviceExtension->DeviceFlags&NEED_SENSEREQUEST)
{
NeedSenseRequestFlags = 1;
pUsbStoreDeviceExtension->DeviceFlags&=~NEED_SENSEREQUEST;
}
}
KeReleaseSpinLockFromDpcLevel(&pUsbStoreDeviceExtension->SpinLock);
if(DeviceErrorFlags ==0)
{
UsbStore_FormatSrb(UsbStorePdo,Irp,Srb,1);
if(pUsbStoreDeviceExtension->DriverFlags==1)
{
UsbStore_BulkOnly(UsbStorePdo,Irp);
return;
}
else
{
if(NeedSenseRequestFlags == 0||Srb->Cdb[0] == SCSIOP_REQUEST_SENSE)
{
UsbStore_CBI(UsbStorePdo, Irp);
return;
}
else
{
UsbStore_CBIGetRequestSense(UsbStorePdo,Irp,1);
return;
}
}
}
else
{
Irp->IoStatus.Status = STATUS_DEVICE_DOES_NOT_EXIST;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
IoStartNextPacket(UsbStorePdo, TRUE);
return;
}
}
}
}
UsbStore_DeviceIoControl函数负责处理IRP_MJ_DEVICE_CONTROL,其中IOCONTROLCODE主要有IOCTL_SCSI_PASS_THROUGH,IOCTL_SCSI_PASS_THROUGH_DIRECT,IOCTL_STORAGE_QUERY_PROPERTY。限于篇幅,就不将此函数的代码列下。
UsbStore_Pnp函数负责处理IRP_MJ_PNP,USB STORAGE DRIVER的FDO IRP_MJ_PNP的IRP_MN_START_DEVICE负责建立USB连接,建立USB STORAGE DRIVER的PDO。USB STORAGE DRIVER FDO 的IRP_MN_REMOVE_DEVICE负责清除设备资源及USB连接。
USB STORAGE DRIVER PDO的IRP_MN_QUERY_DEVICE_TEXT 和IRP_MN_QUERY_ID是BUS DRIVER的PNP所必须处理的。
(未完待续) |
|