2023-06-09 21:26:04 +01:00
|
|
|
#ifndef SOCKS_PCI_H_
|
|
|
|
|
#define SOCKS_PCI_H_
|
|
|
|
|
|
2023-06-10 13:23:57 +01:00
|
|
|
#include <socks/device.h>
|
2023-06-09 21:26:04 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2023-06-10 13:23:57 +01:00
|
|
|
#define PCI_REG_VENDOR_ID 0x00 // 2
|
|
|
|
|
#define PCI_REG_DEVICE_ID 0x02 // 2
|
|
|
|
|
#define PCI_REG_COMMAND 0x04 // 2
|
|
|
|
|
#define PCI_REG_STATUS 0x06 // 2
|
|
|
|
|
#define PCI_REG_REVISION_ID 0x08 // 1
|
|
|
|
|
|
|
|
|
|
#define PCI_REG_PROG_IF 0x09 // 1
|
|
|
|
|
#define PCI_REG_SUBCLASS 0x0a // 1
|
|
|
|
|
#define PCI_REG_CLASS 0x0b // 1
|
|
|
|
|
#define PCI_REG_CACHE_LINE_SIZE 0x0c // 1
|
|
|
|
|
#define PCI_REG_LATENCY_TIMER 0x0d // 1
|
|
|
|
|
#define PCI_REG_HEADER_TYPE 0x0e // 1
|
|
|
|
|
#define PCI_REG_BIST 0x0f // 1
|
|
|
|
|
#define PCI_REG_BAR0 0x10 // 4
|
|
|
|
|
#define PCI_REG_BAR1 0x14 // 4
|
|
|
|
|
#define PCI_REG_BAR2 0x18 // 4
|
|
|
|
|
#define PCI_REG_BAR3 0x1C // 4
|
|
|
|
|
#define PCI_REG_BAR4 0x20 // 4
|
|
|
|
|
#define PCI_REG_BAR5 0x24 // 4
|
|
|
|
|
|
|
|
|
|
#define PCI_REG_INTERRUPT_LINE 0x3C // 1
|
|
|
|
|
#define PCI_REG_INTERRUPT_PIN 0x3D
|
|
|
|
|
|
|
|
|
|
#define PCI_REG_SECONDARY_BUS 0x19 // 1
|
2023-06-09 21:26:04 +01:00
|
|
|
|
|
|
|
|
#define PCI_HEADER_TYPE_DEVICE 0
|
|
|
|
|
#define PCI_HEADER_TYPE_BRIDGE 1
|
|
|
|
|
#define PCI_HEADER_TYPE_CARDBUS 2
|
|
|
|
|
|
|
|
|
|
#define PCI_TYPE_BRIDGE 0x0604
|
|
|
|
|
#define PCI_TYPE_SATA 0x0106
|
|
|
|
|
|
|
|
|
|
#define PCI_ADDRESS_PORT 0xCF8
|
|
|
|
|
#define PCI_VALUE_PORT 0xCFC
|
|
|
|
|
|
|
|
|
|
#define PCI_NONE 0xFFFF
|
|
|
|
|
|
2023-06-10 13:23:57 +01:00
|
|
|
#define PCI_SUBSYSTEM_KEXT_ID "net.doorstuck.socks.pci"
|
|
|
|
|
|
|
|
|
|
#define PCI_DEVICE_ID(vid, did) { .pci_vendor_id = (vid), .pci_device_id = (did) }
|
|
|
|
|
#define PCI_DEVICE_ID_INVALID { .pci_vendor_id = PCI_NONE, .pci_device_id = PCI_NONE }
|
|
|
|
|
|
|
|
|
|
struct pci_device_id {
|
|
|
|
|
uint16_t pci_vendor_id;
|
|
|
|
|
uint16_t pci_device_id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct pci_device {
|
|
|
|
|
struct pci_device_id pci_id;
|
|
|
|
|
unsigned int pci_bus, pci_slot, pci_func;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct pci_driver {
|
|
|
|
|
struct driver pci_base;
|
|
|
|
|
struct queue_entry pci_head;
|
|
|
|
|
const struct pci_device_id *pci_target_devices;
|
|
|
|
|
|
|
|
|
|
kern_status_t(*probe)(struct pci_driver *, struct device *);
|
|
|
|
|
kern_status_t(*remove)(struct pci_driver *, struct device *);
|
|
|
|
|
};
|
2023-06-09 21:26:04 +01:00
|
|
|
|
|
|
|
|
static inline int pci_get_bus(uint32_t device)
|
|
|
|
|
{
|
|
|
|
|
return (uint8_t)((device >> 16));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int pci_get_slot(uint32_t device)
|
|
|
|
|
{
|
|
|
|
|
return (uint8_t)((device >> 8));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int pci_get_func(uint32_t device)
|
|
|
|
|
{
|
|
|
|
|
return (uint8_t)(device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint32_t pci_get_addr(uint32_t device, int field)
|
|
|
|
|
{
|
|
|
|
|
return 0x80000000 | (pci_get_bus(device) << 16) | (pci_get_slot(device) << 11) | (pci_get_func(device) << 8) | ((field) & 0xFC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint32_t pci_box_device(int bus, int slot, int func)
|
|
|
|
|
{
|
|
|
|
|
return (uint32_t)((bus << 16) | (slot << 8) | func);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 13:23:57 +01:00
|
|
|
extern struct pci_driver *pci_driver_create(struct kext *self, const char *name, const struct pci_device_id *device_ids);
|
|
|
|
|
extern kern_status_t pci_driver_destroy(struct pci_driver *driver);
|
|
|
|
|
extern kern_status_t pci_driver_register(struct pci_driver *driver);
|
|
|
|
|
extern kern_status_t pci_driver_unregister(struct pci_driver *driver);
|
|
|
|
|
static inline struct driver *pci_driver_base(struct pci_driver *driver)
|
|
|
|
|
{
|
|
|
|
|
return &driver->pci_base;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 21:26:04 +01:00
|
|
|
extern uint32_t pci_read_field(uint32_t device, int field, int size);
|
|
|
|
|
extern void pci_write_field(uint32_t device, int field, int size, uint32_t value);
|
|
|
|
|
extern uint16_t pci_find_type(uint32_t dev);
|
|
|
|
|
|
2023-06-10 13:23:57 +01:00
|
|
|
extern uint32_t pci_device_read_field(struct device *dev, int field, int size);
|
|
|
|
|
extern void pci_device_write_field(struct device *dev, int field, int size, uint32_t value);
|
|
|
|
|
|
2023-06-09 21:26:04 +01:00
|
|
|
#endif
|