floppycontrollerdriver.java

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

JAVA
145
字号
/*
 * $Id: FloppyControllerDriver.java,v 1.1 2003/11/25 11:50:45 epr Exp $
 */
package org.jnode.driver.floppy;

import java.nio.channels.ClosedByInterruptException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.naming.NameNotFoundException;

import org.apache.log4j.Logger;
import org.jnode.driver.DeviceAlreadyRegisteredException;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.Driver;
import org.jnode.driver.DriverException;
import org.jnode.naming.InitialNaming;
import org.jnode.system.ResourceNotFreeException;
import org.jnode.util.TimeoutException;

/**
 * @author epr
 */
public class FloppyControllerDriver extends Driver {

	/** My logger */
	private final Logger log = Logger.getLogger(getClass());
	/** The controller */
	private FDC fdc;
	/** The global devicemanager */
	private DeviceManager devMan;
	/** All floppy drive devices controlled by this controller */
	private final ArrayList devices = new ArrayList();
	private FloppyControllerBus bus;

	/**
	 * Start the device.
	 * @throws DriverException
	 */
	protected void startDevice() 
	throws DriverException {
		try {
			devices.clear();
			fdc = new FDC(getDevice(), true);
			bus = new FloppyControllerBus(this);
			registerDevices();
		} catch (FloppyException ex) {
			throw new DriverException("Cannot register drives", ex);
		} catch (ResourceNotFreeException ex) {
			throw new DriverException("Cannot claim all resources", ex);
		}
	}

	/**
	 * Stop the device.
	 * @throws DriverException
	 */
	protected void stopDevice() 
	throws DriverException {
		unregisterDevices();
		if (fdc != null) {
			fdc.release();
			fdc = null;
		}
		devices.clear();
		devMan = null;
	}

	/**
	 * Register all existing floppy drives
	 * @throws FloppyException
	 * @throws DriverException
	 */
	protected void registerDevices() 
	throws FloppyException, DriverException {
		try {
			devMan = (DeviceManager)InitialNaming.lookup(DeviceManager.NAME);
		} catch (NameNotFoundException ex) {
			throw new FloppyException("Cannot find device manager", ex);
		}
		final int max = fdc.getDriveCount();
		for (int i = 0; i < max; i++) {
			final FloppyDriveParameters dp = fdc.getDriveParams(i);
			log.debug("For fd" + i + ", found CMOS type " + dp.getCmosType());
			if (dp.isPresent()) {
				try {
					final FloppyDevice fd = new FloppyDevice(bus, i, dp);
					fd.setDriver(new FloppyDriver());
					devMan.register(fd);
					devices.add(fd);
				} catch (DeviceAlreadyRegisteredException ex) {
					log.error("Cannot register fd" + i, ex);
				}
			}
		}
	}

	/**
	 * Unregister all floppy drive devices.
	 * @throws DriverException
	 */
	protected void unregisterDevices() 
	throws DriverException { 
		for (Iterator i = devices.iterator(); i.hasNext(); ) {
			final FloppyDevice fd = (FloppyDevice)i.next();
			devMan.unregister(fd);
		}
	}

	/**
	 * Has the disk changed since the last command?
	 * @param drive
	 * @param resetFlag
	 * @return boolean
	 */
	protected final boolean diskChanged(int drive, boolean resetFlag) {
		return fdc.diskChanged(drive, resetFlag);
	}

	/**
	 * Add the given command to the command queue and wait till the command
	 * has finished.
	 * @param cmd
	 * @param timeout
	 * @throws ClosedByInterruptException
	 * @throws TimeoutException
	 */
	protected final void executeAndWait(FloppyCommand cmd, long timeout)
	throws ClosedByInterruptException, TimeoutException {
		try {
			fdc.executeAndWait(cmd, timeout);
		} catch (InterruptedException ex) {
			throw new ClosedByInterruptException();
		}
	}
	
	/**
	 * Reset the controller
	 */
	protected final void resetFDC() {
		log.debug("Reset FDC");
		fdc.reset();
	}
}

⌨️ 快捷键说明

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