Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit a80701b

Browse files
committed
add usb driver
1 parent 9214a89 commit a80701b

7 files changed

Lines changed: 545 additions & 18 deletions

File tree

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ IMG_OUT_DIR = $(OUT_DIR)
1616
CFLAGS = -O2 -Wimplicit-function-declaration -Wunused-but-set-variable -ffreestanding -m32 -c -Wall -Wextra -I$(INCLUDE)
1717
LDFLAGS = -m elf_i386
1818
NFLAGS = -f bin
19-
QEMU_FLAGS = -serial stdio -display none -monitor none
20-
QEMU_SERIAL = -serial file:kernel_console.log -display none
19+
QEMU_FLAGS = -serial stdio -display none -monitor none -device qemu-xhci,id=xhci
20+
QEMU_VGA = -display curses -device qemu-xhci,id=xhci
2121
CONSOLE = -display curses
2222

2323
SOURCES = $(shell find $(SRC_KERNEL) -name "*.c")
@@ -33,7 +33,7 @@ LINKER = $(SRC_DIR)/kernel.ld
3333

3434
CALC_SCRIPT = $(SRC_BOOT)/config.inc
3535

36-
.PHONY: all run run-console run-serial clean calculate-sectors
36+
.PHONY: all run run-console run-vga clean calculate-sectors
3737
.DEFAULT_GOAL := all
3838

3939
calculate-sectors: $(KERNEL)
@@ -77,15 +77,15 @@ $(LITECORE_IMG):
7777

7878
run: $(K_OUT_DIR) $(B_OUT_DIR) $(IMG) $(LITECORE_IMG)
7979
make all
80-
$(QEMU) $(QEMU_FLAGS) -drive file=$(IMG),format=raw,if=floppy -drive file=$(LITECORE_IMG),format=raw,if=ide
80+
$(QEMU) $(QEMU_FLAGS) -drive file=$(IMG),format=raw,if=floppy -drive file=$(LITECORE_IMG),format=raw,if=ide -nographic
8181

8282
run-console: $(K_OUT_DIR) $(B_OUT_DIR) $(IMG) $(LITECORE_IMG)
8383
make all
84-
$(QEMU) $(CONSOLE) -drive file=$(IMG),format=raw,if=floppy -drive file=$(LITECORE_IMG),format=raw,if=ide
84+
$(QEMU) $(CONSOLE) -device qemu-xhci,id=xhci -drive file=$(IMG),format=raw,if=floppy -drive file=$(LITECORE_IMG),format=raw,if=ide
8585

86-
run-serial: $(K_OUT_DIR) $(B_OUT_DIR) $(IMG) $(LITECORE_IMG)
86+
run-vga: $(K_OUT_DIR) $(B_OUT_DIR) $(IMG) $(LITECORE_IMG)
8787
make all
88-
$(QEMU) $(QEMU_SERIAL) -drive file=$(IMG),format=raw,if=floppy -drive file=$(LITECORE_IMG),format=raw,if=ide
88+
$(QEMU) $(QEMU_VGA) -drive file=$(IMG),format=raw,if=floppy -drive file=$(LITECORE_IMG),format=raw,if=ide
8989

9090
clean:
9191
rm -rf $(OUT_DIR)

src/boot/config.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
KERNEL_OFFSET EQU 0x10000 ; カーネルをロードするアドレス
2-
SECTOR_COUNT EQU 113 ; 読み込むセクタの数
2+
SECTOR_COUNT EQU 129 ; 読み込むセクタの数
33
START_SECTOR EQU 2 ; 開始するセクタ番号
44
CYLINDER_NUM EQU 0 ; シリンダ番号
55
HEAD_NUM EQU 0 ; ヘッド番号

src/include/driver/usb/xhci.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#ifndef _USB_XHCI_H
2+
#define _USB_XHCI_H
3+
4+
#include <util/config.h>
5+
#include <stdint.h>
6+
7+
/* xHCI USB速度定義 */
8+
#define XHCI_SPEED_FULL 1 /* USB 1.1 Full Speed (12 Mbps) */
9+
#define XHCI_SPEED_LOW 2 /* USB 1.1 Low Speed (1.5 Mbps) */
10+
#define XHCI_SPEED_HIGH 3 /* USB 2.0 High Speed (480 Mbps) */
11+
#define XHCI_SPEED_SUPER 4 /* USB 3.0 Super Speed (5 Gbps) */
12+
13+
/* xHCI Capability Registers (オフセット 0x00から) */
14+
struct xhci_cap_regs {
15+
uint8_t caplength; /* 0x00: Capability Registers Length */
16+
uint8_t reserved;
17+
uint16_t hciversion; /* 0x02: Interface Version Number */
18+
uint32_t hcsparams1; /* 0x04: Structural Parameters 1 */
19+
uint32_t hcsparams2; /* 0x08: Structural Parameters 2 */
20+
uint32_t hcsparams3; /* 0x0C: Structural Parameters 3 */
21+
uint32_t hccparams1; /* 0x10: Capability Parameters 1 */
22+
uint32_t dboff; /* 0x14: Doorbell Offset */
23+
uint32_t rtsoff; /* 0x18: Runtime Register Space Offset */
24+
uint32_t hccparams2; /* 0x1C: Capability Parameters 2 */
25+
};
26+
27+
/* xHCI Operational Registers */
28+
struct xhci_op_regs {
29+
uint32_t usbcmd; /* 0x00: USB Command */
30+
uint32_t usbsts; /* 0x04: USB Status */
31+
uint32_t pagesize; /* 0x08: Page Size */
32+
uint32_t reserved1[2];
33+
uint32_t dnctrl; /* 0x14: Device Notification Control */
34+
uint64_t crcr; /* 0x18: Command Ring Control Register */
35+
uint32_t reserved2[4];
36+
uint64_t dcbaap; /* 0x30: Device Context Base Address Array Pointer */
37+
uint32_t config; /* 0x38: Configure */
38+
};
39+
40+
/* xHCI Port Registers (Operational Registers領域内) */
41+
struct xhci_port_regs {
42+
uint32_t portsc; /* Port Status and Control */
43+
uint32_t portpmsc; /* Port Power Management Status and Control */
44+
uint32_t portli; /* Port Link Info */
45+
uint32_t porthlpmc; /* Port Hardware LPM Control */
46+
};
47+
48+
/* USBCMD Register bits */
49+
#define XHCI_CMD_RUN (1 << 0) /* Run/Stop */
50+
#define XHCI_CMD_HCRST (1 << 1) /* Host Controller Reset */
51+
#define XHCI_CMD_INTE (1 << 2) /* Interrupter Enable */
52+
#define XHCI_CMD_HSEE (1 << 3) /* Host System Error Enable */
53+
#define XHCI_CMD_EWE (1 << 10) /* Enable Wrap Event */
54+
55+
/* USBSTS Register bits */
56+
#define XHCI_STS_HCH (1 << 0) /* HC Halted */
57+
#define XHCI_STS_HSE (1 << 2) /* Host System Error */
58+
#define XHCI_STS_EINT (1 << 3) /* Event Interrupt */
59+
#define XHCI_STS_PCD (1 << 4) /* Port Change Detect */
60+
#define XHCI_STS_CNR (1 << 11) /* Controller Not Ready */
61+
#define XHCI_STS_HCE (1 << 12) /* Host Controller Error */
62+
63+
/* PORTSC Register bits */
64+
#define XHCI_PORTSC_CCS (1 << 0) /* Current Connect Status */
65+
#define XHCI_PORTSC_PED (1 << 1) /* Port Enabled/Disabled */
66+
#define XHCI_PORTSC_PR (1 << 4) /* Port Reset */
67+
#define XHCI_PORTSC_PLS_MASK (0xF << 5) /* Port Link State */
68+
#define XHCI_PORTSC_PP (1 << 9) /* Port Power */
69+
#define XHCI_PORTSC_SPEED_MASK (0xF << 10) /* Port Speed */
70+
#define XHCI_PORTSC_CSC (1 << 17) /* Connect Status Change */
71+
#define XHCI_PORTSC_PEC (1 << 18) /* Port Enabled/Disabled Change */
72+
#define XHCI_PORTSC_PRC (1 << 21) /* Port Reset Change */
73+
74+
/* xHCI Host Controller 構造体 */
75+
struct xhci_hc {
76+
/* MMIO Base Address */
77+
uintptr_t base_addr;
78+
79+
/* Register pointers */
80+
volatile struct xhci_cap_regs *cap_regs;
81+
volatile struct xhci_op_regs *op_regs;
82+
volatile uint32_t *doorbell_array;
83+
volatile void *runtime_regs;
84+
volatile struct xhci_port_regs *port_regs;
85+
86+
/* Controller information */
87+
uint16_t hci_version;
88+
uint32_t max_slots;
89+
uint32_t max_ports;
90+
uint32_t max_intrs;
91+
92+
/* PCI information */
93+
uint8_t bus;
94+
uint8_t device;
95+
uint8_t function;
96+
97+
/* State */
98+
int initialized;
99+
};
100+
101+
/* 関数プロトタイプ */
102+
103+
/**
104+
* @brief PCIバスからxHCIコントローラを検出・初期化
105+
* @return 0: 成功, 負数: エラー
106+
*/
107+
int xhci_init(void);
108+
109+
/**
110+
* @brief xHCIコントローラをリセット
111+
* @param hc ホストコントローラ構造体
112+
* @return 0: 成功, 負数: エラー
113+
*/
114+
int xhci_reset_controller(struct xhci_hc *hc);
115+
116+
/**
117+
* @brief xHCIコントローラを起動
118+
* @param hc ホストコントローラ構造体
119+
* @return 0: 成功, 負数: エラー
120+
*/
121+
int xhci_start_controller(struct xhci_hc *hc);
122+
123+
/**
124+
* @brief xHCIポートの状態を取得
125+
* @param hc ホストコントローラ構造体
126+
* @param port_num ポート番号(1から開始)
127+
* @return ポートステータス
128+
*/
129+
uint32_t xhci_get_port_status(struct xhci_hc *hc, uint32_t port_num);
130+
131+
#endif /* _USB_XHCI_H */

src/include/tests/define.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,33 @@
22
#define _TESTS_CONFIG_H
33

44
// テストを実行するかどうか
5-
// #define TEST_TRUE
5+
#define TEST_TRUE
66

77
#ifdef TEST_TRUE
88
// メモリマップとメモリのテスト
9-
#define MEM_TEST
9+
// #define MEM_TEST
1010
// 割り込みのテスト
11-
#define INTERRUPT_TEST
11+
// #define INTERRUPT_TEST
1212
// 割り込みベクタのテスト
13-
#define INTERRUPT_VECTOR_TEST
13+
// #define INTERRUPT_VECTOR_TEST
1414
// 割り込みハンドラでメモリのテスト
15-
#define ALLOC_IRQ_TEST
15+
// #define ALLOC_IRQ_TEST
1616
// GDT(セグメント)再構築テスト
17-
#define GDT_TEST
17+
// #define GDT_TEST
1818
// ページングのテスト
19-
#define PAGING_TEST
19+
// #define PAGING_TEST
2020
// 仮想メモリのテスト
21-
#define VMEM_TEST
21+
// #define VMEM_TEST
2222
// FAT12 test
23-
#define FAT12_TEST
23+
// #define FAT12_TEST
2424
// ext2 test
25-
#define EXT2_TEST
25+
// #define EXT2_TEST
2626
// ACPI Timer test
2727
// #define ACPI_TIMER_TEST
2828
// APIC Timer test
2929
// #define APIC_TIMER_TEST
30+
// xHCI USB test
31+
#define XHCI_TEST
3032

3133
/* -------- 宣言 -------- */
3234
#ifdef MEM_TEST
@@ -73,6 +75,10 @@ void acpi_timer_test();
7375
void apic_timer_test();
7476
#endif
7577

78+
#ifdef XHCI_TEST
79+
void xhci_test();
80+
#endif
81+
7682
#endif /* TEST_TRUE */
7783

7884
#endif /* _TESTS_CONFIG_H */

0 commit comments

Comments
 (0)