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

📄 pciop.c

📁 windows下hardware访问驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
		ret_pci_found = PciopScanIteratePCI(pPciQuery->first,
				pPciQuery->vendorId,
				pPciQuery->deviceId,
				&pPciQuery->slotNum,
				&pci_cm_conf,
				&pPciQuery->busNum);
		KdPrint (("Get PCI device ret %x\n", ret_pci_found));
		if(ret_pci_found)
		{
			KdPrint (("Get one Config sucess\n"));
			pPciResult->pciQuery = *pPciQuery;
			memcpy(&pPciResult->pciComConfig, &pci_cm_conf, sizeof(PCI_COMMON_CONFIG));
			KdPrint(("Get Vendor %x\n", pPciResult->pciComConfig.VendorID));
			Irp->IoStatus.Information = sizeof(PCIQUERYRESULT);
		}else{
			Irp->IoStatus.Information = 2;
			KdPrint (("This is the end\n"));
		}



		break;
	default:

		//
		// The specified I/O control code is unrecognized by this driver.
		//

		ntStatus = STATUS_INVALID_DEVICE_REQUEST;
		SIOCTL_KDPRINT(("ERROR: unrecognized IOCTL %x\n",
				irpSp->Parameters.DeviceIoControl.IoControlCode));
		break;
	}

	//
	// Finish the I/O operation by simply completing the packet and returning
	// the same status as in the packet itself.
	//

	Irp->IoStatus.Status = ntStatus;

	IoCompleteRequest( Irp, IO_NO_INCREMENT );

	return ntStatus;
}

VOID PrintIrpInfo(PIRP Irp) {
	PIO_STACK_LOCATION irpSp;
	irpSp = IoGetCurrentIrpStackLocation(Irp );

	SIOCTL_KDPRINT(("\tIrp->AssociatedIrp.SystemBuffer = 0x%p\n",
			Irp->AssociatedIrp.SystemBuffer));SIOCTL_KDPRINT(("\tIrp->UserBuffer = 0x%p\n", Irp->UserBuffer));SIOCTL_KDPRINT(("\tirpSp->Parameters.DeviceIoControl.Type3InputBuffer = 0x%p\n",
					irpSp->Parameters.DeviceIoControl.Type3InputBuffer));SIOCTL_KDPRINT(("\tirpSp->Parameters.DeviceIoControl.InputBufferLength = %d\n",
							irpSp->Parameters.DeviceIoControl.InputBufferLength));SIOCTL_KDPRINT(("\tirpSp->Parameters.DeviceIoControl.OutputBufferLength = %d\n",
									irpSp->Parameters.DeviceIoControl.OutputBufferLength ));
							return;
}

VOID
PrintHex(
		IN PCHAR BufferAddress,
		IN ULONG CountChars
)
{
	if (CountChars) {

		while (CountChars--) {

			if (*BufferAddress > 31
					&& *BufferAddress != 127) {

				KdPrint (( "%02X", *BufferAddress) );

			} else {

				KdPrint(( ".") );

			}
			BufferAddress++;
		}
		KdPrint (("\n"));
	}
	return;
}

BOOLEAN PciopScanAllPCI(ULONG* pcidevices_found)
/*

 Routine Description:
 This function is called by the driver to find the next PCI card and
 initialize the adapter's configuration.
 Arguments:
 IN BOOLEAN First - TRUE if scan is to start with
 first device
 FALSE to increment to the next
 device
 IN unsigned int pciVendorId - Specified vender ID,
 or 0xFFFF for all venders
 IN unsigned int pciDeviceId - Specified device ID,
 or 0xFFFF for all devices
 IN OUT PCI_SLOT_NUMBER *returnSlotData - Slotdata for found device
 (starting point for
 first == FALSE)
 IN OUT unsigned int *returnBusNumber - Bus number for found card
 Return Value:
 ULONG
 --*/

{
	PCI_SLOT_NUMBER returnSlotData;
	PCI_COMMON_CONFIG returnPciData;
	unsigned int returnBusNumber;
	BOOLEAN ret_scan;
	*pcidevices_found = 0;
	ret_scan = PciopScanIteratePCI(TRUE, 0xffff, 0xffff, &returnSlotData,
			&returnPciData, &returnBusNumber);
	while (ret_scan) {
		(*pcidevices_found)++;
		ret_scan = PciopScanIteratePCI(FALSE, 0xffff, 0xffff, &returnSlotData,
				&returnPciData, &returnBusNumber);

		if(*pcidevices_found > (ULONG)17){
			KdPrint( ("pciScan: Error, too many pci devices\n", *pcidevices_found));
			break;
		}

	}

	KdPrint( ("pciScan: exiting, has found %d pci devices\n", *pcidevices_found));
	return TRUE;

} // end pciScanFindPCI()


BOOLEAN PciopScanIteratePCI(
		IN BOOLEAN First,
		IN unsigned int pciVendorId,
		IN unsigned int pciDeviceId,
		IN OUT PCI_SLOT_NUMBER *returnSlotData,
		IN OUT PCI_COMMON_CONFIG *returnPciData,
		IN OUT unsigned int *returnBusNumber )
/*++

 Routine Description:
 This function is called by the driver to find the next PCI card and
 initialize the adapter's configuration.
 Arguments:
 IN BOOLEAN First - TRUE if scan is to start with
 first device
 FALSE to increment to the next
 device
 IN unsigned int pciVendorId - Specified vender ID,
 or 0xFFFF for all venders
 IN unsigned int pciDeviceId - Specified device ID,
 or 0xFFFF for all devices
 IN OUT PCI_SLOT_NUMBER *returnSlotData - Slotdata for found device
 (starting point for
 first == FALSE)
 IN OUT unsigned int *returnBusNumber - Bus number for found card
 Return Value:
 ULONG
 --*/

{

	PCI_SLOT_NUMBER slotData;
	PCI_COMMON_CONFIG pciData;
	BOOLEAN nextBus = FALSE;
	ULONG busNumber = 1;
	ULONG bytesReturned;

	unsigned int DeviceNumber = returnSlotData->u.bits.DeviceNumber;
	unsigned int FunctionNumber = returnSlotData->u.bits.FunctionNumber;

	BOOLEAN firstFunction;
	BOOLEAN firstDevice;

	KdPrint( ("pciScan: FindPCI \n") );
	//
	// iterate over all PCI slots
	//
	firstFunction = First;
	firstDevice = First;

	slotData.u.AsULONG = 0;

	if (!First)
	{ /* continuation */
		//
		// if not the first we want to point after the last device we found
		//
		FunctionNumber++;
		if (PCI_MAX_FUNCTION <= FunctionNumber)
		{ /* next device */
			FunctionNumber = 0;
			DeviceNumber++;
			if (PCI_MAX_DEVICES <= DeviceNumber)
			{ /* next bus */
				DeviceNumber = 0;
				*returnBusNumber++;
			} /* next bus */
		} /* next device */
	} /* continuation */

	//
	// iterate over all PCI busses looking for devices
	//
	for ( busNumber = First?0:*returnBusNumber;
	busNumber < 256;
	busNumber++, firstDevice = TRUE)
	{ /* busses loop */

		nextBus = FALSE;

		//
		// Iterate over all devices
		//
		for ( DeviceNumber = firstDevice?0:DeviceNumber;
		DeviceNumber < PCI_MAX_DEVICES;
		DeviceNumber++, firstFunction = TRUE )
		{ /* devices loop */


			slotData.u.bits.DeviceNumber = DeviceNumber;
			//
			// Iterate over all functions
			//

			KdPrint( ("pciScan: after check all function plus DeviceNumber= %x\n ",
										slotData.u.bits.DeviceNumber));

			for ( FunctionNumber =
				firstFunction?0:FunctionNumber;
			FunctionNumber < PCI_MAX_FUNCTION;
			FunctionNumber++)
			{ /* functions loop */
				slotData.u.bits.FunctionNumber = FunctionNumber;

				KdPrint( ("pciScan: FindPCI - call HalGetBusData DeviceNumber= %x, Function = %x, busNumber = %x ",
						slotData.u.bits.DeviceNumber,
						slotData.u.bits.FunctionNumber,
						busNumber) );
				RtlZeroMemory(&pciData, sizeof(PCI_COMMON_CONFIG));
				bytesReturned = HalGetBusData( PCIConfiguration,
						busNumber,
						slotData.u.AsULONG,
						&pciData,
						sizeof(PCI_COMMON_CONFIG));

				if (2 == bytesReturned) // device/function does not exist
				{ /* no device/function */
					KdPrint( (" - no match 2 returned pciData.VendorID = %x %x\n",
							pciData.VendorID,
							slotData.u.AsULONG) );
					//
					// MUST CONTINUE WITH LOOP --
					// functions do not have to be consective
					//
				} /* no device/function */
				else if (0 == bytesReturned) // bus does not exist
					{ /* no bus */
						KdPrint( (" - no match 0 next Bus returned pciData.VendorID = %x %x\n",
								pciData.VendorID,
								slotData.u.AsULONG) );
						//
						// We can skip to next bus
						// set flag to break out of device loop
						// and break out of function loop
						//
						nextBus = TRUE;

						break;
					} /* no bus */
					else if (0xffff == pciData.VendorID)
						{ /* invalid vendor ID */
							KdPrint( (" - no match PCI_INVALID_VENDER_ID HalGetBusData pciData.VendorID = %x\n",
									pciData.VendorID) );
						} /* invalid vendor ID */
						else if ( ((0xFFFF == pciVendorId)
									|| ( pciData.VendorID == pciVendorId )) &&
									((0xFFFF == pciDeviceId)
											|| ( pciData.DeviceID == pciDeviceId)))
							{ /* found next */
								// Found it this is the next instance of
								// this card type

								KdPrint( ("\npciScan: ( busNumber, device, function ) = ( %x, %x, %x )\n",
										busNumber,
										slotData.u.bits.DeviceNumber,
										slotData.u.bits.FunctionNumber));

								//dumpPciConfig( pciData );

								KdPrint( ("\n\n") );
								*returnSlotData = slotData;
								*returnPciData = pciData;
								*returnBusNumber = busNumber;

								KdPrint( ("pciScan: Get pci config exiting\n") );
								return TRUE;
							} /* found next */
							else
							{ /* does not match */
								KdPrint( (" - no match pciData.VendorID = %x %x\n", pciData.VendorID, slotData.u.AsULONG) );
							} /* does not match */
			} /* functions loop */

			if (nextBus){
				KdPrint(("Check next bus!\n"));
				break; // break to next bus
			}
		} /* devices loop */
	} /* busses loop */

	KdPrint( ("pciScan-------No more pci devices: exiting\n") );
	return FALSE;

} // end pciScanFindPCI()


NTSTATUS
FindPciDevice(
		IN USHORT VendorId,
		IN USHORT DeviceId
		//OUT PPCI_DEVICE_LIST PciDeviceList
)
{
	ULONG busNumber = 0;
	ULONG count = 0;
	ULONG deviceNumber;
	ULONG functionNumber;
	NTSTATUS status = STATUS_DEVICE_DOES_NOT_EXIST;
	PCI_SLOT_NUMBER slotNumber;
	PCI_COMMON_CONFIG pciData;
	//PPCI_DEVICE_LOCATION pciDeviceLocation;
	KdPrint(("FindPciDevice \n"));
	slotNumber.u.AsULONG = 0;
	//PciDeviceList->Count = 0;
	//pciDeviceLocation = &PciDeviceList->List[0];
	// Scan each bus.
	for ( busNumber = 0; busNumber < 256; busNumber++ ) {
		// Scan each device.
		for ( deviceNumber = 0; deviceNumber < PCI_MAX_DEVICES; deviceNumber++ ) {
			slotNumber.u.bits.DeviceNumber = deviceNumber;
			// Scan each function.
			for ( functionNumber = 0; functionNumber < PCI_MAX_FUNCTION; functionNumber++ ) {
				slotNumber.u.bits.FunctionNumber = functionNumber;
				// Check what's in the current slot.
				if (!HalGetBusData(PCIConfiguration,
						busNumber,
						slotNumber.u.AsULONG,
						&pciData,
						sizeof(ULONG)
				) ) {
					deviceNumber = PCI_MAX_DEVICES;
					break;
				}

				if (pciData.VendorID == PCI_INVALID_VENDORID ) {
					continue;
				}

				KdPrint((
						"PCI device found: bus = 0x%x slot = 0x%x\n",
						busNumber,
						slotNumber
				));

				if ( ( VendorId != PCI_INVALID_VENDORID ) &&
						( pciData.VendorID != VendorId || pciData.DeviceID != DeviceId )) {
					KdPrint((
							"VendorId (0x%0x) or DeviceId (0x%0x) not as requested. \n",
							pciData.VendorID,
							pciData.DeviceID
					));
					// Check next function.
					continue;
				}

				KdPrint(( "VendorId and DeviceId match, VendorId (0x%0x) or DeviceId (0x%0x)! \n",
						pciData.VendorID,
						pciData.DeviceID));
				// At this point, we've found a valid PCI device.
				// Save the found information.
				//pciDeviceLocation->BusNumber = busNumber;
				//pciDeviceLocation->SlotNumber = slotNumber;
				// Increment the PCI device found count.
				// Point to the next location to store data.
				//pciDeviceLocation = &PciDeviceList->List[++count];
				// Indicate at least one device found.
				count++;
				status = STATUS_SUCCESS;
			} // functionNumber
		} // deviceNumber
	} // busNumber
	KdPrint(( "%d matching PCI devices found. \n", count));
	// Update the number of devices in the found list.
	//PciDeviceList->Count = count;
	return status;
} // FindPciDevice

/*
 * Read Write the io port
 */

ULONG
IOReadWrite(
		IN   PCHAR inBuf,
		OUT  PCHAR outBuf
)
{
	NTSTATUS ntStatus = STATUS_SUCCESS;// Assume success
	PIOPORTRWP pIoRwp = (PIOPORTRWP)inBuf;
	ULONG one = 1;
	UCHAR  portWrChar;
	USHORT portWrShort;
	ULONG  portWrLong;
	ULONG ret = pIoRwp->rwSize;
	if(pIoRwp->isRead)
	{
		//read port
		switch(ret)
		{
		case 1:
			READ_PORT_BUFFER_UCHAR((PUCHAR)pIoRwp->portAddress, (PCHAR)outBuf, one);
			//KdPrint(( "Read port %x, value %x.\n", portAddChar, (CHAR)*outBuf));
			break;
		case 2:

			READ_PORT_BUFFER_USHORT((PUSHORT)pIoRwp->portAddress, (PUSHORT)outBuf, one);
			break;
		case 4:

			READ_PORT_BUFFER_ULONG((PULONG)pIoRwp->portAddress, (PULONG)outBuf, one);
			break;
		default:
			ret = 0;
			break;
		}
	}
	else
	{
		//write port
		switch(ret)
		{
		case 1:
			//write port 0x70
			portWrChar = (UCHAR)pIoRwp->portValue;
			WRITE_PORT_BUFFER_UCHAR((PUCHAR)pIoRwp->portAddress, &portWrChar, one);
			//KdPrint(( "Write port %x, value %x.\n", portAddChar, portWrChar));

			break;
		case 2:
			portWrShort = (USHORT)pIoRwp->portValue;
			WRITE_PORT_BUFFER_USHORT((PUSHORT)pIoRwp->portAddress, &portWrShort, one);
			break;
		case 4:
			portWrLong = (ULONG)pIoRwp->portValue;
			WRITE_PORT_BUFFER_ULONG((PULONG)pIoRwp->portAddress, &portWrLong, one);
			break;
		default:
			ret = 0;
			break;
		}
	}
	return ret; //0, error parameter
}

⌨️ 快捷键说明

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