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

📄 usbhubdriver.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
字号:
/*
 * $Id: USBHubDriver.java,v 1.1 2003/11/25 11:42:24 epr Exp $
 */
package org.jnode.driver.usb.hub;

import org.apache.log4j.Logger;
import org.jnode.driver.Driver;
import org.jnode.driver.DriverException;
import org.jnode.driver.usb.USBRequest;
import org.jnode.driver.usb.SetupPacket;
import org.jnode.driver.usb.USBBus;
import org.jnode.driver.usb.USBControlPipe;
import org.jnode.driver.usb.USBDevice;
import org.jnode.driver.usb.USBException;
import org.jnode.driver.usb.USBHubAPI;
import org.jnode.driver.usb.USBHubMonitor;

/**
 * Driver for USB Hubs.
 * 
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class USBHubDriver extends Driver implements USBHubAPI, USBHubConstants {

	/** My logger */
	private final Logger log = Logger.getLogger(getClass());
	/** The number of ports */
	private int nrPorts;
	/** The devices per port */
	private USBDevice[] devices;
	/** The monitor for this HUB */
	private USBHubMonitor monitor;
	/** The HUB device itself */
	private USBDevice dev;
	/** The HUB descriptor */
	private HubDescriptor descr;
	/** Data packet for getPortStatus */
	private final PortStatus statusData = new PortStatus();

	/**
	 * @see org.jnode.driver.Driver#startDevice()
	 */
	protected void startDevice() throws DriverException {
		this.dev = (USBDevice) getDevice();

		try {
			// Read the descriptor
			this.descr = new HubDescriptor();
			dev.readDescriptor(USB_RT_HUB, USB_DT_HUB, 0, 0, -1, descr);
			log.debug("Read HUB: " + descr);
			// Set the variables
			this.nrPorts = descr.getNumPorts();
			this.devices = new USBDevice[nrPorts];

			// Power the ports
			powerOn();
			
			// Create the monitor
			monitor = new USBHubMonitor(dev, this);
			monitor.startMonitor();
		} catch (USBException ex) {
			throw new DriverException(ex);
		}
	}

	/**
	 * @see org.jnode.driver.Driver#stopDevice()
	 */
	protected void stopDevice() throws DriverException {
		// First stop all devices connected to this HUB.
		for (int i = 0; i < nrPorts; i++) {
			final USBDevice dev = devices[i];
			if (dev != null) {
				dev.stop();
			}
			devices[i] = null;
		}
		if (monitor != null) {
			monitor.stopMonitor();
			monitor = null;
		}
		this.nrPorts = 0;
		this.devices = null;
		this.dev = null;
		// Now unregister the API
		final USBDevice dev = (USBDevice) getDevice();
		dev.unregisterAPI(USBHubAPI.class);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#clearPortConnectionStatusChanged(int)
	 */
	public void clearPortConnectionStatusChanged(int port) 
	throws USBException {
		testPort(port);
		clearPortFeature(port, USB_PORT_FEAT_C_CONNECTION);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#getDevice(int)
	 */
	public USBDevice getDevice(int port) {
		testPort(port);
		return this.devices[port];
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#getNumPorts()
	 */
	public int getNumPorts() {
		return nrPorts;
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#getPortStatus(int)
	 */
	public synchronized int getPortStatus(int port) throws USBException {
		readPortStatus(port, statusData);
		return (statusData.getPortStatus() << 16) | statusData.getPortChange();
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#getUSBBus()
	 */
	public USBBus getUSBBus() {
		return ((USBDevice) getDevice()).getUSBBus();
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#isPortConnected(int)
	 */
	public synchronized boolean isPortConnected(int port) throws USBException {
		readPortStatus(port, statusData);
		return statusData.getPortStatusBits(USB_PORT_STAT_CONNECTION);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#isPortConnectedToHighSpeed(int)
	 */
	public boolean isPortConnectedToHighSpeed(int port) throws USBException {
		readPortStatus(port, statusData);
		return statusData.getPortStatusBits(USB_PORT_STAT_HIGH_SPEED);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#isPortConnectedToLowSpeed(int)
	 */
	public boolean isPortConnectedToLowSpeed(int port) throws USBException {
		readPortStatus(port, statusData);
		return statusData.getPortStatusBits(USB_PORT_STAT_LOW_SPEED);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#isPortConnectionStatusChanged(int)
	 */
	public boolean isPortConnectionStatusChanged(int port) throws USBException {
		readPortStatus(port, statusData);
		return statusData.getPortChangeBits(USB_PORT_STAT_C_CONNECTION);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#isPortEnabled(int)
	 */
	public boolean isPortEnabled(int port) throws USBException {
		readPortStatus(port, statusData);
		return statusData.getPortStatusBits(USB_PORT_STAT_ENABLE);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#resetPort(int)
	 */
	public void resetPort(int port) throws USBException {
		testPort(port);
		setPortFeature(port, USB_PORT_FEAT_RESET);
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#setDevice(org.jnode.driver.usb.USBDevice, int)
	 */
	public void setDevice(USBDevice dev, int port) {
		testPort(port);
		this.devices[port] = dev;
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#setPortEnabled(int, boolean)
	 */
	public void setPortEnabled(int port, boolean enabled) throws USBException {
		testPort(port);
		if (enabled) {
			setPortFeature(port, USB_PORT_FEAT_ENABLE);
		} else {
			clearPortFeature(port, USB_PORT_FEAT_ENABLE);
		}
	}

	/**
	 * @see org.jnode.driver.usb.USBHubAPI#unsetDevice(int)
	 */
	public void unsetDevice(int port) {
		testPort(port);
		devices[port] = null;
	}

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

	/**
	 * Clear a HUB feature
	 * 
	 * @param featureSelector
	 * @throws USBException
	 */
	/*private final void clearHubFeature(int featureSelector) throws USBException {
		dev.clearFeature(USB_RT_HUB, 0, featureSelector);
	}*/

	/**
	 * Clear a Port feature
	 * 
	 * @param port
	 *            0..nrPorts-1
	 * @param featureSelector
	 * @throws USBException
	 */
	private final void clearPortFeature(int port, int featureSelector) throws USBException {
		dev.clearFeature(USB_RT_PORT, port+1, featureSelector);
	}

	/**
	 * Set a HUB feature
	 * 
	 * @param featureSelector
	 * @throws USBException
	 */
	/*private final void setHubFeature(int featureSelector) throws USBException {
		dev.setFeature(USB_RT_HUB, 0, featureSelector);
	}*/

	/**
	 * Set a Port feature
	 * 
	 * @param port
	 *            0..nrPorts-1
	 * @param featureSelector
	 * @throws USBException
	 */
	private final void setPortFeature(int port, int featureSelector) throws USBException {
		dev.setFeature(USB_RT_PORT, port+1, featureSelector);
	}
	
	/**
	 * Set power on all ports
	 * @throws USBException
	 */
	private final void powerOn()
	throws USBException {
		for (int port = 0; port < nrPorts; port++) {
			setPortFeature(port, USB_PORT_FEAT_POWER);
		}
		// Wait for power good
		try {
			Thread.sleep(descr.getPowerOn2PowerGood());
		} catch (InterruptedException ex) {
			// Ignore
		}
	}

	/**
	 * Read the status of a given port into the given structure.
	 */
	private void readPortStatus(int port, PortStatus data) throws USBException {
		testPort(port);
		final USBControlPipe pipe = dev.getDefaultControlPipe();
		final USBRequest req = pipe.createRequest(new SetupPacket(USB_DIR_IN | USB_RT_PORT, USB_REQ_GET_STATUS, 0, port+1, 4), data);
		pipe.syncSubmit(req, GET_TIMEOUT);
	}

}

⌨️ 快捷键说明

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