The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
xhci.c File Reference
#include "xhci.h"
#include "kernel/sync/spinlock.h"
#include "arch/arch.h"
#include "arch/x86/cpu/io.h"
#include "common/memory.h"
#include "drivers/keyboard/keyboard.h"
#include "drivers/pci/pci.h"
#include "drivers/pci/pci_irq.h"
#include "drivers/usb/hid.h"
#include "kernel/input/input.h"
#include "kernel/klog/klog.h"
#include "kernel/memory/mmio.h"
#include "kernel/memory/page.h"
#include "kernel/memory/pmm.h"
#include "kernel/time/timer.h"

Macros

#define MAX_CONTROLLERS   4
 a small xHCI driver for USB keyboards and pointing devices (eXtensible Host Controller Interface 1.2; USB HID 1.11).
#define MAX_HID_DEVICES   8
#define RING_TRBS   256
#define SPIN_LIMIT   50000000u
#define CAP_LENGTH   0x00
#define CAP_HCS1   0x04
#define CAP_HCS2   0x08
#define CAP_HCC1   0x10
#define CAP_DBOFF   0x14
#define CAP_RTSOFF   0x18
#define OP_USBCMD   0x00
#define OP_USBSTS   0x04
#define OP_CRCR   0x18
#define OP_DCBAAP   0x30
#define OP_CONFIG   0x38
#define OP_PORTS   0x400
#define USBCMD_RUN   0x1u
#define USBCMD_RESET   0x2u
#define USBCMD_INTE   0x4u
#define USBSTS_HALTED   0x1u
#define USBSTS_EINT   0x8u
#define USBSTS_NOT_READY   (1u << 11)
#define PORT_CONNECTED   (1u << 0)
#define PORT_ENABLED   (1u << 1)
#define PORT_RESET   (1u << 4)
#define PORT_POWER   (1u << 9)
#define PORT_RESET_DONE   (1u << 21)
#define PORT_CHANGE_BITS   0x00FE0000u
#define PORT_SPEED(v)
#define HCC1_PORT_POWER   (1u << 3)
#define SPEED_FULL   1
#define SPEED_LOW   2
#define SPEED_HIGH   3
#define SPEED_SUPER   4
#define IR0_IMAN   0x20
#define IR0_IMOD   0x24
#define IR0_ERSTSZ   0x28
#define IR0_ERSTBA   0x30
#define IR0_ERDP   0x38
#define ERDP_BUSY   0x8u
#define IMAN_IP   0x1u
#define IMAN_IE   0x2u
#define TRB_NORMAL   1
#define TRB_SETUP   2
#define TRB_DATA   3
#define TRB_STATUS   4
#define TRB_LINK   6
#define TRB_ENABLE_SLOT   9
#define TRB_ADDRESS_DEV   11
#define TRB_CONFIGURE_EP   12
#define TRB_EVALUATE_CTX   13
#define TRB_TRANSFER_EVT   32
#define TRB_COMMAND_EVT   33
#define TRB_CYCLE   (1u << 0)
#define TRB_TOGGLE   (1u << 1)
#define TRB_IOC   (1u << 5)
#define TRB_IDT   (1u << 6)
#define TRB_TYPE(t)
#define TRB_DIR_IN   (1u << 16)
#define COMPLETION_SUCCESS   1
#define COMPLETION_SHORT   13
#define EP_TYPE_CONTROL   4
#define EP_TYPE_INT_IN   7
#define XCAP_LEGACY   1
#define LEGACY_BIOS_OWNS   (1u << 16)
#define LEGACY_OS_OWNS   (1u << 24)
#define REPEAT_DELAY   50
#define REPEAT_RATE   3
#define PAGE_INPUT   0x000
#define PAGE_OUTPUT   0x800
#define PAGE_DATA   0xC00
#define DATA_MAX   0x400

Typedefs

typedef struct xhci xhci_t

Enumerations

enum  hid_kind_t { HID_KEYBOARD , HID_MOUSE , HID_POINTER }

Functions

uint32_t xhci_init (void)
 set up the xHCI controllers and their input devices.

Macro Definition Documentation

◆ MAX_CONTROLLERS

#define MAX_CONTROLLERS   4

a small xHCI driver for USB keyboards and pointing devices (eXtensible Host Controller Interface 1.2; USB HID 1.11).

keyboards and mice use the HID boot protocol (fixed reports). other pointing devices (QEMU's tablet, for example) send the report their report descriptor describes (hid.c). keys go to the keyboard driver as PS/2 scan codes; buttons, motion and the wheel go to /dev/input.

the controller works with rings of 16-byte TRBs in RAM. the driver puts commands on the command ring, transfers on each endpoint's transfer ring, and reads the results from the event ring. a cycle bit in each TRB marks which side owns it; it changes each time a ring wraps (through a link TRB on the command and transfer rings).

during boot (interrupts off) the driver polls the event ring. after boot the controller interrupts (MSI-X, MSI or INTx) for each event; a controller without an interrupt is polled from the timer interrupt. the timer also repeats a held key. each controller has one page for each ring, the device context array, and one page per device for its contexts and its data.

◆ MAX_HID_DEVICES

#define MAX_HID_DEVICES   8

◆ RING_TRBS

#define RING_TRBS   256

◆ SPIN_LIMIT

#define SPIN_LIMIT   50000000u

◆ CAP_LENGTH

#define CAP_LENGTH   0x00

◆ CAP_HCS1

#define CAP_HCS1   0x04

◆ CAP_HCS2

#define CAP_HCS2   0x08

◆ CAP_HCC1

#define CAP_HCC1   0x10

◆ CAP_DBOFF

#define CAP_DBOFF   0x14

◆ CAP_RTSOFF

#define CAP_RTSOFF   0x18

◆ OP_USBCMD

#define OP_USBCMD   0x00

◆ OP_USBSTS

#define OP_USBSTS   0x04

◆ OP_CRCR

#define OP_CRCR   0x18

◆ OP_DCBAAP

#define OP_DCBAAP   0x30

◆ OP_CONFIG

#define OP_CONFIG   0x38

◆ OP_PORTS

#define OP_PORTS   0x400

◆ USBCMD_RUN

#define USBCMD_RUN   0x1u

◆ USBCMD_RESET

#define USBCMD_RESET   0x2u

◆ USBCMD_INTE

#define USBCMD_INTE   0x4u

◆ USBSTS_HALTED

#define USBSTS_HALTED   0x1u

◆ USBSTS_EINT

#define USBSTS_EINT   0x8u

◆ USBSTS_NOT_READY

#define USBSTS_NOT_READY   (1u << 11)

◆ PORT_CONNECTED

#define PORT_CONNECTED   (1u << 0)

◆ PORT_ENABLED

#define PORT_ENABLED   (1u << 1)

◆ PORT_RESET

#define PORT_RESET   (1u << 4)

◆ PORT_POWER

#define PORT_POWER   (1u << 9)

◆ PORT_RESET_DONE

#define PORT_RESET_DONE   (1u << 21)

◆ PORT_CHANGE_BITS

#define PORT_CHANGE_BITS   0x00FE0000u

◆ PORT_SPEED

#define PORT_SPEED ( v)
Value:
(((v) >> 10) & 0xFu)

◆ HCC1_PORT_POWER

#define HCC1_PORT_POWER   (1u << 3)

◆ SPEED_FULL

#define SPEED_FULL   1

◆ SPEED_LOW

#define SPEED_LOW   2

◆ SPEED_HIGH

#define SPEED_HIGH   3

◆ SPEED_SUPER

#define SPEED_SUPER   4

◆ IR0_IMAN

#define IR0_IMAN   0x20

◆ IR0_IMOD

#define IR0_IMOD   0x24

◆ IR0_ERSTSZ

#define IR0_ERSTSZ   0x28

◆ IR0_ERSTBA

#define IR0_ERSTBA   0x30

◆ IR0_ERDP

#define IR0_ERDP   0x38

◆ ERDP_BUSY

#define ERDP_BUSY   0x8u

◆ IMAN_IP

#define IMAN_IP   0x1u

◆ IMAN_IE

#define IMAN_IE   0x2u

◆ TRB_NORMAL

#define TRB_NORMAL   1

◆ TRB_SETUP

#define TRB_SETUP   2

◆ TRB_DATA

#define TRB_DATA   3

◆ TRB_STATUS

#define TRB_STATUS   4

◆ TRB_LINK

#define TRB_LINK   6

◆ TRB_ENABLE_SLOT

#define TRB_ENABLE_SLOT   9

◆ TRB_ADDRESS_DEV

#define TRB_ADDRESS_DEV   11

◆ TRB_CONFIGURE_EP

#define TRB_CONFIGURE_EP   12

◆ TRB_EVALUATE_CTX

#define TRB_EVALUATE_CTX   13

◆ TRB_TRANSFER_EVT

#define TRB_TRANSFER_EVT   32

◆ TRB_COMMAND_EVT

#define TRB_COMMAND_EVT   33

◆ TRB_CYCLE

#define TRB_CYCLE   (1u << 0)

◆ TRB_TOGGLE

#define TRB_TOGGLE   (1u << 1)

◆ TRB_IOC

#define TRB_IOC   (1u << 5)

◆ TRB_IDT

#define TRB_IDT   (1u << 6)

◆ TRB_TYPE

#define TRB_TYPE ( t)
Value:
((uint32_t)(t) << 10)
unsigned int uint32_t
Definition fb.h:17

◆ TRB_DIR_IN

#define TRB_DIR_IN   (1u << 16)

◆ COMPLETION_SUCCESS

#define COMPLETION_SUCCESS   1

◆ COMPLETION_SHORT

#define COMPLETION_SHORT   13

◆ EP_TYPE_CONTROL

#define EP_TYPE_CONTROL   4

◆ EP_TYPE_INT_IN

#define EP_TYPE_INT_IN   7

◆ XCAP_LEGACY

#define XCAP_LEGACY   1

◆ LEGACY_BIOS_OWNS

#define LEGACY_BIOS_OWNS   (1u << 16)

◆ LEGACY_OS_OWNS

#define LEGACY_OS_OWNS   (1u << 24)

◆ REPEAT_DELAY

#define REPEAT_DELAY   50

◆ REPEAT_RATE

#define REPEAT_RATE   3

◆ PAGE_INPUT

#define PAGE_INPUT   0x000

◆ PAGE_OUTPUT

#define PAGE_OUTPUT   0x800

◆ PAGE_DATA

#define PAGE_DATA   0xC00

◆ DATA_MAX

#define DATA_MAX   0x400

Typedef Documentation

◆ xhci_t

typedef struct xhci xhci_t

Enumeration Type Documentation

◆ hid_kind_t

enum hid_kind_t
Enumerator
HID_KEYBOARD 
HID_MOUSE 
HID_POINTER 

Function Documentation

◆ xhci_init()

uint32_t xhci_init ( void )

set up the xHCI controllers and their input devices.

call after pci_init and mmio_init, with interrupts off.

Returns
the number of input devices found.