X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fdrv%2Fpci.c;h=20eb93261ec6cee8bc44f1028559ad4418c9fccd;hb=e29b02ca55d580b2f7f10d1093c3d6ad1bc59458;hp=e0a25b575947de04de983e0c0d66360c1aeeb5ec;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/drv/pci.c b/Kernel/drv/pci.c index e0a25b57..20eb9326 100644 --- a/Kernel/drv/pci.c +++ b/Kernel/drv/pci.c @@ -1,28 +1,32 @@ /* -AcessOS/AcessBasic v0.1 -PCI Bus Driver -*/ -#include + * AcessOS/AcessBasic v0.1 + * PCI Bus Driver + */ +#define DEBUG 0 +#include +#include #include #include #include -#define DEBUG 0 #define LIST_DEVICES 1 // === STRUCTURES === -typedef struct s_pciDevice { +typedef struct sPCIDevice +{ Uint16 bus, slot, fcn; Uint16 vendor, device; union { - struct {Uint8 class, subclass;}; + struct { + Uint8 class, subclass; + }; Uint16 oc; }; Uint16 revision; Uint32 ConfigCache[256/4]; char Name[8]; tVFS_Node Node; -} t_pciDevice; +} tPCIDevice; // === CONSTANTS === #define SPACE_STEP 5 @@ -45,22 +49,23 @@ Uint32 PCI_GetBAR4(int id); Uint32 PCI_GetBAR5(int id); Uint16 PCI_AssignPort(int id, int bar, int count); - int PCI_EnumDevice(Uint16 bus, Uint16 dev, Uint16 fcn, t_pciDevice *info); + int PCI_EnumDevice(Uint16 bus, Uint16 dev, Uint16 fcn, tPCIDevice *info); Uint32 PCI_CfgReadDWord(Uint16 bus, Uint16 dev, Uint16 func, Uint16 offset); void PCI_CfgWriteDWord(Uint16 bus, Uint16 dev, Uint16 func, Uint16 offset, Uint32 data); Uint16 PCI_CfgReadWord(Uint16 bus, Uint16 dev, Uint16 func, Uint16 offset); Uint8 PCI_CfgReadByte(Uint16 bus, Uint16 dev, Uint16 func, Uint16 offset); // === GLOBALS === -//MODULE_DEFINE(0, 0x0100, PCI, PCI_Install, NULL); +MODULE_DEFINE(0, 0x0100, PCI, PCI_Install, NULL, NULL); int giPCI_BusCount = 1; int giPCI_InodeHandle = -1; int giPCI_DeviceCount = 0; -t_pciDevice *gPCI_Devices = NULL; +tPCIDevice *gPCI_Devices = NULL; tDevFS_Driver gPCI_DriverStruct = { NULL, "pci", { .Flags = VFS_FFLAG_DIRECTORY, + .Size = -1, .NumACLs = 1, .ACLs = &gVFS_ACL_EveryoneRX, .ReadDir = PCI_ReadDirRoot, @@ -78,92 +83,104 @@ int PCI_Install(char **Arguments) { int bus, dev, fcn, i; int space = 0; - t_pciDevice devInfo; + tPCIDevice devInfo; void *tmpPtr = NULL; // Build Portmap gaPCI_PortBitmap = malloc( 1 << 13 ); + if( !gaPCI_PortBitmap ) { + Log_Error("PCI", "Unable to allocate %i bytes for bitmap", 1 << 13); + return MODULE_ERR_MALLOC; + } memset( gaPCI_PortBitmap, 0, 1 << 13 ); for( i = 0; i < MAX_RESERVED_PORT / 32; i ++ ) gaPCI_PortBitmap[i] = -1; for( i = 0; i < MAX_RESERVED_PORT % 32; i ++ ) gaPCI_PortBitmap[MAX_RESERVED_PORT / 32] = 1 << i; - //LogF("Done.\n"); // Scan Busses for( bus = 0; bus < giPCI_BusCount; bus++ ) { - for( dev = 0; dev < 10; dev++ ) // 10 Devices per bus + for( dev = 0; dev < 32; dev++ ) // 32 Devices per bus { - for( fcn = 0; fcn < 8; fcn++ ) // 8 functions per device + for( fcn = 0; fcn < 8; fcn++ ) // Max 8 functions per device { // Check if the device/function exists if(!PCI_EnumDevice(bus, dev, fcn, &devInfo)) - { continue; - } if(giPCI_DeviceCount == space) { space += SPACE_STEP; - tmpPtr = realloc(gPCI_Devices, space*sizeof(t_pciDevice)); + tmpPtr = realloc(gPCI_Devices, space*sizeof(tPCIDevice)); if(tmpPtr == NULL) - break; + return MODULE_ERR_MALLOC; gPCI_Devices = tmpPtr; } if(devInfo.oc == PCI_OC_PCIBRIDGE) { #if LIST_DEVICES - Log("[PCI ] Bridge @ %i,%i:%i (0x%x:0x%x)", + Log_Log("PCI", "Bridge @ %i,%i:%i (0x%x:0x%x)", bus, dev, fcn, devInfo.vendor, devInfo.device); #endif giPCI_BusCount++; } + else + { + #if LIST_DEVICES + Log_Log("PCI", "Device %i,%i:%i %04x => 0x%04x:0x%04x", + bus, dev, fcn, devInfo.oc, devInfo.vendor, devInfo.device); + #endif + } + devInfo.Node.Inode = giPCI_DeviceCount; - memcpy(&gPCI_Devices[giPCI_DeviceCount], &devInfo, sizeof(t_pciDevice)); + memcpy(&gPCI_Devices[giPCI_DeviceCount], &devInfo, sizeof(tPCIDevice)); giPCI_DeviceCount ++; - #if LIST_DEVICES - Log("[PCI ] Device %i,%i:%i => 0x%x:0x%x", - bus, dev, fcn, devInfo.vendor, devInfo.device); - #endif // WTF is this for? + // Maybe bit 23 must be set for the device to be valid? + // - Actually, maybe 23 means that there are sub-functions if(fcn == 0) { if( !(devInfo.ConfigCache[3] & 0x800000) ) break; } } - if(tmpPtr != gPCI_Devices) - break; } - if(tmpPtr != gPCI_Devices) - break; } - tmpPtr = realloc(gPCI_Devices, giPCI_DeviceCount*sizeof(t_pciDevice)); + + if(giPCI_DeviceCount == 0) { + Log_Notice("PCI", "No devices were found"); + return MODULE_ERR_NOTNEEDED; + } + + tmpPtr = realloc(gPCI_Devices, giPCI_DeviceCount*sizeof(tPCIDevice)); if(tmpPtr == NULL) - return 0; + return MODULE_ERR_MALLOC; gPCI_Devices = tmpPtr; - //LogF("Done.\n"); - // Complete Driver Structure + // Complete Driver Structure gPCI_DriverStruct.RootNode.Size = giPCI_DeviceCount; // And add to DevFS DevFS_AddDevice(&gPCI_DriverStruct); - return 1; + return MODULE_ERR_OK; } /** - * \fn char *PCI_ReadDirRoot(tVFS_Node *node, int pos) + * \fn char *PCI_ReadDirRoot(tVFS_Node *Node, int Pos) * \brief Read from Root of PCI Driver */ -char *PCI_ReadDirRoot(tVFS_Node *node, int pos) -{ - if(pos < 0 || pos >= giPCI_DeviceCount) +char *PCI_ReadDirRoot(tVFS_Node *Node, int Pos) +{ + ENTER("pNode iPos", Node, Pos); + if(Pos < 0 || Pos >= giPCI_DeviceCount) { + LEAVE('n'); return NULL; + } - return gPCI_Devices[pos].Name; + LEAVE('s', gPCI_Devices[Pos].Name); + return strdup( gPCI_Devices[Pos].Name ); } /** * \fn tVFS_Node *PCI_FindDirRoot(tVFS_Node *node, char *filename) @@ -279,8 +296,8 @@ int PCI_GetDeviceByClass(Uint16 class, Uint16 mask, int prev) for( ; i < giPCI_DeviceCount; i++ ) { - if((gPCI_Devices[i].oc & mask) != class) continue; - return i; + if((gPCI_Devices[i].oc & mask) == class) + return i; } return -1; } @@ -361,7 +378,7 @@ Uint16 PCI_AssignPort(int id, int bar, int count) Uint16 portVals; int gran=0; int i, j; - t_pciDevice *dev; + tPCIDevice *dev; //LogF("PCI_AssignPort: (id=%i,bar=%i,count=%i)\n", id, bar, count); @@ -418,9 +435,9 @@ Uint16 PCI_AssignPort(int id, int bar, int count) } /** - * \fn int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, t_pciDevice *info) + * \fn int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info) */ -int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, t_pciDevice *info) +int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info) { Uint16 vendor; int i; @@ -463,7 +480,7 @@ int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, t_pciDevice *info) info->Name[2] = '.'; info->Name[3] = '0' + slot/10; info->Name[4] = '0' + slot%10; - info->Name[5] = '.'; + info->Name[5] = ':'; info->Name[6] = '0' + fcn; info->Name[7] = '\0'; @@ -548,9 +565,10 @@ Uint8 PCI_CfgReadByte(Uint16 bus, Uint16 dev, Uint16 func, Uint16 offset) // === EXPORTS === -/* +//* EXPORT(PCI_CountDevices); EXPORT(PCI_GetDevice); +EXPORT(PCI_GetDeviceByClass); EXPORT(PCI_AssignPort); EXPORT(PCI_GetIRQ); -*/ +//*/