quinn-os/pci.c
2015-08-24 16:50:16 +10:00

64 lines
2.1 KiB
C

#include "io.h"
#include "pci.h"
#include "string.h"
struct pci_device **pci_devices;
int pci_device_count;
unsigned short pci_config_read_word(unsigned short bus, unsigned short slot, unsigned short func, unsigned short offset) {
unsigned long address;
unsigned long lbus = (unsigned long)bus;
unsigned long lslot = (unsigned long)slot;
unsigned long lfunc = (unsigned long)func;
unsigned short tmp = 0;
address = (unsigned long)((lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | ((unsigned long)0x80000000));
outportl(0xCF8, address);
tmp = (unsigned short)((inportl(0xCFC) >> ((offset & 2) * 8)) & 0xffff);
return tmp;
}
void init_pci(void) {
int bus;
int slot;
int func;
struct pci_device tmp_device;
pci_device_count = 0;
for (bus = 0; bus < 4; bus++) {
for (slot = 0; slot < 32; slot++) {
for (func = 0; func < 8; func++) {
if ((tmp_device.vendor = pci_config_read_word(bus, slot, 0, 0)) != 0xFFFF) {
if (func && !(tmp_device.header_type & 0x80)) {
continue;
}
tmp_device.device = pci_config_read_word(bus, slot, 0, 2);
tmp_device.header_type = pci_config_read_word(bus, slot, 0, 14) & 0x00FF;
tmp_device.classtype = pci_config_read_word(bus, slot, 0, 10)>>8 & 0x00FF;
tmp_device.subclasstype = pci_config_read_word(bus, slot, 0, 10) & 0x00FF;
kprintf("Vendor %x, Class %x, Subclass %x\n", tmp_device.vendor, tmp_device.classtype, tmp_device.subclasstype);
if (pci_device_count == 0) {
pci_devices = (struct pci_device **)malloc(sizeof(struct pci_device *));
} else {
pci_devices = (struct pci_device **)realloc(pci_devices, sizeof(struct pci_device *) * (pci_device_count + 1));
}
pci_devices[pci_device_count] = (struct pci_device *)malloc(sizeof(struct pci_device));
memcpy((char *)pci_devices[pci_device_count], (char *)&tmp_device, sizeof(struct pci_device));
pci_device_count++;
}
}
}
}
kprintf("Found %d PCI devices\n", pci_device_count);
}