uhciroothub.java

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

JAVA
185
字号
/*
 * $Id: UHCIRootHub.java,v 1.1 2003/11/25 11:42:17 epr Exp $
 */
package org.jnode.driver.usb.uhci;

import org.jnode.driver.usb.USBBus;
import org.jnode.driver.usb.USBDevice;
import org.jnode.driver.usb.USBHubAPI;

/**
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class UHCIRootHub implements USBHubAPI, UHCIConstants {

	private final UHCIIO io;
	private final USBBus bus;
	private final USBDevice[] devices;

	/**
	 * Initialize this instance.
	 * 
	 * @param io
	 */
	public UHCIRootHub(UHCIIO io, USBBus bus) {
		this.io = io;
		this.bus = bus;
		this.devices = new USBDevice[getNumPorts()];
	}
	
	public void resetHub() {
		final int ports = getNumPorts();
		for (int i = 0; i < ports; i++) {
			io.setPortSC(i, 0);
		}
	}

	/**
	 * Gets the bus to which this HUB is connected.
	 */
	public USBBus getUSBBus() {
		return bus;
	}

	/**
	 * Gets the device for the given port.
	 * 
	 * @param port
	 */
	public USBDevice getDevice(int port) {
		return devices[port];
	}

	/**
	 * Set the device for the given port.
	 * 
	 * @param dev
	 * @param port
	 */
	public void setDevice(USBDevice dev, int port) {
		if (devices[port] != null) {
			throw new IllegalStateException("Cannot overwrite the device at port " + port);
		} else {
			this.devices[port] = dev;
		}
	}

	/**
	 * Unset the device for the given port.
	 * 
	 * @param port
	 */
	public void unsetDevice(int port) {
		this.devices[port] = null;
	}

	/**
	 * Gets the number of downstream ports connected to this HUB.
	 */
	public int getNumPorts() {
		return 2;
	}

	/**
	 * Is the given port enabled.
	 */
	public boolean isPortEnabled(int port) {
		testPort(port);
		return io.getPortSCBits(port, USBPORTSC_PE);
	}

	/**
	 * Enable/disable a given port
	 * 
	 * @param enabled
	 */
	public void setPortEnabled(int port, boolean enabled) {
		testPort(port);
		io.setPortSCBits(port, USBPORTSC_PE, enabled);
	}

	/**
	 * Reset a given port
	 */
	public void resetPort(int port) {
		try {
			testPort(port);
			io.setPortSCBits(port, USBPORTSC_PR, true);
			Thread.sleep(100);
			io.setPortSCBits(port, USBPORTSC_PR, false);
			Thread.sleep(1);
			io.setPortSCBits(port, USBPORTSC_PE, true);
			Thread.sleep(10);
			io.setPortSCBits(port, 0xA, true);
			Thread.sleep(200);
		} catch (InterruptedException ex) {
			// Ignore
		}
	}

	/**
	 * Gets the port connection status.
	 * 
	 * @return True if a device is connected, false otherwise
	 */
	public boolean isPortConnected(int port) {
		testPort(port);
		return io.getPortSCBits(port, USBPORTSC_CCS);
	}

	/**
	 * Has the port connection status changed.
	 */
	public boolean isPortConnectionStatusChanged(int port) {
		testPort(port);
		return io.getPortSCBits(port, USBPORTSC_CSC);
	}

	/**
	 * Clear the port connection status changed flag.
	 */
	public void clearPortConnectionStatusChanged(int port) {
		testPort(port);
		io.setPortSCBits(port, USBPORTSC_CSC, true);
	}

	/**
	 * Is a lowspeed device connected to the given port. This method is only relevant if the port
	 * connection status is true.
	 * 
	 * @return True if a lowspeed device is connected, false otherwise (full or high speed)
	 */
	public boolean isPortConnectedToLowSpeed(int port) {
		testPort(port);
		return io.getPortSCBits(port, USBPORTSC_LSDA);
	}

	/**
	 * Is a highspeed device connected to the given port. This method is only relevant if the port
	 * connection status is true.
	 * 
	 * @return True if a highspeed device is connected, false otherwise (low or full)
	 */
	public boolean isPortConnectedToHighSpeed(int port) {
		testPort(port);
		return false;
	}

	public int getPortStatus(int port) {
		testPort(port);
		return io.getPortSC(port);
	}

	/**
	 * Test for a valid port number
	 * 
	 * @param port
	 * @throws IllegalArgumentException
	 */
	private final void testPort(int port) throws IllegalArgumentException {
		if ((port < 0) || (port > 1)) {
			throw new IllegalArgumentException("Invalid port " + port);
		}
	}
}

⌨️ 快捷键说明

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