148 lines
5.0 KiB
C
148 lines
5.0 KiB
C
#pragma once
|
|
// io_driver.h - definitive kernel driver/device I/O model
|
|
//
|
|
// Disk controllers register DEVICE_OBJECTs. IRPs are submitted only through
|
|
// IoCallDriver()/IoSubmitIrp(); drivers complete accepted IRPs by calling
|
|
// IoCompleteRequest(). There is no legacy DISK_DRIVER submit table.
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <ntlite/status.h>
|
|
|
|
typedef struct HAL_API HAL_API;
|
|
typedef KSTATUS (*DRIVER_ENTRY_FN)(const HAL_API* hal);
|
|
typedef void (*DRIVER_UNLOAD_FN)(void);
|
|
|
|
struct IRP;
|
|
typedef struct DRIVER_OBJECT DRIVER_OBJECT;
|
|
typedef struct DEVICE_OBJECT DEVICE_OBJECT;
|
|
|
|
typedef enum IO_MAJOR_FUNCTION {
|
|
IRP_MJ_READ = 0,
|
|
IRP_MJ_WRITE = 1,
|
|
IRP_MJ_DEVICE_CONTROL = 2,
|
|
IRP_MJ_MAXIMUM_FUNCTION = 3,
|
|
} IO_MAJOR_FUNCTION;
|
|
|
|
typedef KSTATUS (*PDRIVER_DISPATCH)(DEVICE_OBJECT* device, struct IRP* irp);
|
|
|
|
struct DRIVER_OBJECT {
|
|
const char* Name;
|
|
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION];
|
|
DRIVER_UNLOAD_FN DriverUnload;
|
|
};
|
|
|
|
|
|
|
|
typedef enum IO_BUS_TYPE {
|
|
IoBusUnknown = 0,
|
|
IoBusPci = 1,
|
|
IoBusUsb = 2,
|
|
} IO_BUS_TYPE;
|
|
|
|
#define IO_PUBLISHED_USB_MAX_ENDPOINTS 8u
|
|
|
|
typedef struct IO_PUBLISHED_USB_ENDPOINT {
|
|
uint8_t Address;
|
|
uint8_t Attributes;
|
|
uint16_t MaxPacketSize;
|
|
uint8_t Interval;
|
|
} IO_PUBLISHED_USB_ENDPOINT;
|
|
|
|
typedef struct IO_PUBLISHED_USB_DEVICE {
|
|
uint16_t VendorId;
|
|
uint16_t ProductId;
|
|
uint16_t BcdUsb;
|
|
uint16_t BcdDevice;
|
|
|
|
uint8_t DeviceClass;
|
|
uint8_t DeviceSubclass;
|
|
uint8_t DeviceProtocol;
|
|
|
|
uint8_t SlotId;
|
|
uint8_t Port;
|
|
uint8_t Speed;
|
|
uint8_t ConfigurationValue;
|
|
|
|
uint8_t InterfaceNumber;
|
|
uint8_t AlternateSetting;
|
|
uint8_t InterfaceClass;
|
|
uint8_t InterfaceSubclass;
|
|
uint8_t InterfaceProtocol;
|
|
|
|
uint8_t EndpointCount;
|
|
IO_PUBLISHED_USB_ENDPOINT Endpoints[IO_PUBLISHED_USB_MAX_ENDPOINTS];
|
|
} IO_PUBLISHED_USB_DEVICE;
|
|
|
|
typedef struct IO_PUBLISHED_DEVICE {
|
|
IO_BUS_TYPE BusType;
|
|
const char* Name;
|
|
void* BusContext;
|
|
void* DeviceContext;
|
|
uint32_t Flags;
|
|
bool Bound;
|
|
const char* BoundDriverName;
|
|
|
|
union {
|
|
IO_PUBLISHED_USB_DEVICE Usb;
|
|
} u;
|
|
} IO_PUBLISHED_DEVICE;
|
|
|
|
typedef bool (*IO_DRIVER_MATCH_CALLBACK)(const IO_PUBLISHED_DEVICE* device,
|
|
void* context);
|
|
typedef KSTATUS (*IO_DRIVER_BIND_CALLBACK)(IO_PUBLISHED_DEVICE* device,
|
|
void* context);
|
|
|
|
__declspec(dllexport) KSTATUS IoRegisterDriverMatcher(const char* driverName,
|
|
IO_BUS_TYPE busType,
|
|
IO_DRIVER_MATCH_CALLBACK match,
|
|
IO_DRIVER_BIND_CALLBACK bind,
|
|
void* context);
|
|
__declspec(dllexport) KSTATUS IoPublishDevice(const IO_PUBLISHED_DEVICE* device);
|
|
__declspec(dllexport) uint32_t IoPublishedDeviceCount(void);
|
|
|
|
struct DEVICE_OBJECT {
|
|
DRIVER_OBJECT* DriverObject;
|
|
void* DeviceExtension;
|
|
uint32_t Flags;
|
|
uint32_t AlignmentRequirement;
|
|
uint64_t SectorCount;
|
|
uint8_t DiskIndex;
|
|
};
|
|
|
|
#define DEVICE_OBJECT_DISK 0x00000001u
|
|
#define DEVICE_OBJECT_REMOVABLE 0x00000002u
|
|
|
|
__declspec(dllexport) KSTATUS IoCreateDevice(DRIVER_OBJECT* driver,
|
|
size_t extensionSize,
|
|
DEVICE_OBJECT** outDevice);
|
|
__declspec(dllexport) void IoDeleteDevice(DEVICE_OBJECT* device);
|
|
|
|
__declspec(dllexport) KSTATUS IoRegisterDiskDevice(uint8_t driveIndex,
|
|
DEVICE_OBJECT* device);
|
|
__declspec(dllexport) DEVICE_OBJECT* IoGetDiskDevice(uint8_t driveIndex);
|
|
__declspec(dllexport) bool IoDiskDevicePresent(uint8_t driveIndex);
|
|
__declspec(dllexport) uint64_t IoDiskSectorCount(uint8_t driveIndex);
|
|
|
|
typedef void (*IO_DISK_ARRIVAL_CALLBACK)(uint8_t driveIndex,
|
|
DEVICE_OBJECT* device,
|
|
void* context);
|
|
|
|
__declspec(dllexport) KSTATUS IoRegisterDiskArrivalCallback(IO_DISK_ARRIVAL_CALLBACK callback,
|
|
void* context);
|
|
__declspec(dllexport) KSTATUS IoEnumerateDiskDevices(IO_DISK_ARRIVAL_CALLBACK callback,
|
|
void* context);
|
|
|
|
__declspec(dllexport) KSTATUS IoCallDriver(DEVICE_OBJECT* device,
|
|
struct IRP* irp);
|
|
|
|
__declspec(dllexport) KSTATUS IoReadDiskSectors(uint8_t driveIndex,
|
|
uint64_t lba,
|
|
uint32_t count,
|
|
void* buffer);
|
|
__declspec(dllexport) KSTATUS IoWriteDiskSectors(uint8_t driveIndex,
|
|
uint64_t lba,
|
|
uint32_t count,
|
|
const void* buffer);
|