/// <summary> /// Claims the resources. /// </summary> /// <param name="hardwareResources">The hardware resources.</param> /// <returns></returns> public bool ClaimResources(IHardwareResources hardwareResources) { spinLock.Enter(); for (byte r = 0; r < hardwareResources.IOPointRegionCount - 1; r++) { IIOPortRegion region = hardwareResources.GetIOPortRegion(r); for (int p = 0; p < region.Size; p++) { if (portUsed[region.BaseIOPort + p]) { return(false); } } } for (byte r = 0; r < hardwareResources.IOPointRegionCount; r++) { IIOPortRegion region = hardwareResources.GetIOPortRegion(r); for (int p = 0; p < region.Size; p++) { portUsed[region.BaseIOPort + p] = true; } } spinLock.Exit(); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="HardwareResources"/> class. /// </summary> /// <param name="resourceManager">The resource manager.</param> /// <param name="ioPortRegions">The io port regions.</param> /// <param name="memoryRegions">The memory regions.</param> /// <param name="interruptHandler">The interrupt handler.</param> public HardwareResources(IResourceManager resourceManager, IIOPortRegion[] ioPortRegions, IMemoryRegion[] memoryRegions, IInterruptHandler interruptHandler) { this.resourceManager = resourceManager; this.ioPortRegions = ioPortRegions; this.memoryRegions = memoryRegions; this.interruptHandler = interruptHandler; }
/// <summary> /// Releases the resources. /// </summary> /// <param name="hardwareResources">The hardware resources.</param> public void ReleaseResources(IHardwareResources hardwareResources) { spinLock.Enter(); for (byte r = 0; r < hardwareResources.IOPointRegionCount; r++) { IIOPortRegion region = hardwareResources.GetIOPortRegion(r); for (int p = 0; p < region.Size; p++) { portUsed[region.BaseIOPort + p] = false; } } spinLock.Exit(); }
/// <summary> /// Starts the device. /// </summary> /// <param name="pciDevice">The pci device.</param> public static void StartDevice(IPCIDevice pciDevice) { DeviceDriver deviceDriver = deviceDriverRegistry.FindDriver(pciDevice); if (deviceDriver == null) { pciDevice.SetNoDriverFound(); return; } IHardwareDevice hardwareDevice = System.Activator.CreateInstance(deviceDriver.DriverType) as IHardwareDevice; int ioRegionCount = 0; int memoryRegionCount = 0; foreach (BaseAddress pciBaseAddress in pciDevice.BaseAddresses) { switch (pciBaseAddress.Region) { case AddressType.IO: ioRegionCount++; break; case AddressType.Memory: memoryRegionCount++; break; default: break; } } foreach (DeviceDriverPhysicalMemoryAttribute memoryAttribute in deviceDriver.MemoryAttributes) { if (memoryAttribute.MemorySize > 0) { memoryRegionCount++; } } IIOPortRegion[] ioPortRegions = new IIOPortRegion[ioRegionCount]; IMemoryRegion[] memoryRegions = new IMemoryRegion[memoryRegionCount]; int ioRegionIndex = 0; int memoryRegionIndex = 0; foreach (BaseAddress pciBaseAddress in pciDevice.BaseAddresses) { switch (pciBaseAddress.Region) { case AddressType.IO: ioPortRegions[ioRegionIndex++] = new IOPortRegion((ushort)pciBaseAddress.Address, (ushort)pciBaseAddress.Size); break; case AddressType.Memory: memoryRegions[memoryRegionIndex++] = new MemoryRegion(pciBaseAddress.Address, pciBaseAddress.Size); break; default: break; } } foreach (DeviceDriverPhysicalMemoryAttribute memoryAttribute in deviceDriver.MemoryAttributes) { if (memoryAttribute.MemorySize > 0) { IMemory memory = Mosa.DeviceSystem.HAL.AllocateMemory(memoryAttribute.MemorySize, memoryAttribute.MemoryAlignment); memoryRegions[memoryRegionIndex++] = new MemoryRegion(memory.Address, memory.Size); } } HardwareResources hardwareResources = new HardwareResources(resourceManager, ioPortRegions, memoryRegions, new InterruptHandler(resourceManager.InterruptManager, pciDevice.IRQ, hardwareDevice), pciDevice as IDeviceResource); if (resourceManager.ClaimResources(hardwareResources)) { hardwareResources.EnableIRQ(); if (hardwareDevice.Start() == DeviceDriverStartStatus.Started) pciDevice.SetDeviceOnline(); else { hardwareResources.DisableIRQ(); resourceManager.ReleaseResources(hardwareResources); } } }
/// <summary> /// Starts the device. /// </summary> /// <param name="deviceDriver">The device driver.</param> public static void StartDevice(ISADeviceDriverAttribute driverAtttribute, IHardwareDevice hardwareDevice) { int ioRegionCount = 1; int memoryRegionCount = 0; if (driverAtttribute.AltBasePort != 0x00) { ioRegionCount++; } if (driverAtttribute.BaseAddress != 0x00) memoryRegionCount++; IIOPortRegion[] ioPortRegions = new IIOPortRegion[ioRegionCount]; IMemoryRegion[] memoryRegions = new IMemoryRegion[memoryRegionCount]; ioPortRegions[0] = new IOPortRegion(driverAtttribute.BasePort, driverAtttribute.PortRange); if (driverAtttribute.AltBasePort != 0x00) { ioPortRegions[1] = new IOPortRegion(driverAtttribute.AltBasePort, driverAtttribute.AltPortRange); } if (driverAtttribute.BaseAddress != 0x00) { memoryRegions[0] = new MemoryRegion(driverAtttribute.BaseAddress, driverAtttribute.AddressRange); } IHardwareResources hardwareResources = new HardwareResources(resourceManager, ioPortRegions, memoryRegions, new InterruptHandler(resourceManager.InterruptManager, driverAtttribute.IRQ, hardwareDevice)); hardwareDevice.Setup(hardwareResources); Mosa.Kernel.x86.Screen.NextLine(); Mosa.CoolWorld.x86.Boot.BulletPoint(); Console.Write("Adding device "); Boot.InBrackets(hardwareDevice.Name, Colors.White, Colors.LightGreen); Console.WriteLine(); if (resourceManager.ClaimResources(hardwareResources)) { hardwareResources.EnableIRQ(); if (hardwareDevice.Start() == DeviceDriverStartStatus.Started) { deviceManager.Add(hardwareDevice); } else { hardwareResources.DisableIRQ(); resourceManager.ReleaseResources(hardwareResources); } } }