mirror of
https://github.com/Fennix-Project/Drivers.git
synced 2025-07-10 22:59:25 +00:00
Added drivers
This commit is contained in:
220
include/io.h
Normal file
220
include/io.h
Normal file
@ -0,0 +1,220 @@
|
||||
#ifndef __FENNIX_API_IO_H__
|
||||
#define __FENNIX_API_IO_H__
|
||||
|
||||
#include <types.h>
|
||||
|
||||
#if defined(__amd64__) || defined(__i386__)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
static inline uint8_t inportb(uint16_t Port)
|
||||
{
|
||||
uint8_t Result;
|
||||
__asm__("in %%dx, %%al"
|
||||
: "=a"(Result)
|
||||
: "d"(Port));
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint16_t inportw(uint16_t Port)
|
||||
{
|
||||
uint16_t Result;
|
||||
__asm__("in %%dx, %%ax"
|
||||
: "=a"(Result)
|
||||
: "d"(Port));
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint32_t inportl(uint16_t Port)
|
||||
{
|
||||
uint32_t Result;
|
||||
__asm__ volatile("inl %1, %0"
|
||||
: "=a"(Result)
|
||||
: "dN"(Port));
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline void outportb(uint16_t Port, uint8_t Data)
|
||||
{
|
||||
__asm__ volatile("out %%al, %%dx"
|
||||
:
|
||||
: "a"(Data), "d"(Port));
|
||||
}
|
||||
|
||||
static inline void outportw(uint16_t Port, uint16_t Data)
|
||||
{
|
||||
__asm__ volatile("out %%ax, %%dx"
|
||||
:
|
||||
: "a"(Data), "d"(Port));
|
||||
}
|
||||
|
||||
static inline void outportl(uint16_t Port, uint32_t Data)
|
||||
{
|
||||
__asm__ volatile("outl %1, %0"
|
||||
:
|
||||
: "dN"(Port), "a"(Data));
|
||||
}
|
||||
|
||||
static inline uint8_t mmioin8(uint64_t Address)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
uint8_t Result = *(volatile uint8_t *)Address;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint16_t mmioin16(uint64_t Address)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
uint16_t Result = *(volatile uint16_t *)Address;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint32_t mmioin32(uint64_t Address)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
uint32_t Result = *(volatile uint32_t *)Address;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint64_t mmioin64(uint64_t Address)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
uint64_t Result = *(volatile uint64_t *)Address;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline void mmioout8(uint64_t Address, uint8_t Data)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
*(volatile uint8_t *)Address = Data;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void mmioout16(uint64_t Address, uint16_t Data)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
*(volatile uint16_t *)Address = Data;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void mmioout32(uint64_t Address, uint32_t Data)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
*(volatile uint32_t *)Address = Data;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void mmioout64(uint64_t Address, uint64_t Data)
|
||||
{
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
*(volatile uint64_t *)Address = Data;
|
||||
__asm__ volatile("" ::
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void mmoutb(void *Address, uint8_t Value)
|
||||
{
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=m"((*(uint8_t *)(Address)))
|
||||
: "r"(Value)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void mmoutw(void *Address, uint16_t Value)
|
||||
{
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=m"((*(uint16_t *)(Address)))
|
||||
: "r"(Value)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void mmoutl(void *Address, uint32_t Value)
|
||||
{
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=m"((*(uint32_t *)(Address)))
|
||||
: "r"(Value)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void mmoutq(void *Address, uint64_t Value)
|
||||
{
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=m"((*(uint64_t *)(Address)))
|
||||
: "r"(Value)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline uint8_t mminb(void *Address)
|
||||
{
|
||||
uint8_t Result;
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=r"(Result)
|
||||
: "m"((*(uint8_t *)(Address)))
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint16_t mminw(void *Address)
|
||||
{
|
||||
uint16_t Result;
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=r"(Result)
|
||||
: "m"((*(uint16_t *)(Address)))
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint32_t mminl(void *Address)
|
||||
{
|
||||
uint32_t Result;
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=r"(Result)
|
||||
: "m"((*(uint32_t *)(Address)))
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
|
||||
static inline uint64_t mminq(void *Address)
|
||||
{
|
||||
uint64_t Result;
|
||||
__asm__ volatile("mov %1, %0"
|
||||
: "=r"(Result)
|
||||
: "m"((*(uint64_t *)(Address)))
|
||||
: "memory");
|
||||
return Result;
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#define inb(Port) inportb(Port)
|
||||
#define inw(Port) inportw(Port)
|
||||
#define inl(Port) inportl(Port)
|
||||
#define outb(Port, Data) outportb(Port, Data)
|
||||
#define outw(Port, Data) outportw(Port, Data)
|
||||
#define outl(Port, Data) outportl(Port, Data)
|
||||
|
||||
#endif // defined(__amd64__) || defined(__i386__)
|
||||
|
||||
#endif // !__FENNIX_API_IO_H__
|
128
include/netools.h
Normal file
128
include/netools.h
Normal file
@ -0,0 +1,128 @@
|
||||
#ifndef __FENNIX_API_NETWORK_UTILS_H__
|
||||
#define __FENNIX_API_NETWORK_UTILS_H__
|
||||
|
||||
#include <types.h>
|
||||
|
||||
typedef __UINT64_TYPE__ uint48_t;
|
||||
|
||||
struct MediaAccessControl
|
||||
{
|
||||
uint8_t Address[6];
|
||||
|
||||
inline bool operator==(const MediaAccessControl &lhs) const
|
||||
{
|
||||
return lhs.Address[0] == this->Address[0] &&
|
||||
lhs.Address[1] == this->Address[1] &&
|
||||
lhs.Address[2] == this->Address[2] &&
|
||||
lhs.Address[3] == this->Address[3] &&
|
||||
lhs.Address[4] == this->Address[4] &&
|
||||
lhs.Address[5] == this->Address[5];
|
||||
}
|
||||
|
||||
inline bool operator==(const uint48_t &lhs) const
|
||||
{
|
||||
MediaAccessControl MAC;
|
||||
MAC.Address[0] = (uint8_t)((lhs >> 40) & 0xFF);
|
||||
MAC.Address[1] = (uint8_t)((lhs >> 32) & 0xFF);
|
||||
MAC.Address[2] = (uint8_t)((lhs >> 24) & 0xFF);
|
||||
MAC.Address[3] = (uint8_t)((lhs >> 16) & 0xFF);
|
||||
MAC.Address[4] = (uint8_t)((lhs >> 8) & 0xFF);
|
||||
MAC.Address[5] = (uint8_t)(lhs & 0xFF);
|
||||
return MAC.Address[0] == this->Address[0] &&
|
||||
MAC.Address[1] == this->Address[1] &&
|
||||
MAC.Address[2] == this->Address[2] &&
|
||||
MAC.Address[3] == this->Address[3] &&
|
||||
MAC.Address[4] == this->Address[4] &&
|
||||
MAC.Address[5] == this->Address[5];
|
||||
}
|
||||
|
||||
inline bool operator!=(const MediaAccessControl &lhs) const { return !(*this == lhs); }
|
||||
inline bool operator!=(const uint48_t &lhs) const { return !(*this == lhs); }
|
||||
|
||||
inline uint48_t ToHex()
|
||||
{
|
||||
return ((uint48_t)this->Address[0] << 40) |
|
||||
((uint48_t)this->Address[1] << 32) |
|
||||
((uint48_t)this->Address[2] << 24) |
|
||||
((uint48_t)this->Address[3] << 16) |
|
||||
((uint48_t)this->Address[4] << 8) |
|
||||
((uint48_t)this->Address[5]);
|
||||
}
|
||||
|
||||
inline MediaAccessControl FromHex(uint48_t Hex)
|
||||
{
|
||||
this->Address[0] = (uint8_t)((Hex >> 40) & 0xFF);
|
||||
this->Address[1] = (uint8_t)((Hex >> 32) & 0xFF);
|
||||
this->Address[2] = (uint8_t)((Hex >> 24) & 0xFF);
|
||||
this->Address[3] = (uint8_t)((Hex >> 16) & 0xFF);
|
||||
this->Address[4] = (uint8_t)((Hex >> 8) & 0xFF);
|
||||
this->Address[5] = (uint8_t)(Hex & 0xFF);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline bool Valid()
|
||||
{
|
||||
// TODO: More complex MAC validation
|
||||
return (this->Address[0] != 0 ||
|
||||
this->Address[1] != 0 ||
|
||||
this->Address[2] != 0 ||
|
||||
this->Address[3] != 0 ||
|
||||
this->Address[4] != 0 ||
|
||||
this->Address[5] != 0) &&
|
||||
(this->Address[0] != 0xFF ||
|
||||
this->Address[1] != 0xFF ||
|
||||
this->Address[2] != 0xFF ||
|
||||
this->Address[3] != 0xFF ||
|
||||
this->Address[4] != 0xFF ||
|
||||
this->Address[5] != 0xFF);
|
||||
}
|
||||
};
|
||||
|
||||
struct InternetProtocol4
|
||||
{
|
||||
uint8_t Address[4];
|
||||
|
||||
inline bool operator==(const InternetProtocol4 &lhs) const
|
||||
{
|
||||
return lhs.Address[0] == this->Address[0] &&
|
||||
lhs.Address[1] == this->Address[1] &&
|
||||
lhs.Address[2] == this->Address[2] &&
|
||||
lhs.Address[3] == this->Address[3];
|
||||
}
|
||||
|
||||
inline bool operator==(const uint32_t &lhs) const
|
||||
{
|
||||
InternetProtocol4 IP;
|
||||
IP.Address[0] = (uint8_t)((lhs >> 24) & 0xFF);
|
||||
IP.Address[1] = (uint8_t)((lhs >> 16) & 0xFF);
|
||||
IP.Address[2] = (uint8_t)((lhs >> 8) & 0xFF);
|
||||
IP.Address[3] = (uint8_t)(lhs & 0xFF);
|
||||
|
||||
return IP.Address[0] == this->Address[0] &&
|
||||
IP.Address[1] == this->Address[1] &&
|
||||
IP.Address[2] == this->Address[2] &&
|
||||
IP.Address[3] == this->Address[3];
|
||||
}
|
||||
|
||||
inline bool operator!=(const InternetProtocol4 &lhs) const { return !(*this == lhs); }
|
||||
inline bool operator!=(const uint32_t &lhs) const { return !(*this == lhs); }
|
||||
|
||||
inline uint32_t ToHex()
|
||||
{
|
||||
return ((uint64_t)this->Address[0] << 24) |
|
||||
((uint64_t)this->Address[1] << 16) |
|
||||
((uint64_t)this->Address[2] << 8) |
|
||||
((uint64_t)this->Address[3]);
|
||||
}
|
||||
|
||||
inline InternetProtocol4 FromHex(uint32_t Hex)
|
||||
{
|
||||
this->Address[0] = (uint8_t)((Hex >> 24) & 0xFF);
|
||||
this->Address[1] = (uint8_t)((Hex >> 16) & 0xFF);
|
||||
this->Address[2] = (uint8_t)((Hex >> 8) & 0xFF);
|
||||
this->Address[3] = (uint8_t)(Hex & 0xFF);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !__FENNIX_API_NETWORK_UTILS_H__
|
72
include/pci.h
Normal file
72
include/pci.h
Normal file
@ -0,0 +1,72 @@
|
||||
#ifndef __FENNIX_API_PCI_H__
|
||||
#define __FENNIX_API_PCI_H__
|
||||
|
||||
#include <types.h>
|
||||
|
||||
/* https://sites.uclouvain.be/SystInfo/usr/include/linux/pci_regs.h.html */
|
||||
enum PCICommands
|
||||
{
|
||||
/** @brief Enable response in I/O space */
|
||||
PCI_COMMAND_IO = 0x1,
|
||||
/** @brief Enable response in Memory space */
|
||||
PCI_COMMAND_MEMORY = 0x2,
|
||||
/** @brief Enable bus mastering */
|
||||
PCI_COMMAND_MASTER = 0x4,
|
||||
/** @brief Enable response to special cycles */
|
||||
PCI_COMMAND_SPECIAL = 0x8,
|
||||
/** @brief Use memory write and invalidate */
|
||||
PCI_COMMAND_INVALIDATE = 0x10,
|
||||
/** @brief Enable palette snooping */
|
||||
PCI_COMMAND_VGA_PALETTE = 0x20,
|
||||
/** @brief Enable parity checking */
|
||||
PCI_COMMAND_PARITY = 0x40,
|
||||
/** @brief Enable address/data stepping */
|
||||
PCI_COMMAND_WAIT = 0x80,
|
||||
/** @brief Enable SERR */
|
||||
PCI_COMMAND_SERR = 0x100,
|
||||
/** @brief Enable back-to-back writes */
|
||||
PCI_COMMAND_FAST_BACK = 0x200,
|
||||
/** @brief INTx Emulation Disable */
|
||||
PCI_COMMAND_INTX_DISABLE = 0x400
|
||||
};
|
||||
|
||||
struct PCIDeviceHeader
|
||||
{
|
||||
uint16_t VendorID;
|
||||
uint16_t DeviceID;
|
||||
uint16_t Command;
|
||||
uint16_t Status;
|
||||
uint8_t RevisionID;
|
||||
uint8_t ProgIF;
|
||||
uint8_t Subclass;
|
||||
uint8_t Class;
|
||||
uint8_t CacheLineSize;
|
||||
uint8_t LatencyTimer;
|
||||
uint8_t HeaderType;
|
||||
uint8_t BIST;
|
||||
};
|
||||
|
||||
struct PCIHeader0
|
||||
{
|
||||
PCIDeviceHeader Header;
|
||||
uint32_t BAR0;
|
||||
uint32_t BAR1;
|
||||
uint32_t BAR2;
|
||||
uint32_t BAR3;
|
||||
uint32_t BAR4;
|
||||
uint32_t BAR5;
|
||||
uint32_t CardbusCISPtr;
|
||||
uint16_t SubsystemVendorID;
|
||||
uint16_t SubsystemID;
|
||||
uint32_t ExpansionROMBaseAddr;
|
||||
uint8_t CapabilitiesPtr;
|
||||
uint8_t Rsv0;
|
||||
uint16_t Rsv1;
|
||||
uint32_t Rsv2;
|
||||
uint8_t InterruptLine;
|
||||
uint8_t InterruptPin;
|
||||
uint8_t MinGrant;
|
||||
uint8_t MaxLatency;
|
||||
};
|
||||
|
||||
#endif // !__FENNIX_API_PCI_H__
|
42
include/types.h
Normal file
42
include/types.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef __FENNIX_API_TYPES_H__
|
||||
#define __FENNIX_API_TYPES_H__
|
||||
|
||||
typedef __INT8_TYPE__ int8_t;
|
||||
typedef __INT16_TYPE__ int16_t;
|
||||
typedef __INT32_TYPE__ int32_t;
|
||||
typedef __INT64_TYPE__ int64_t;
|
||||
typedef __UINT8_TYPE__ uint8_t;
|
||||
typedef __UINT16_TYPE__ uint16_t;
|
||||
typedef __UINT32_TYPE__ uint32_t;
|
||||
typedef __UINT64_TYPE__ uint64_t;
|
||||
|
||||
typedef __INT_LEAST8_TYPE__ int_least8_t;
|
||||
typedef __INT_LEAST16_TYPE__ int_least16_t;
|
||||
typedef __INT_LEAST32_TYPE__ int_least32_t;
|
||||
typedef __INT_LEAST64_TYPE__ int_least64_t;
|
||||
typedef __UINT_LEAST8_TYPE__ uint_least8_t;
|
||||
typedef __UINT_LEAST16_TYPE__ uint_least16_t;
|
||||
typedef __UINT_LEAST32_TYPE__ uint_least32_t;
|
||||
typedef __UINT_LEAST64_TYPE__ uint_least64_t;
|
||||
|
||||
typedef __INT_FAST8_TYPE__ int_fast8_t;
|
||||
typedef __INT_FAST16_TYPE__ int_fast16_t;
|
||||
typedef __INT_FAST32_TYPE__ int_fast32_t;
|
||||
typedef __INT_FAST64_TYPE__ int_fast64_t;
|
||||
typedef __UINT_FAST8_TYPE__ uint_fast8_t;
|
||||
typedef __UINT_FAST16_TYPE__ uint_fast16_t;
|
||||
typedef __UINT_FAST32_TYPE__ uint_fast32_t;
|
||||
typedef __UINT_FAST64_TYPE__ uint_fast64_t;
|
||||
|
||||
typedef __INTPTR_TYPE__ intptr_t;
|
||||
typedef __UINTPTR_TYPE__ uintptr_t;
|
||||
|
||||
typedef __INTMAX_TYPE__ intmax_t;
|
||||
typedef __UINTMAX_TYPE__ uintmax_t;
|
||||
|
||||
typedef __PTRDIFF_TYPE__ ptrdiff_t;
|
||||
typedef __SIZE_TYPE__ size_t;
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
#endif // !__FENNIX_API_TYPES_H__
|
Reference in New Issue
Block a user