pcideviceconfig.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 299 行

JAVA
299
字号
/*
 * $Id: PCIDeviceConfig.java,v 1.1 2003/11/25 11:41:21 epr Exp $
 */
package org.jnode.driver.pci;

import org.jnode.util.NumberUtils;

/**
 * @author epr
 */
public class PCIDeviceConfig implements PCIConstants {
	
	/** My device */
	private final PCIDevice device;
	/** My device ID */
	private final int deviceID;
	/** The vendor ID */
	private final int vendorID;
	/** The major class */
	private final int majorClass;
	/** The sub class */
	private final int subClass;
	/** The minor class */
	private final int minorClass;
	/** The revision */
	private final int revision;
	/** The header type */
	private final int headerTypeRaw;
	
	/**
	 * Create a new instance
	 * @param device
	 */
	protected PCIDeviceConfig(PCIDevice device) {
		this.device = device;
		int reg0 = getRegister(0);
		int reg2 = getRegister(2);
		int reg3 = getRegister(3);
		this.deviceID = (reg0 >> 16) & 0xFFFF;
		this.vendorID = (reg0 & 0xFFFF);
		this.majorClass = (reg2 >> 24) & 0xFF;
		this.subClass = (reg2 >> 16) & 0xFF;
		this.minorClass = (reg2 >> 8) & 0xFF;
		this.revision = (reg2 & 0xFF);
		this.headerTypeRaw = (reg3 >> 16) & 0xFF;

	}
	
	/**
	 * Gets the identifier of this unit
	 */
	public int getDeviceID() {
		return deviceID;
	}
	
	public static int getDeviceID(PCIDriver pci, int bus, int unit, int func) {
		final int reg0 = reg(pci, bus, unit, func, 0);
		return (reg0 >> 16) & 0xFFFF;
	}
	
	/**
	 * Is this unit present
	 */
	public boolean isPresent() {
		final int devID = getDeviceID();
		switch (devID) {
			case 0xFFFF: return false;
			case 0: return (device.getFunction() == 0);
			default: return true;
		}
	}
	
	/**
	 * Is a device at a given location present?
	 */
	public static boolean isPresent(PCIDriver pci, int bus, int unit, int func) {
		final int devID = getDeviceID(pci, bus, unit, func);
		switch (devID) {
			case 0xFFFF: return false;
			case 0: return (func == 0);
			default: return true;
		}
	}
	
	/**
	 * Gets the PCI class information of a device at a given location
	 * @param pci
	 * @param bus
	 * @param unit
	 * @param func
	 * @return { major, sub, minor, revision }
	 */
	public static int[] getPCIClass(PCIDriver pci, int bus, int unit, int func) {
		final int reg2 = reg(pci, bus, unit, func, 2);
		final int[] rc = new int[4];
		rc[0] = (reg2 >> 24) & 0xFF;
		rc[1] = (reg2 >> 16) & 0xFF;
		rc[2] = (reg2 >> 8) & 0xFF;
		rc[3] = (reg2 & 0xFF);
		return rc;
	}
	
	/**
	 * Gets the identifier of the vendor of this unit
	 */
	public int getVendorID() {
		return vendorID;
	}
	
	/**
	 * Gets the descriptor of the vendor of this unit
	 */
	public VendorDescriptor getVendorDescriptor() {
		return PCIDescriptors.getInstance().findVendor(getVendorID());
	}
	
	/**
	 * Gets the descriptor of this device.
	 */
	public DeviceDescriptor getDeviceDescriptor() {
		return getVendorDescriptor().findDevice(getDeviceID());
	}
	
	/**
	 * Gets the status of this unit
	 */
	public int getStatus() {
		return (getRegister(1) >> 16) & 0xFFFF;
	}
	
	/**
	 * Gets the command info of this unit
	 */
	public int getCommand() {
		return (getRegister(1) & 0xFFFF);
	}
	
	/**
	 * Gets the major class of this unit
	 */
	public int getBaseClass() {
		return majorClass;
	}
	
	/**
	 * Gets the subclass of this unit
	 */
	public int getSubClass() {
		return subClass;
	}
	
	/**
	 * Gets the minor class (API identification) of this unit
	 */
	public int getMinorClass() {
		return minorClass;
	}
	
	/**
	 * Gets the revision of this unit
	 */
	public int getRevision() {
		return revision;
	}
	
	public int getBist() {
		return (getRegister(3) >> 24) & 0xFF;
	}
	
	public int getHeaderType() {
		return headerTypeRaw & 0x7F;
	}
	
	/**
	 * Is this unit a multifunctional device?
	 */
	public boolean isMultiFunctional() {
		return ((headerTypeRaw & 0x80) != 0);
	}
	
	public int getLatency() {
		return (getRegister(3) >> 8) & 0xFF;
	}
	
	public int getCacheLineSize() {
		return (getRegister(3) & 0xFF);
	}
	
	public PCIBaseAddress[] getBaseAddresses() {
		PCIBaseAddress[] addresses = new PCIBaseAddress[6];
		int idx = 0;
		for (int r = 0; r < 6; r++) {
			PCIBaseAddress a = PCIBaseAddress.read(device, r);			
			if (a != null) {
				addresses[idx++] = a;
				if (a.is64Bit()) {
					r++;
				}
			}
		}
		if (idx < 6) {
			PCIBaseAddress[] result = new PCIBaseAddress[idx];
			for (int i = 0; i < idx; i++) {
				result[i] = addresses[i];
			}
			return result;
		} else {
			return addresses;
		}
	}
	
	/**
	 * Gets the interrupt pin
	 */
	public final int getInterruptPin() {
		return device.readConfigByte(PCI_INTERRUPT_PIN);
	}
	
	/**
	 * Gets the interrupt line
	 */
	public final int getInterruptLine() {
		return device.readConfigByte(PCI_INTERRUPT_LINE);
	}
	
	/**
	 * Sets the interrupt line
	 * @param line
	 */
	public final void setInterruptLine(int line) {
		device.writeConfigByte(PCI_INTERRUPT_LINE, line);
	}
	
	/**
	 * Gets the maximum latency.
	 */
	public final int getMaxLatency() {
		return device.readConfigByte(PCI_MAX_LAT);
	}
	
	/**
	 * Gets the primary bus number.
	 * Only valid for bridge devices.
	 */
	public final int getBridgePrimaryBus() {
		return getRegister(6) & 0xFF;
	}
	
	/**
	 * Gets the secondary bus number.
	 * Only valid for bridge devices.
	 */
	public final int getBridgeSecondaryBus() {
		return (getRegister(6) >> 8) & 0xFF;
	}
	
	/**
	 * Convert myself to a String representation.
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String str = "device=0x" + NumberUtils.hex(getDeviceID(), 4) + ", " +
		    "vendor=0x" + NumberUtils.hex(getVendorID(), 4) + ", " +
		    "class=" + getBaseClass() + ":" + getSubClass() + ":" + getMinorClass() + ", " +
		    "revision=" + getRevision() + ", " + 
		    "headertype=" + getHeaderType();
		if (getInterruptPin() != 0) {
			str += ", " + 
			"intr-pin=" + getInterruptPin() + ", " + 
			"intr-line=" + getInterruptLine();
		}
		PCIBaseAddress[] baseAddresses = getBaseAddresses();
		if (baseAddresses.length > 0) {
			str += ", base-addresses={";
			for (int i = 0; i < baseAddresses.length; i++) {
				if (i > 0) {
					str += ",";
				}
				str += baseAddresses[i];
			}
			str += "}";
		}
		if (getHeaderType() == HEADER_TYPE_BRIDGE) {
			str += ", " + 
			"primary-bus=" + getBridgePrimaryBus() + ", " +
			"secondary-bus=" + getBridgeSecondaryBus();
		}
		return str;
	}
	
	public int getRegister(int register) {
		return device.readConfigDword(register, 0);
	}
	
	private static int reg(PCIDriver pci, int bus, int unit, int func, int reg) {
		return pci.readConfigDword(bus, unit, func, reg, 0);
	}
}

⌨️ 快捷键说明

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