⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.c

📁 如题:POLAR LPC23XX-EK_DEMO software_Keil
💻 C
📖 第 1 页 / 共 4 页
字号:
		DbgPrint("UsbCom_ConfigureDevice() DO=%08x Urb=%08x CallUSBD() 1 FAIL=%08x\n", 
			DeviceObject, urb, ntStatus );
		goto UsbCom_ConfigureDevice_Exit3;
	}
	
	Size = deviceExtension->UsbConfigurationDescriptor->wTotalLength;

    ExFreePool(deviceExtension->UsbConfigurationDescriptor);

	if ( ( deviceExtension->UsbConfigurationDescriptor = ExAllocatePool(NonPagedPool, Size ) ) == NULL ) {		
		DbgPrint("UsbCom_ConfigureDevice() DO=%08x ExAllocatePool(%d) : FAIL\n", DeviceObject, Size );
		ntStatus = STATUS_INSUFFICIENT_RESOURCES;	
		goto UsbCom_ConfigureDevice_Exit2;
	}
	

	UsbBuildGetDescriptorRequest(
		urb,
		(USHORT) sizeof (struct _URB_CONTROL_DESCRIPTOR_REQUEST),
		USB_CONFIGURATION_DESCRIPTOR_TYPE,
		0,
		0,
		deviceExtension->UsbConfigurationDescriptor,
		NULL,
		Size,
		NULL);

	ntStatus = UsbCom_CallUSBD(DeviceObject, urb);

	if (!(NT_SUCCESS(ntStatus))) {	
		DbgPrint("UsbCom_ConfigureDevice() DO=%08x Urb=%08x CallUSBD() 2 FAIL=%08x\n", 
			DeviceObject, urb, ntStatus );
		goto UsbCom_ConfigureDevice_Exit3;
	}
	
    ExFreePool(urb);

    ntStatus = UsbCom_SelectInterface(DeviceObject,
        deviceExtension->UsbConfigurationDescriptor);

	return ntStatus;
	
UsbCom_ConfigureDevice_Exit3 :
	ExFreePool ( deviceExtension->UsbConfigurationDescriptor );	

UsbCom_ConfigureDevice_Exit2 :
    ExFreePool(urb);

UsbCom_ConfigureDevice_Exit1 :
    return ntStatus;
} 


NTSTATUS
UsbCom_SelectInterface(
    IN PDEVICE_OBJECT DeviceObject,
    IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
    )
/*++

Routine Description:

    Initializes interface;
	This minidriver only supports one interface (with multiple endpoints).

Arguments:

    DeviceObject - pointer to the device object for this instance of the 82930
                    device.

    ConfigurationDescriptor - pointer to the USB configuration
                    descriptor containing the interface and endpoint
                    descriptors.

Return Value:

    NT status code

--*/
{
    PDEVICE_EXTENSION deviceExtension;
    NTSTATUS ntStatus;
    PURB urb = NULL;
    ULONG i;
    PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor = NULL;
	PUSBD_INTERFACE_INFORMATION InterfaceInformation = NULL;
    USHORT siz;
    ULONG j;
    ULONG x;
	PVOID StartPosition;
	ULONG lTotalInterfaces;
   	USBD_INTERFACE_LIST_ENTRY InterfaceList[2];
 
    deviceExtension = DeviceObject->DeviceExtension;
    
   	lTotalInterfaces = ConfigurationDescriptor->bNumInterfaces;

	for ( i = 0 ; i < 2 ; i ++ ) {
		InterfaceList[i].InterfaceDescriptor = NULL;
		InterfaceList[i].Interface           = NULL;
	}

	DbgPrint("UsbCom_SelectInterface() DO=%08x Interfaces=%d\n", DeviceObject, lTotalInterfaces );

	StartPosition = (PVOID)((ULONG)ConfigurationDescriptor + (ULONG)(ConfigurationDescriptor->bLength));

    InterfaceList[0].InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx (
			ConfigurationDescriptor,
			StartPosition, //search from start of config  descriptro
			0,		// try interface #0
			-1,   // not interested in alternate setting here either
			-1,   // interface class not a criteria
			-1,   // interface subclass not a criteria
			-1 );   // interface protocol not a criteria

	deviceExtension->ChannelNumber = 0;

	if ( InterfaceList[0].InterfaceDescriptor == NULL ) {

		DbgPrint("UsbCom_SelectInterface() DO=%08x USBD_ParseConfigurationDescriptorEx() Interfaces[0] : FAIL \n", DeviceObject );

		InterfaceList[0].InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx (
				ConfigurationDescriptor,
				StartPosition, //search from start of config  descriptro
				1,		// try interface #1
				-1,   // not interested in alternate setting here either
				-1,   // interface class not a criteria
				-1,   // interface subclass not a criteria
				-1 );   // interface protocol not a criteria
				
		deviceExtension->ChannelNumber = 1;
	}
	
	if ( InterfaceList[0].InterfaceDescriptor == NULL ) {
		DbgPrint("UsbCom_SelectInterface() DO=%08x USBD_ParseConfigurationDescriptorEx() Interfaces[1] : FAIL \n", DeviceObject );
		goto UsbCom_SelectInterface_Exit1;
	}
  
    if ( ( urb = USBD_CreateConfigurationRequestEx(ConfigurationDescriptor, InterfaceList) ) == NULL ) {
        DbgPrint("UsbCom_SelectInterface() DO=%08x USBD_CreateConfigurationRequest() : FAIL\n", DeviceObject );
        ntStatus = STATUS_INSUFFICIENT_RESOURCES;    
		goto UsbCom_SelectInterface_Exit1;
    }

	InterfaceDescriptor = InterfaceList[0].InterfaceDescriptor;
	InterfaceInformation = InterfaceList[0].Interface;

	for (x = 0 ; x < InterfaceInformation->NumberOfPipes ; x++) {
		InterfaceInformation->Pipes[x].MaximumTransferSize = deviceExtension->MaximumTransferSize;
		InterfaceInformation->Pipes[x].PipeFlags = 0;
	}

	InterfaceInformation->Length = GET_USBD_INTERFACE_SIZE( InterfaceDescriptor->bNumEndpoints );
	InterfaceInformation->InterfaceNumber = InterfaceDescriptor->bInterfaceNumber;
	InterfaceInformation->AlternateSetting = InterfaceDescriptor->bAlternateSetting;
 
    ntStatus = UsbCom_CallUSBD(DeviceObject, urb);

	if ( ! NT_SUCCESS(ntStatus)) {
		DbgPrint("UsbCom_SelectInterface() DO=%08x UsbCom_CallUSBD() : FAIL=%08x\n", DeviceObject, ntStatus );
		goto UsbCom_SelectInterface_Exit2;
	}
	
    deviceExtension->UsbConfigurationHandle =  urb->UrbSelectConfiguration.ConfigurationHandle;
	
	ExFreePool  ( urb );

    //
    // Save the configuration handle for this device
    //

    //deviceExtension->UsbConfigurationHandle =
        //urb->UrbSelectConfiguration.ConfigurationHandle;

    if ( ( deviceExtension->UsbInterface = ExAllocatePool(NonPagedPool, InterfaceInformation->Length) ) == NULL ) {
		DbgPrint("UsbCom_SelectInterface() DO=%08x ExAllocatePool(%d) : FAIL\n", DeviceObject, InterfaceInformation->Length );
		goto UsbCom_SelectInterface_Exit1;
    }


    //
    // save a copy of the interface information returned
    //
    RtlCopyMemory(deviceExtension->UsbInterface, InterfaceInformation, InterfaceInformation->Length);

    //
    // Dump the interface to the debugger
    //
    DbgPrint("---------\n");
    DbgPrint("NumberOfPipes 0x%x\n", deviceExtension->UsbInterface->NumberOfPipes);
    DbgPrint("Length 0x%x\n", deviceExtension->UsbInterface->Length);
    DbgPrint("Alt Setting 0x%x\n", deviceExtension->UsbInterface->AlternateSetting);
    DbgPrint("Interface Number 0x%x\n", deviceExtension->UsbInterface->InterfaceNumber);
    DbgPrint("Class, subclass, protocol 0x%x 0x%x 0x%x\n",
        deviceExtension->UsbInterface->Class,
        deviceExtension->UsbInterface->SubClass,
        deviceExtension->UsbInterface->Protocol);

    // Dump the pipe info

    for (j=0; j<InterfaceInformation->NumberOfPipes; j++) {
        PUSBD_PIPE_INFORMATION pipeInformation;

        pipeInformation = &deviceExtension->UsbInterface->Pipes[j];

        DbgPrint("---------\n");
        DbgPrint("PipeType 0x%x\n", pipeInformation->PipeType);
        DbgPrint("EndpointAddress 0x%x\n", pipeInformation->EndpointAddress);
        DbgPrint("MaxPacketSize 0x%x\n", pipeInformation->MaximumPacketSize);
        DbgPrint("Interval 0x%x\n", pipeInformation->Interval);
        DbgPrint("Handle 0x%x\n", pipeInformation->PipeHandle);
        DbgPrint("MaximumTransferSize 0x%x\n", pipeInformation->MaximumTransferSize);
        switch (pipeInformation->PipeType) {       

                case UsbdPipeTypeBulk:
                    if (USBD_PIPE_DIRECTION_IN(pipeInformation)) {
                        DbgPrint("DataInPipe 0x%x\n", j);
                        deviceExtension->DataInPipe = j;
                    } else {
                        DbgPrint("DataOutPipe 0x%x\n", j);
                        deviceExtension->DataOutPipe = j;
                    }
                    break;
	

                case UsbdPipeTypeInterrupt:
                    DbgPrint("InterruptPipe 0x%x\n", j);
                    deviceExtension->InterruptPipe = j;
                    break;


                default:
                    DbgPrint("Unknown pipe 0x%x\n", j);
                    break;
		}
	}

    DbgPrint("---------\n");
    
    return STATUS_SUCCESS;

 /*
#ifndef USE_SMARTNIC

    // .........Kawshol........
    // Retrieve the selected Configuration and Interface setting from the
    // device.  (The only purpose of doing this here is to exercise the
    // URB_FUNCTION_GET_CONFIGURATION and URB_FUNCTION_GET_INTERFACE
    // requests).
    //
   if (NT_SUCCESS(ntStatus)) {

        urb = ExAllocatePool(
                  NonPagedPool,
                  sizeof(struct _URB_CONTROL_GET_CONFIGURATION_REQUEST) + 1);

        if (urb)
        {
            PUCHAR configuration;

            configuration = (PUCHAR)urb + sizeof(struct 
            _URB_CONTROL_GET_CONFIGURATION_REQUEST);
            *configuration = 0xFF;

            urb->UrbHeader.Function = URB_FUNCTION_GET_CONFIGURATION;
            urb->UrbHeader.Length = sizeof(struct 
            _URB_CONTROL_GET_CONFIGURATION_REQUEST);
            urb->UrbControlGetConfigurationRequest.TransferBufferLength = 1;
            urb->UrbControlGetConfigurationRequest.TransferBuffer = 
            configuration;
            urb->UrbControlGetConfigurationRequest.TransferBufferMDL = NULL;
            urb->UrbControlGetConfigurationRequest.UrbLink = NULL;

            ntStatus = UsbCom_CallUSBD(DeviceObject, urb);

            DbgPrint ("D12TEST.SYS: Configuration %d (%x)\n",
                             *configuration, ntStatus);

            ExFreePool(urb);
            urb = NULL;
        }

        urb = ExAllocatePool(
                  NonPagedPool,
                  sizeof(struct _URB_CONTROL_GET_INTERFACE_REQUEST) + 1);

        if (urb)
        {
            PUCHAR interface;

            interface = (PUCHAR)urb + sizeof(struct 
            _URB_CONTROL_GET_INTERFACE_REQUEST);
            *interface = 0xFF;

            urb->UrbHeader.Function = URB_FUNCTION_GET_INTERFACE;
            urb->UrbHeader.Length = sizeof(struct 
            _URB_CONTROL_GET_INTERFACE_REQUEST);
            urb->UrbControlGetInterfaceRequest.TransferBufferLength = 1;
            urb->UrbControlGetInterfaceRequest.TransferBuffer = interface;
            urb->UrbControlGetInterfaceRequest.TransferBufferMDL = NULL;
            urb->UrbControlGetInterfaceRequest.UrbLink = NULL;
            urb->UrbControlGetInterfaceRequest.Interface =
                deviceExtension->UsbInterface->InterfaceNumber;

            ntStatus = UsbCom_CallUSBD(DeviceObject, urb);

            DbgPrint ("D12TEST.SYS: Interface %d (%x)\n",
                             *interface, ntStatus);

            ExFreePool(urb);
            urb = NULL;
        }
    } 
   
   // kawshol
#endif
*/

UsbCom_SelectInterface_Exit2:
	ExFreePool ( urb );
	
UsbCom_SelectInterface_Exit1:
    return ntStatus; 
}



NTSTATUS
UsbCom_ResetPipe(
    IN PDEVICE_OBJECT DeviceObject,
    IN PUSBD_PIPE_INFORMATION PipeInfo
    )
/*++

Routine Description:

    Reset a given USB pipe.

    NOTES:

    This will reset the host to Data0 and should also reset the device to Data0 

Arguments:

    Ptrs to our FDO and a USBD_PIPE_INFORMATION struct

Return Value:

    NT status code

--*/
{
    NTSTATUS ntStatus;
    PURB urb;
    PDEVICE_EXTENSION deviceExtension;

    deviceExtension = DeviceObject->DeviceExtension;

    DbgPrint("UsbCom_ResetPipe() Reset Pipe %x\n", PipeInfo);

    urb = ExAllocatePool(NonPagedPool,
                         sizeof(struct _URB_PIPE_REQUEST));

    if (urb) {

        urb->UrbHeader.Length = (USHORT) sizeof (struct _URB_PIPE_REQUEST);
        urb->UrbHeader.Function = URB_FUNCTION_RESET_PIPE;
        urb->UrbPipeRequest.PipeHandle =
            PipeInfo->PipeHandle;

        ntStatus = UsbCom_CallUSBD(DeviceObject, urb);

        ExFreePool(urb);

    } else {
        ntStatus = STATUS_INSUFFICIENT_RESOURCES;
    }

    if (!(NT_SUCCESS(ntStatus))) {
        DbgPrint("UsbCom_ResetPipe() FAILED, ntStatus =0x%x\n", ntStatus );
    }
    else {
        DbgPrint("UsbCom_ResetPipe() SUCCESS, ntStatus =0x%x\n", ntStatus );
    }

    return ntStatus;
}


NTSTATUS
UsbCom_Create(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
/*++

Routine Description:

    This is the dispatch table routine for IRP_MJ_CREATE.
    It's the entry point for CreateFile() calls

Arguments:

    DeviceObject - pointer to our FDO ( Functional Device Object )


Return Value:

    NT status code

--*/
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PFILE_OBJECT fileObject;
    PIO_STACK_LOCATION irpStack;
    PDEVICE_EXTENSION deviceExtension;
    ULONG i, ix;
	NTSTATUS actStat;
    PUSBD_INTERFACE_INFORMATION interface;
	PUSBD_PIPE_INFORMATION PipeInfo;
    PUsbCom_PIPEINFO ourPipeInfo = NULL;


    deviceExtension = DeviceObject->DeviceExtension;
    interface = deviceExtension->UsbInterface;

    DbgPrint("entering UsbCom_Create DO=%8x Irp=%08x\n", DeviceObject, Irp );

    UsbCom_IncrementIoCount(DeviceObject);

    // Can't accept a new io request if:
    //  1) device is removed, 
    //  2) has never been started, 
    //  3) is stopped,
    //  4) has a remove request pending,
    //  5) has a stop device pending
    if ( !UsbCom_CanAcceptIoRequests( DeviceObject ) ) {
        ntStatus = STATUS_DELETE_PENDING;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -