Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LIBDRIVER_H
- #define LIBDRIVER_H
- #include <stdint.h>
- #include <stddef.h>
- /*
- * Note: any functions not marked as weak might not be implemented by the host, in that
- * case the driver may choose to either try and continue without them, or just fail
- */
- ///////////////////////////////////////////////////////////////////////////
- // Memory Allocation
- ///////////////////////////////////////////////////////////////////////////
- /**
- * The addr represents a physical address to allocate from
- *
- * @remark
- * This implies LIBDRIVER_FLAG_CONTIGUOUS
- */
- #define LIBDRIVER_FLAG_PHYS (1 << 0)
- /**
- * The memory allocated should be physically contiguous
- */
- #define LIBDRIVER_FLAG_CONTIGUOUS (1 << 1)
- /**
- * Map memory for the driver
- *
- * @remark
- * The driver can assume the given memory is READ/WRITE but not EXEC
- *
- * @param addr [IN] Used with LIBDRIVER_FLAG_PHYS
- * @param length [IN] The length of memory to map
- * @param flags [IN] Flags about how to allocate the memory
- */
- void* libdriver_map(uintptr_t addr, size_t length, int flags);
- /**
- * Unmap mapped memory
- *
- * @param addr [IN] The virtual address to unmap from
- * @param length [IN] The length to unmap
- */
- int libdriver_unmap(void* addr, size_t length);
- /**
- * Will convert the given virtual address to a physical address
- *
- * @param addr [IN] The virtual address
- */
- void* libdriver_virt_to_phys(uintptr_t addr);
- ///////////////////////////////////////////////////////////////////////////
- // IO
- ///////////////////////////////////////////////////////////////////////////
- /**
- * PORT IO operations
- */
- void libdriver_outb(uint16_t port, uint8_t data);
- void libdriver_outw(uint16_t port, uint16_t data);
- void libdriver_outd(uint16_t port, uint32_t data);
- uint8_t libdriver_inb(uint16_t port);
- uint16_t libdriver_inw(uint16_t port);
- uint32_t libdriver_ind(uint16_t port);
- /**
- * PCI operations
- */
- void libdriver_pci_writeb(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset, uint8_t data);
- uint8_t libdriver_pci_readb(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset);
- void libdriver_pci_writew(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset, uint16_t data);
- uint16_t libdriver_pci_readw(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset);
- void libdriver_pci_writed(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset, uint32_t data);
- uint32_t libdriver_pci_readd(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function, uint16_t offset);
- #define LIBDRIVER_INVALID_IRQ (-1)
- /**
- * Get the pci irq from the given pci device
- *
- * This may return LIBDRIVER_INVALID_IRQ if there was a problem routing the irq
- */
- __attribute__((weak)) int libdriver_pci_irq_route(uint16_t segment, uint8_t bus, uint8_t device, uint8_t function);
- ///////////////////////////////////////////////////////////////////////////
- // Interrupt handling
- ///////////////////////////////////////////////////////////////////////////
- typedef void(*libdriver_function_t)(void* context);
- /**
- * Register an interrupt handler
- *
- * The interrupt is an GSI, so if working in apic mode it has to be redirected in the APIC
- *
- * @remark
- * Passing LIBDRIVER_INVALID_IRQ will do nothing
- *
- * @remark
- * The driver can assume that all the mapped and allocated memory from the libdriver functions are
- * still accessible from the interrupt
- *
- * @param irq [IN] The interrupt to register
- * @param handler [IN] The handler to call
- * @param user [IN] The data to pass to the interrupt handler
- */
- __attribute__((weak)) void libdriver_request_interrupt(int irq, libdriver_function_t handler, void* user);
- /**
- * Request from the host an address and data for an msi, with the handler to call on that interrupt
- *
- * The idea is to allow the driver to use MSI/MSI-X but for the host to still manage the interrupt themselves
- *
- * @remark
- * The driver can assume that all the mapped and allocated memory from the libdriver functions are
- * still accessible from the interrupt
- *
- * @param msi_addr [OUT] The address to write in the msi
- * @param msi_data [OUT] The data to write in the msi
- * @param handler [IN] The handler to call on interrupt
- * @param user [IN] The data to pass to the interrupt handler
- */
- __attribute__((weak)) void libdriver_request_msi(uint64_t* msi_addr, uint32_t* msi_data, libdriver_function_t handler, void* user);
- ///////////////////////////////////////////////////////////////////////////
- // Threading
- ///////////////////////////////////////////////////////////////////////////
- /*
- * Create a new thread
- *
- * The device should always call kill thread at the end of the handler
- *
- * @param handler [IN] The entry of the thread
- * @param user [OUT] The data to call the entry with
- */
- __attribute__((weak)) void* libdriver_create_thread(libdriver_function_t handler, void* user);
- /**
- * Yield from the current thread
- */
- __attribute__((weak)) void libdriver_thread_yield();
- /**
- * Kill the thread using the value returned on the thread creation
- */
- __attribute__((weak)) void libdriver_kill_thread(void* thread);
- ///////////////////////////////////////////////////////////////////////////
- // Logging related
- ///////////////////////////////////////////////////////////////////////////
- /**
- * Something really bad that the driver can not correct itself from has
- * happened, assume the driver will not work anymore
- */
- #define LIBDRIVER_LOG_FATAL (4)
- /**
- * Something bad happened but the driver can still work, but might have
- * some side affects on the operations
- */
- #define LIBDRIVER_LOG_ERROR (3)
- /**
- * Something happened which was unexpected but is fine
- */
- #define LIBDRIVER_LOG_WARN (2)
- /**
- * Debug messages, usually used to diagnose driver problems
- */
- #define LIBDRIVER_LOG_DEBUG (1)
- /**
- * Allows the driver to log stuff, the host may choose to not implement this function
- */
- __attribute__((weak)) void libdriver_log(int level, const char* message);
- #endif //LIBDRIVER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement