feat: taken qsort from PDCLIB, Sorting drivers. Generic driver
structure. Driver ranking
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#ifdef ENABLE_PCIPP
|
||||
|
||||
#include <drivers/pci_devices.h>
|
||||
#include <libc/sort.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -34,17 +35,6 @@
|
||||
#define MASK_BAR_IOSPACE 0xFFFFFFFC
|
||||
#define MASK_BAR_MEMSPACE 0xFFFFFFF0
|
||||
|
||||
const struct pci_driver pci_internal_secondary_bus = {
|
||||
.name = "pci-secondary-bus",
|
||||
.description = "A PCI bus connected to the primary bus",
|
||||
.order = 0,
|
||||
.pci_class = 0x06,
|
||||
.pci_subclass = 0x04,
|
||||
.pci_use_subclass = true,
|
||||
.validate = NULL,
|
||||
.initialize = NULL,
|
||||
};
|
||||
|
||||
//const pci_driver *pci_drivers[MAX_PCI_DRIVERS];
|
||||
extern struct pci_driver __start_pci_driver[];
|
||||
extern struct pci_driver __stop_pci_driver[];
|
||||
@@ -55,6 +45,13 @@ pci_device pci_devices[MAX_PCI_DEVICES];
|
||||
|
||||
void pci_check_bus(uint8_t bus);
|
||||
|
||||
uint8_t used pci_secondary_bus_use(const pci_device *device) {
|
||||
uint8_t secondary_bus = pci_config_read_byte(device->bus, device->slot, device->func,
|
||||
PCI_CONFIG_SECONDARY_BUS_NUMBER);
|
||||
pci_check_bus(secondary_bus);
|
||||
return PCI_USE_OK;
|
||||
}
|
||||
|
||||
uint32_t pci_config_address(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||
return PCI_CONFIG_ENABLE
|
||||
| ((uint32_t) bus << PCI_CONFIG_SHIFT_BUS_NUMBER)
|
||||
@@ -124,32 +121,35 @@ uint8_t pci_get_header_type(uint8_t bus, uint8_t slot, uint8_t func) {
|
||||
}
|
||||
|
||||
void pci_pick_driver(pci_device *device) {
|
||||
// check special drivers
|
||||
// PCI Secondary bus
|
||||
if (device->class == pci_internal_secondary_bus.pci_class &&
|
||||
device->subclass == pci_internal_secondary_bus.pci_subclass) {
|
||||
uint8_t secondary_bus = pci_config_read_byte(device->bus, device->slot, device->func,
|
||||
PCI_CONFIG_SECONDARY_BUS_NUMBER);
|
||||
device->pci_driver = &pci_internal_secondary_bus;
|
||||
pci_check_bus(secondary_bus);
|
||||
return;
|
||||
}
|
||||
|
||||
// use normal drivers
|
||||
for (size_t i = 0; i < NUM_DRIVERS; ++i) {
|
||||
if (DRIVER(i) == NULL) {
|
||||
struct pci_driver *driver = DRIVER(i);
|
||||
if (driver == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (device->class != DRIVER(i)->pci_class) {
|
||||
if (driver->mask.class && driver->match.class != device->class) {
|
||||
continue;
|
||||
}
|
||||
if (DRIVER(i)->pci_use_subclass && device->subclass != DRIVER(i)->pci_subclass) {
|
||||
if (driver->mask.subclass && driver->match.subclass != device->subclass) {
|
||||
continue;
|
||||
}
|
||||
if (!DRIVER(i)->validatable) {
|
||||
if (driver->mask.interface && driver->match.interface != device->programInterface) {
|
||||
continue;
|
||||
}
|
||||
if (DRIVER(i)->validate(device) != PCI_VALIDATE_OK) {
|
||||
if (driver->mask.vendor && driver->match.vendor != device->vendorId) {
|
||||
continue;
|
||||
}
|
||||
if (driver->mask.device && driver->match.device != device->deviceId) {
|
||||
continue;
|
||||
}
|
||||
if (driver->direct_use) {
|
||||
if (driver->use(device) != PCI_USE_OK) {
|
||||
continue;
|
||||
}
|
||||
} else if (driver->validatable) {
|
||||
if (driver->validate(device) != PCI_VALIDATE_OK) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
device->pci_driver = DRIVER(i);
|
||||
@@ -192,8 +192,19 @@ void pci_check_bus(uint8_t bus) {
|
||||
}
|
||||
}
|
||||
|
||||
int pci_driver_compare(const void* a, const void* b) {
|
||||
int rank_a = ((struct pci_driver *) a)->rank;
|
||||
int rank_b = ((struct pci_driver *) b)->rank;
|
||||
if (rank_a > rank_b) {
|
||||
return 1;
|
||||
} else if (rank_a < rank_b) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pci_sort_drivers() {
|
||||
// todo
|
||||
qsort(DRIVER(0), NUM_DRIVERS, sizeof(struct pci_driver), pci_driver_compare);
|
||||
}
|
||||
|
||||
void pci_scan() {
|
||||
@@ -373,4 +384,17 @@ void pci_init_bar(pci_device *device, uint8_t bar_index) {
|
||||
bar->present = 1;
|
||||
}
|
||||
|
||||
// internal drivers
|
||||
PCI_DRIVER(
|
||||
.name = "pci-secondary-bus",
|
||||
.description = "A PCI bus connected to the primary bus",
|
||||
.rank = 0, // internal driver for PCI bus
|
||||
.match.class = PCI_CLASS_BRIDGE,
|
||||
.match.subclass = PCI_SUB_CLASS_PCI_PCI_BRIDGE_4,
|
||||
.mask.class = true,
|
||||
.mask.subclass = true,
|
||||
.direct_use = true,
|
||||
.use = pci_secondary_bus_use,
|
||||
);
|
||||
|
||||
// todo https://wiki.osdev.org/Universal_Serial_Bus if i dare
|
||||
Reference in New Issue
Block a user