Преглед на файлове

xhci: 获取设备描述符并配置endpoint (#50)

* 调整:使用宏定义来声明usb请求包

* new: 获取usb设备的config描述符

* new: 获取接口及端点描述符

* 临时修正系统根分区的挂载,使得能在真机上启动shell

* xhci: set_config

* bugfix: 解决之前错误的将control_endpoint信息绑定在xhci_hc下的bug

* xhci configure endpoint(存在bug

* 1

* 解决了configure endpoint出错的问题

* new: xhci驱动程序能够配置端点

* 删除trace usb的代码

xhci: 获取设备描述符并配置endpoint
存在问题:尚未能够正确初始化usb键盘,也没有将usb键盘相关的代码独立出来。
login преди 2 години
родител
ревизия
fb417a5e81

+ 26 - 21
kernel/driver/disk/ahci/ahci.c

@@ -7,12 +7,14 @@
 #include <common/string.h>
 #include <common/block.h>
 #include <filesystem/MBR.h>
+#include <debug/bug.h>
 
 struct pci_device_structure_header_t *ahci_devs[MAX_AHCI_DEVICES];
 
 struct block_device_request_queue ahci_req_queue;
 
 struct blk_gendisk ahci_gendisk0 = {0}; // 暂时硬性指定一个ahci_device
+static int __first_port = -1;           // 临时用于存储 ahci控制器的第一个可用端口 的变量
 
 static uint32_t count_ahci_devices = 0;
 
@@ -98,16 +100,17 @@ static int ahci_init_gendisk()
     // todo: 支持GPT
 
     ((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->ahci_ctrl_num = 0;
-    ((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->ahci_port_num = 0;
+    ((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->ahci_port_num = __first_port;
 
     MBR_read_partition_table(&ahci_gendisk0, ((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->part_table);
+
     struct MBR_disk_partition_table_t *ptable = ((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->part_table;
 
     // 求出可用分区数量
     for (int i = 0; i < 4; ++i)
     {
         // 分区可用
-        if (ptable->DPTE[i].type !=0)
+        if (ptable->DPTE[i].type != 0)
             ++ahci_gendisk0.part_cnt;
     }
     if (ahci_gendisk0.part_cnt)
@@ -119,7 +122,7 @@ static int ahci_init_gendisk()
         for (int i = 0; i < 4; ++i)
         {
             // 分区可用
-            if (ptable->DPTE[i].type !=0)
+            if (ptable->DPTE[i].type != 0)
             {
                 // 初始化分区结构体
                 ahci_gendisk0.partition[cnt].bd_disk = &ahci_gendisk0;
@@ -171,14 +174,13 @@ void ahci_init()
     ahci_port_base_vaddr = (uint64_t)kmalloc(1048576, 0);
     kdebug("ahci_port_base_vaddr=%#018lx", ahci_port_base_vaddr);
     ahci_probe_port(0);
-    port_rebase(&ahci_devices[0].hba_mem->ports[0], 0);
 
     // 初始化请求队列
     ahci_req_queue.in_service = NULL;
     wait_queue_init(&ahci_req_queue.wait_queue_list, NULL);
     ahci_req_queue.request_count = 0;
 
-    ahci_init_gendisk();
+    BUG_ON(ahci_init_gendisk() != 0);
     kinfo("AHCI initialized.");
 }
 
@@ -224,25 +226,28 @@ static void ahci_probe_port(const uint32_t device_num)
         {
             uint dt = check_type(&abar->ports[i]);
             ahci_devices[i].type = dt;
-            if (dt == AHCI_DEV_SATA)
+            switch (dt)
             {
+            case AHCI_DEV_SATA:
                 kdebug("SATA drive found at port %d", i);
-            }
-            else if (dt == AHCI_DEV_SATAPI)
-            {
+                goto found;
+            case AHCI_DEV_SATAPI:
                 kdebug("SATAPI drive found at port %d", i);
-            }
-            else if (dt == AHCI_DEV_SEMB)
-            {
+                goto found;
+            case AHCI_DEV_SEMB:
                 kdebug("SEMB drive found at port %d", i);
-            }
-            else if (dt == AHCI_DEV_PM)
-            {
+                goto found;
+            case AHCI_DEV_PM:
                 kdebug("PM drive found at port %d", i);
-            }
-            else
-            {
-                // kdebug("No drive found at port %d", i);
+                goto found;
+            found:;
+                port_rebase(&ahci_devices[0].hba_mem->ports[i], i);
+                if (__first_port == -1)
+                    __first_port = i;
+                break;
+            default:
+                kdebug("No drive found at port %d", i);
+                break;
             }
         }
     }
@@ -599,7 +604,7 @@ static long ahci_query_disk()
     ahci_req_queue.in_service = (struct block_device_request_packet *)pack;
     list_del(&(ahci_req_queue.in_service->wait_queue.wait_list));
     --ahci_req_queue.request_count;
-
+    // kdebug("ahci_query_disk");
     long ret_val = 0;
 
     switch (pack->blk_pak.cmd)
@@ -615,7 +620,7 @@ static long ahci_query_disk()
         ret_val = E_UNSUPPORTED_CMD;
         break;
     }
-
+    // kdebug("ahci_query_disk: retval=%d", ret_val);
     // ahci_end_request();
     return ret_val;
 }

+ 1 - 1
kernel/driver/interrupt/apic/apic_timer.c

@@ -94,5 +94,5 @@ void apic_timer_init()
     io_mfence();
     irq_register(APIC_TIMER_IRQ_NUM, &apic_timer_ticks_result, &apic_timer_handler, 0, &apic_timer_intr_controller, "apic timer");
     io_mfence();
-    kinfo("Successfully initialized apic timer for cpu %d", proc_current_cpu_id);
+    // kinfo("Successfully initialized apic timer for cpu %d", proc_current_cpu_id);
 }

+ 156 - 2
kernel/driver/usb/usb.h

@@ -19,6 +19,19 @@
 #define USB_TIME_RST_NOMORE 3 // No more than this between resets for root hubs
 #define USB_TIME_RST_REC 10   // reset recovery
 
+/**
+ * @brief usb描述符的头部
+ *
+ * String Descriptor:
+ * String Language Descriptor:
+ *      先获取头部,然后根据长度申请空间,再获取整个string desc
+ */
+struct usb_desc_header
+{
+    uint8_t len; // 整个描述符的大小(字节)
+    uint8_t type;
+} __attribute__((packed));
+
 /**
  * @brief usb 设备描述符
  *
@@ -41,8 +54,85 @@ struct usb_device_desc
 
     uint8_t serial_index;
     uint8_t config; // number of configurations
+} __attribute__((packed));
+
+/**
+ * @brief usb设备配置信息描述符
+ *
+ */
+struct usb_config_desc
+{
+    uint8_t len;            // 当前描述符的大小(字节)
+    uint8_t type;           // USB_DT_CONFIG
+    uint16_t total_len;     /*
+                                Total length of data returned for this
+                                configuration. Includes the combined length
+                                of all descriptors (configuration, interface,
+                                endpoint, and class- or vendor-specific)
+                                returned for this configuration
+                            */
+    uint8_t num_interfaces; // 当前conf对应的接口数量
+    uint8_t value;          /*
+                                Value to use as an argument to the
+                                SetConfiguration() request to select this
+                                configuration
+                            */
+    uint8_t index;          // Index of string descriptor describing this configuration
+    uint8_t bmAttr;         /*
+                                Configuration characteristics:
+                                D7: Reserved (要设置为1)
+                                D6: Self-powered
+                                D5: Remote Wakeup
+                                D4...0: Reserved (设置为0)
+                            */
+    uint8_t max_power;      /*
+                                当这个设备满载时,为在这个conf上提供对应的功能,需要消耗的电流值。
+                                当设备是在High-speed时,这里的单位是2mA (也就是说,值为50,代表最大消耗100mA的电流)
+                                当设备运行在Gen X speed时,这里的单位是8mA
+                            */
+} __attribute__((packed));
+
+/**
+ * @brief usb接口描述符
+ *
+ */
+struct usb_interface_desc
+{
+    uint8_t len;
+    uint8_t type;                // USB_DT_INTERFACE
+    uint8_t interface_number;    // 当前接口序号(从0开始的)
+    uint8_t alternate_setting;   // used to select alt. setting
+    uint8_t num_endpoints;       // 当前interface的端点数量
+    uint8_t interface_class;     // Class code
+    uint8_t interface_sub_class; // Sub class code
+    uint8_t interface_protocol;  // 协议  These codes are qualified by the value of thebInterfaceClass and the bInterfaceSubClass fields.
+    uint8_t index;               // index of String Descriptor describing this interface
+} __attribute__((packed));
+
+/**
+ * @brief usb端点描述符
+ *
+ * 详见usb3.2 Specification Table 9-26
+ */
+struct usb_endpoint_desc
+{
+    uint8_t len;
+    uint8_t type;          // descriptor type
+    uint8_t endpoint_addr; /*  Bit 3...0: The endpoint number
+                               Bit 6...4: Reserved, reset to zero
+                               Bit 7: Direction, ignored for
+                               control endpoints
+                               0 = OUT endpoint
+                               1 = IN endpoint
+                               */
+    uint8_t attributes;
+    uint16_t max_packet;
+    uint8_t interval;
 };
 
+// 从endpoint描述符中获取max burst size大小
+#define usb_get_max_burst_from_ep(__ep_desc) (((__ep_desc)->max_packet & 0x1800) >> 11)
+
 /**
  * @brief usb设备请求包
  *
@@ -55,7 +145,7 @@ struct usb_request_packet_t
 
     uint16_t index;
     uint16_t length;
-};
+} __attribute__((packed));
 // usb设备请求包的request_type字段的值
 #define __USB_REQ_TYPE_H2D 0x00
 #define __USB_REQ_TYPE_D2H 0x80
@@ -73,6 +163,7 @@ struct usb_request_packet_t
 #define USB_REQ_TYPE_GET_REQUEST (__USB_REQ_TYPE_D2H | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_DEVICE)
 #define USB_REQ_TYPE_SET_REQUEST (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_DEVICE)
 #define USB_REQ_TYPE_SET_INTERFACE (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_INTERFACE)
+#define USB_REQ_TYPE_SET_CLASS_INTERFACE (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_CLASS | __USB_REQ_TYPE_INTERFACE)
 
 // device requests
 enum
@@ -90,6 +181,21 @@ enum
     USB_REQ_SET_INTERFACE,
     // standard endpoint requests
     USB_REQ_SYNCH_FRAME,
+    USB_REQ_SET_ENCRYPTION,
+    USB_REQ_GET_ENCRYPTION,
+    USB_REQ_SET_HANDSHAKE,
+    USB_REQ_GET_HANDSHAKE,
+    USB_REQ_SET_CONNECTION,
+    USB_REQ_SET_SECURITY_DATA,
+    USB_REQ_GET_SECURITY_DATA,
+    USB_REQ_SET_WUSB_DATA,
+    USB_REQ_LOOPBACK_DATA_WRITE,
+    USB_REQ_LOOPBACK_DATA_READ,
+    USB_REQ_SET_INTERFACE_DS,
+    USB_REQ_GET_FW_STATUS = 26,
+    USB_REQ_SET_FW_STATUS,
+    USB_REQ_SET_SEL = 48,
+    USB_REQ_SET_ISOCH_DELAY,
     // Device specific
     USB_REQ_GET_MAX_LUNS = 0xFE,
     USB_REQ_BULK_ONLY_RESET
@@ -109,6 +215,8 @@ enum
     USB_DT_OTG,
     USB_DT_DEBUG,
     USB_DT_INTERFACE_ASSOSIATION,
+    USB_DT_BOS = 15,
+    USB_DT_DEVICE_CAPABILITY,
 
     USB_DT_HID = 0x21,
     USB_DT_HID_REPORT,
@@ -117,7 +225,10 @@ enum
     USB_DT_INTERFACE_FUNCTION = 0x24,
     USB_DT_ENDPOINT_FUNCTION,
 
-    HUB = 0x29
+    // HUB = 0x29
+
+    USB_DT_SUPERSPEED_USB_ENDPOINT_COMPANION = 48,
+    USB_DT_SUPERSPEEDPLUS_ISOCHRONOUS_ENDPOINT_COMPANION,
 };
 
 // transfer types (Endpoint types) (USB 2.0 page 270)
@@ -129,6 +240,49 @@ enum
     USB_EP_INTERRUPT
 };
 
+/**
+ * @brief 该宏定义用于声明usb请求包,并初始化其中的各个字段
+ *
+ */
+#define DECLARE_USB_PACKET(pak_name, _trans_req_type, _trans_request, _trans_value, _trans_index, _transfer_length) \
+    struct usb_request_packet_t pak_name = {0};                                                                     \
+    pak_name.request_type = (_trans_req_type);                                                                      \
+    pak_name.request = (_trans_request);                                                                            \
+    pak_name.value = (_trans_value);                                                                                \
+    pak_name.index = (_trans_index);                                                                                \
+    pak_name.length = (_transfer_length);
+
+/*
+    usb class codes
+    refs: https://www.usb.org/defined-class-codes
+*/
+enum
+{
+    USB_CLASS_IF = 0x00,
+    USB_CLASS_AUDIO,
+    USB_CLASS_CDC,
+    USB_CLASS_HID,
+    USB_CLASS_PHYSICAL = 0x05,
+    USB_CLASS_IMAGE,
+    USB_CLASS_PRINTER,
+    USB_CLASS_MASS_STORAGE,
+    USB_CLASS_HUB,
+    USB_CLASS_CDC_DATA,
+    USB_CLASS_SMART_CARD,
+    USB_CLASS_CONTENT_SEC = 0x0d,
+    USB_CLASS_VIDEO,
+    USB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
+    USB_CLASS_AV,
+    USB_CLASS_BILLBOARD,
+    USB_CLASS_TYPEC_BRIDGE,
+    USB_CLASS_I3C = 0x3c,
+    USB_CLASS_DIAGNOSTIC = 0xdc,
+    USB_CLASS_WIRELESS_CTRL = 0xe0,
+    USB_CLASS_MISC = 0xef,
+    USB_CLASS_APP_SPEC = 0xfe,
+    USB_CLASS_VENDOR_SPEC = 0XFF,
+};
+
 /**
  * @brief 初始化usb驱动程序
  *

+ 87 - 0
kernel/driver/usb/xhci/internal.h

@@ -0,0 +1,87 @@
+#pragma once
+
+/*
+    注意!!!
+
+    尽管采用MMI/O的方式访问寄存器,但是对于指定大小的寄存器,
+    在发起读请求的时候,只能从寄存器的起始地址位置开始读取。
+
+    例子:不能在一个32bit的寄存器中的偏移量8的位置开始读取1个字节
+            这种情况下,我们必须从32bit的寄存器的0地址处开始读取32bit,然后通过移位的方式得到其中的字节。
+*/
+
+#define xhci_read_cap_reg32(id, offset) (__read4b(xhci_hc[id].vbase + (offset)))
+#define xhci_get_ptr_cap_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase + (offset)))
+#define xhci_write_cap_reg32(id, offset, value) (__write4b(xhci_hc[id].vbase + (offset), (value)))
+
+#define xhci_read_cap_reg64(id, offset) (__read8b(xhci_hc[id].vbase + (offset)))
+#define xhci_get_ptr_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase + (offset)))
+#define xhci_write_cap_reg64(id, offset, value) (__write8b(xhci_hc[id].vbase + (offset), (value)))
+
+#define xhci_read_op_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase_op + (offset)))
+#define xhci_get_ptr_op_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase_op + (offset)))
+#define xhci_write_op_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase_op + (offset)) = (uint8_t)(value))
+
+#define xhci_read_op_reg32(id, offset) (__read4b(xhci_hc[id].vbase_op + (offset)))
+#define xhci_get_ptr_op_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase_op + (offset)))
+#define xhci_write_op_reg32(id, offset, value) (__write4b(xhci_hc[id].vbase_op + (offset), (value)))
+
+#define xhci_read_op_reg64(id, offset) (__read8b(xhci_hc[id].vbase_op + (offset)))
+#define xhci_get_ptr_op_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase_op + (offset)))
+#define xhci_write_op_reg64(id, offset, value) (__write8b(xhci_hc[id].vbase_op + (offset), (value)))
+
+/**
+ * @brief 计算中断寄存器组虚拟地址
+ * @param id 主机控制器id
+ * @param num xhci中断寄存器组号
+ */
+#define xhci_calc_intr_vaddr(id, num) (xhci_hc[id].vbase + xhci_hc[id].rts_offset + XHCI_RT_IR0 + (num)*XHCI_IR_SIZE)
+/**
+ * @brief 读取/写入中断寄存器
+ * @param id 主机控制器id
+ * @param num xhci中断寄存器组号
+ * @param intr_offset 寄存器在当前寄存器组中的偏移量
+ */
+#define xhci_read_intr_reg32(id, num, intr_offset) (__read4b(xhci_calc_intr_vaddr(id, num) + (intr_offset)))
+#define xhci_write_intr_reg32(id, num, intr_offset, value) (__write4b(xhci_calc_intr_vaddr(id, num) + (intr_offset), (value)))
+#define xhci_read_intr_reg64(id, num, intr_offset) (__read8b(xhci_calc_intr_vaddr(id, num) + (intr_offset)))
+#define xhci_write_intr_reg64(id, num, intr_offset, value) (__write8b(xhci_calc_intr_vaddr(id, num) + (intr_offset), (value)))
+
+#define xhci_is_aligned64(addr) (((addr)&0x3f) == 0) // 是否64bytes对齐
+
+/**
+ * @brief 判断端口信息
+ * @param cid 主机控制器id
+ * @param pid 端口id
+ */
+#define XHCI_PORT_IS_USB2(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB2)
+#define XHCI_PORT_IS_USB3(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB3)
+
+#define XHCI_PORT_IS_USB2_HSO(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HSO) == XHCI_PROTOCOL_HSO)
+#define XHCI_PORT_HAS_PAIR(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HAS_PAIR) == XHCI_PROTOCOL_HAS_PAIR)
+#define XHCI_PORT_IS_ACTIVE(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_ACTIVE) == XHCI_PROTOCOL_ACTIVE)
+
+#define XHCI_PORT_REGISTER_OFFSET(__port_id) (XHCI_OPS_PRS + 16 * (__port_id))
+
+// 获取端口速度 full=1, low=2, high=3, super=4
+#define xhci_get_port_speed(__id, __port_id) ((xhci_read_op_reg32((__id), XHCI_PORT_REGISTER_OFFSET(__port_id) + XHCI_PORT_PORTSC) >> 10) & 0xf)
+
+/**
+ * @brief 设置link TRB的命令(dword3)
+ *
+ */
+#define xhci_TRB_set_link_cmd(trb_vaddr)                                         \
+    do                                                                           \
+    {                                                                            \
+        struct xhci_TRB_normal_t *ptr = (struct xhci_TRB_normal_t *)(trb_vaddr); \
+        ptr->TRB_type = TRB_TYPE_LINK;                                           \
+        ptr->ioc = 0;                                                            \
+        ptr->chain = 0;                                                          \
+        ptr->ent = 0;                                                            \
+        ptr->cycle = 1;                                                          \
+    } while (0)
+
+// 设置endpoint结构体的dequeue_cycle_state bit
+#define xhci_ep_set_dequeue_cycle_state(ep_ctx_ptr, state) ((ep_ctx_ptr)->tr_dequeue_ptr |= ((state)&1))
+// 获取endpoint结构体的dequeue_cycle_state bit
+#define xhci_ep_get_dequeue_cycle_state(ep_ctx_ptr) (((ep_ctx_ptr)->tr_dequeue_ptr) & 1)

+ 483 - 167
kernel/driver/usb/xhci/xhci.c

@@ -1,4 +1,5 @@
 #include "xhci.h"
+#include "internal.h"
 #include <common/kprint.h>
 #include <debug/bug.h>
 #include <common/spinlock.h>
@@ -38,14 +39,23 @@ static int xhci_hc_init_intr(int id);
 static int xhci_hc_start_ports(int id);
 
 static int xhci_send_command(int id, struct xhci_TRB_t *trb, const bool do_ring);
-static uint64_t xhci_initialize_slot(const int id, const int slot_id, const int port, const int speed, const int max_packet);
-static void xhci_initialize_ep(const int id, const uint64_t slot_vaddr, const int slot_id, const int ep_num, const int max_packet, const int type, const int direction, const int speed, const int ep_interval);
+static uint64_t xhci_initialize_slot(const int id, const int port, const int speed, const int max_packet);
+static void xhci_initialize_ep(const int id, const uint64_t slot_vaddr, const int port_id, const int ep_num, const int max_packet, const int max_burst, const int type, const int direction, const int speed, const int ep_interval);
 static int xhci_set_address(const int id, const uint64_t slot_vaddr, const int slot_id, const bool block);
-static int xhci_control_in(const int id, void *target, const int in_size, const int slot_id, const int max_packet);
-static int xhci_setup_stage(struct xhci_ep_ring_info_t *ep, const struct usb_request_packet_t *packet, const uint8_t direction);
-static int xhci_data_stage(struct xhci_ep_ring_info_t *ep, uint64_t buf_vaddr, uint8_t trb_type, const uint32_t size, uint8_t direction, const int max_packet, const uint64_t status_vaddr);
-static int xhci_status_stage(const int id, uint8_t direction, uint64_t status_buf_vaddr);
+static int xhci_control_in(const int id, struct usb_request_packet_t *packet, void *target, const int port_id, const int max_packet);
+static int xhci_control_out(const int id, struct usb_request_packet_t *packet, void *target, const int slot_id, const int max_packet);
+static int xhci_setup_stage(struct xhci_ep_info_t *ep, const struct usb_request_packet_t *packet, const uint8_t direction);
+static int xhci_data_stage(struct xhci_ep_info_t *ep, uint64_t buf_vaddr, uint8_t trb_type, const uint32_t size, uint8_t direction, const int max_packet, const uint64_t status_vaddr);
+static int xhci_status_stage(struct xhci_ep_info_t *ep, uint8_t direction, uint64_t status_buf_vaddr);
 static int xhci_wait_for_interrupt(const int id, uint64_t status_vaddr);
+static inline int xhci_get_desc(const int id, const int port_id, void *target, const uint16_t desc_type, const uint8_t desc_index, const uint16_t lang_id, const uint16_t length);
+static int xhci_get_config_desc(const int id, const int port_id, struct usb_config_desc *conf_desc);
+static inline int xhci_get_config_desc_full(const int id, const int port_id, const struct usb_config_desc *conf_desc, void *target);
+static int xhci_get_interface_desc(const void *in_buf, const uint8_t if_num, struct usb_interface_desc **if_desc);
+static inline int xhci_get_endpoint_desc(const struct usb_interface_desc *if_desc, const uint8_t ep_num, struct usb_endpoint_desc **ep_desc);
+static int xhci_get_descriptor(const int id, const int port_id, struct usb_device_desc *dev_desc);
+static int xhci_configure_port(const int id, const int port_id);
+static int xhci_configure_endpoint(const int id, const int port_id, const uint8_t ep_num, const uint8_t ep_type, struct usb_endpoint_desc *ep_desc);
 
 hardware_intr_controller xhci_hc_intr_controller =
     {
@@ -56,87 +66,6 @@ hardware_intr_controller xhci_hc_intr_controller =
         .ack = apic_local_apic_edge_ack,
 };
 
-/*
-    注意!!!
-
-    尽管采用MMI/O的方式访问寄存器,但是对于指定大小的寄存器,
-    在发起读请求的时候,只能从寄存器的起始地址位置开始读取。
-
-    例子:不能在一个32bit的寄存器中的偏移量8的位置开始读取1个字节
-            这种情况下,我们必须从32bit的寄存器的0地址处开始读取32bit,然后通过移位的方式得到其中的字节。
-*/
-
-#define xhci_read_cap_reg32(id, offset) (__read4b(xhci_hc[id].vbase + (offset)))
-#define xhci_get_ptr_cap_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase + (offset)))
-#define xhci_write_cap_reg32(id, offset, value) (__write4b(xhci_hc[id].vbase + (offset), (value)))
-
-#define xhci_read_cap_reg64(id, offset) (__read8b(xhci_hc[id].vbase + (offset)))
-#define xhci_get_ptr_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase + (offset)))
-#define xhci_write_cap_reg64(id, offset, value) (__write8b(xhci_hc[id].vbase + (offset), (value)))
-
-#define xhci_read_op_reg8(id, offset) (*(uint8_t *)(xhci_hc[id].vbase_op + (offset)))
-#define xhci_get_ptr_op_reg8(id, offset) ((uint8_t *)(xhci_hc[id].vbase_op + (offset)))
-#define xhci_write_op_reg8(id, offset, value) (*(uint8_t *)(xhci_hc[id].vbase_op + (offset)) = (uint8_t)(value))
-
-#define xhci_read_op_reg32(id, offset) (__read4b(xhci_hc[id].vbase_op + (offset)))
-#define xhci_get_ptr_op_reg32(id, offset) ((uint32_t *)(xhci_hc[id].vbase_op + (offset)))
-#define xhci_write_op_reg32(id, offset, value) (__write4b(xhci_hc[id].vbase_op + (offset), (value)))
-
-#define xhci_read_op_reg64(id, offset) (__read8b(xhci_hc[id].vbase_op + (offset)))
-#define xhci_get_ptr_op_reg64(id, offset) ((uint64_t *)(xhci_hc[id].vbase_op + (offset)))
-#define xhci_write_op_reg64(id, offset, value) (__write8b(xhci_hc[id].vbase_op + (offset), (value)))
-
-/**
- * @brief 计算中断寄存器组虚拟地址
- * @param id 主机控制器id
- * @param num xhci中断寄存器组号
- */
-#define xhci_calc_intr_vaddr(id, num) (xhci_hc[id].vbase + xhci_hc[id].rts_offset + XHCI_RT_IR0 + (num)*XHCI_IR_SIZE)
-/**
- * @brief 读取/写入中断寄存器
- * @param id 主机控制器id
- * @param num xhci中断寄存器组号
- * @param intr_offset 寄存器在当前寄存器组中的偏移量
- */
-#define xhci_read_intr_reg32(id, num, intr_offset) (__read4b(xhci_calc_intr_vaddr(id, num) + (intr_offset)))
-#define xhci_write_intr_reg32(id, num, intr_offset, value) (__write4b(xhci_calc_intr_vaddr(id, num) + (intr_offset), (value)))
-#define xhci_read_intr_reg64(id, num, intr_offset) (__read8b(xhci_calc_intr_vaddr(id, num) + (intr_offset)))
-#define xhci_write_intr_reg64(id, num, intr_offset, value) (__write8b(xhci_calc_intr_vaddr(id, num) + (intr_offset), (value)))
-
-#define xhci_is_aligned64(addr) (((addr)&0x3f) == 0) // 是否64bytes对齐
-
-/**
- * @brief 判断端口信息
- * @param cid 主机控制器id
- * @param pid 端口id
- */
-#define XHCI_PORT_IS_USB2(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB2)
-#define XHCI_PORT_IS_USB3(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_INFO) == XHCI_PROTOCOL_USB3)
-
-#define XHCI_PORT_IS_USB2_HSO(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HSO) == XHCI_PROTOCOL_HSO)
-#define XHCI_PORT_HAS_PAIR(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_HAS_PAIR) == XHCI_PROTOCOL_HAS_PAIR)
-#define XHCI_PORT_IS_ACTIVE(cid, pid) ((xhci_hc[cid].ports[pid].flags & XHCI_PROTOCOL_ACTIVE) == XHCI_PROTOCOL_ACTIVE)
-
-/**
- * @brief 设置link TRB的命令(dword3)
- *
- */
-#define xhci_TRB_set_link_cmd(trb_vaddr)                                         \
-    do                                                                           \
-    {                                                                            \
-        struct xhci_TRB_normal_t *ptr = (struct xhci_TRB_normal_t *)(trb_vaddr); \
-        ptr->TRB_type = TRB_TYPE_LINK;                                           \
-        ptr->ioc = 0;                                                            \
-        ptr->chain = 0;                                                          \
-        ptr->ent = 0;                                                            \
-        ptr->cycle = 1;                                                          \
-    } while (0)
-
-// 设置endpoint结构体的dequeue_cycle_state bit
-#define xhci_ep_set_dequeue_cycle_state(ep_ctx_ptr, state) ((ep_ctx_ptr)->tr_dequeue_ptr |= ((state)&1))
-// 获取endpoint结构体的dequeue_cycle_state bit
-#define xhci_ep_get_dequeue_cycle_state(ep_ctx_ptr) (((ep_ctx_ptr)->tr_dequeue_ptr) & 1)
-
 /**
  * @brief 在controller数组之中寻找可用插槽
  *
@@ -252,7 +181,7 @@ static __always_inline void __xhci_write_doorbell(const int id, const uint16_t s
  * @param ep_info 端点信息结构体
  * @param trb 待写入的trb
  */
-static __always_inline void __xhci_write_trb(struct xhci_ep_ring_info_t *ep_info, struct xhci_TRB_t *trb)
+static __always_inline void __xhci_write_trb(struct xhci_ep_info_t *ep_info, struct xhci_TRB_t *trb)
 {
     memcpy((void *)ep_info->current_ep_ring_vaddr, trb, sizeof(struct xhci_TRB_t));
 
@@ -269,6 +198,18 @@ static __always_inline void __xhci_write_trb(struct xhci_ep_ring_info_t *ep_info
     }
 }
 
+/**
+ * @brief 获取设备上下文缓冲区的虚拟地址
+ *
+ * @param id 主机控制器id
+ * @param port_id 端口id
+ * @return 设备上下文缓冲区的虚拟地址
+ */
+static __always_inline uint64_t xhci_get_device_context_vaddr(const int id, const int port_id)
+{
+    return phys_2_virt(__read8b(xhci_hc[id].dcbaap_vaddr + (xhci_hc[id].ports[port_id].slot_id * sizeof(uint64_t))));
+}
+
 /**
  * @brief 停止xhci主机控制器
  *
@@ -715,6 +656,7 @@ void xhci_hc_irq_handler(uint64_t irq_num, uint64_t cid, struct pt_regs *regs)
 
     if (((iman0 & 3) == 3) || (dequeue_reg & 8)) // 中断被启用,且pending不为0
     {
+        // kdebug("to handle");
         // 写入1以清除该interrupter的pending bit
         xhci_write_intr_reg32(cid, 0, XHCI_IR_MAN, iman0 | 3);
         io_mfence();
@@ -723,10 +665,16 @@ void xhci_hc_irq_handler(uint64_t irq_num, uint64_t cid, struct pt_regs *regs)
         // 暂存当前trb的起始地址
         uint64_t last_event_ring_vaddr = xhci_hc[cid].current_event_ring_vaddr;
         xhci_get_trb(&event_trb, xhci_hc[cid].current_event_ring_vaddr);
+
+        {
+            struct xhci_TRB_cmd_complete_t *event_trb_ptr = (struct xhci_TRB_cmd_complete_t *)&event_trb;
+            // kdebug("TRB_type=%d, comp_code=%d", event_trb_ptr->TRB_type, event_trb_ptr->code);
+        }
         while ((event_trb.command & 1) == xhci_hc[cid].current_event_ring_cycle) // 循环处理处于当前周期的所有event ring
         {
 
             struct xhci_TRB_cmd_complete_t *event_trb_ptr = (struct xhci_TRB_cmd_complete_t *)&event_trb;
+            // kdebug("TRB_type=%d, comp_code=%d", event_trb_ptr->TRB_type, event_trb_ptr->code);
             if ((event_trb.command & (1 << 2)) == 0) // 当前event trb不是由于short packet产生的
             {
                 // kdebug("event_trb_ptr->code=%d", event_trb_ptr->code);
@@ -851,7 +799,7 @@ static int xhci_reset_port(const int id, const int port)
         io_mfence();
         if (val & (1 << 21))
             break;
-        // QEMU对usb的模拟有bug,因此需要检测这里
+            // QEMU对usb的模拟有bug,因此需要检测这里
 #ifdef __QEMU_EMULATION__
 
         if (XHCI_PORT_IS_USB3(id, port) && (val & (1 << 31)) == 0)
@@ -911,22 +859,19 @@ static int xhci_reset_port(const int id, const int port)
  *   set the slot->hub, ->mtt, ->ttt, ->etc, items.
  *
  * @param id 控制器id
- * @param slot_id enable_slot命令分配的插槽id
  * @param port 端口号
  * @param speed 端口速度
  * @param max_packet 最大数据包大小
  * @return uint64_t 初始化好的设备上下文空间的虚拟地址
  */
-static uint64_t xhci_initialize_slot(const int id, const int slot_id, const int port, const int speed, const int max_packet)
+static uint64_t xhci_initialize_slot(const int id, const int port, const int speed, const int max_packet)
 {
-    // 暂时只初始化slot和control EP0
-
-    // 申请上下文数据结构所占用的内存空间
-
-    uint64_t device_context_vaddr = (uint64_t)kzalloc(xhci_hc[id].context_size * 2, 0);
+    // 为所有的endpoint分配上下文空间
+    // todo: 按需分配上下文空间
+    uint64_t device_context_vaddr = (uint64_t)kzalloc(xhci_hc[id].context_size * 32, 0);
     // kdebug("slot id=%d, device_context_vaddr=%#018lx, port=%d", slot_id, device_context_vaddr, port);
     // 写到数组中
-    __write8b(xhci_hc[id].dcbaap_vaddr + (slot_id * sizeof(uint64_t)), virt_2_phys(device_context_vaddr));
+    __write8b(xhci_hc[id].dcbaap_vaddr + (xhci_hc[id].ports[port].slot_id * sizeof(uint64_t)), virt_2_phys(device_context_vaddr));
     struct xhci_slot_context_t slot_ctx = {0};
     slot_ctx.entries = 1;
     slot_ctx.speed = speed;
@@ -940,7 +885,8 @@ static uint64_t xhci_initialize_slot(const int id, const int slot_id, const int
     // 将slot信息写入上下文空间
     __write_slot(device_context_vaddr, &slot_ctx);
 
-    xhci_initialize_ep(id, device_context_vaddr, slot_id, XHCI_EP_CONTROL, max_packet, USB_EP_CONTROL, 0, speed, 0);
+    // 初始化控制端点
+    xhci_initialize_ep(id, device_context_vaddr, port, XHCI_EP_CONTROL, max_packet, 0, USB_EP_CONTROL, 0, speed, 0);
 
     return device_context_vaddr;
 }
@@ -950,7 +896,7 @@ static uint64_t xhci_initialize_slot(const int id, const int slot_id, const int
  *
  * @param id 控制器id
  * @param slot_vaddr slot上下文的虚拟地址
- * @param slot_id 插槽id
+ * @param port_id 插槽id
  * @param ep_num 端点上下文在slot上下文区域内的编号
  * @param max_packet 最大数据包大小
  * @param type 端点类型
@@ -958,36 +904,59 @@ static uint64_t xhci_initialize_slot(const int id, const int slot_id, const int
  * @param speed 传输速度
  * @param ep_interval 端点的连续请求间隔
  */
-static void xhci_initialize_ep(const int id, const uint64_t slot_vaddr, const int slot_id, const int ep_num, const int max_packet, const int type, const int direction, const int speed, const int ep_interval)
+static void xhci_initialize_ep(const int id, const uint64_t slot_vaddr, const int port_id, const int ep_num, const int max_packet, const int max_burst, const int type, const int direction, const int speed, const int ep_interval)
 {
     // 由于目前只实现获取设备的描述符,因此暂时只支持control ep
-    if (type != USB_EP_CONTROL)
+    if (type != USB_EP_CONTROL && type != USB_EP_INTERRUPT)
         return;
     struct xhci_ep_context_t ep_ctx = {0};
     memset(&ep_ctx, 0, sizeof(struct xhci_ep_context_t));
 
-    xhci_hc[id].control_ep_info.ep_ring_vbase = xhci_create_ring(XHCI_TRBS_PER_RING);
+    xhci_hc[id].ports[port_id].ep_info[ep_num].ep_ring_vbase = xhci_create_ring(XHCI_TRBS_PER_RING);
     // 申请ep的 transfer ring
-    ep_ctx.tr_dequeue_ptr = virt_2_phys(xhci_hc[id].control_ep_info.ep_ring_vbase);
+    ep_ctx.tr_dequeue_ptr = virt_2_phys(xhci_hc[id].ports[port_id].ep_info[ep_num].ep_ring_vbase);
     xhci_ep_set_dequeue_cycle_state(&ep_ctx, XHCI_TRB_CYCLE_ON);
 
-    xhci_hc[id].control_ep_info.current_ep_ring_vaddr = xhci_hc[id].control_ep_info.ep_ring_vbase;
-    xhci_hc[id].control_ep_info.current_ep_ring_cycle = xhci_ep_get_dequeue_cycle_state(&ep_ctx);
+    xhci_hc[id].ports[port_id].ep_info[ep_num].current_ep_ring_vaddr = xhci_hc[id].ports[port_id].ep_info[ep_num].ep_ring_vbase;
+    xhci_hc[id].ports[port_id].ep_info[ep_num].current_ep_ring_cycle = xhci_ep_get_dequeue_cycle_state(&ep_ctx);
     // kdebug("ep_ctx.tr_dequeue_ptr = %#018lx", ep_ctx.tr_dequeue_ptr);
     // kdebug("xhci_hc[id].control_ep_info.current_ep_ring_cycle  = %d", xhci_hc[id].control_ep_info.current_ep_ring_cycle);
+    kdebug("max_packet=%d, max_burst=%d", max_packet, max_burst);
+    switch (type)
+    {
+    case USB_EP_CONTROL: // Control ep
+        // 设置初始值
+        ep_ctx.max_packet_size = max_packet;
+        ep_ctx.linear_stream_array = 0;
+        ep_ctx.max_primary_streams = 0;
+        ep_ctx.mult = 0;
+        ep_ctx.ep_state = XHCI_EP_STATE_DISABLED;
+        ep_ctx.hid = 0;
+        ep_ctx.ep_type = XHCI_EP_TYPE_CONTROL;
+        ep_ctx.average_trb_len = 8; // 所有的control ep的该值均为8
+        ep_ctx.err_cnt = 3;
+        ep_ctx.max_burst_size = max_burst;
+        ep_ctx.interval = ep_interval;
+
+        break;
+    case USB_EP_INTERRUPT:
+        ep_ctx.max_packet_size = max_packet & 0x7ff;
+        ep_ctx.max_burst_size = max_burst;
+        ep_ctx.ep_state = XHCI_EP_STATE_DISABLED;
+        ep_ctx.mult = 0;
+        ep_ctx.err_cnt = 3;
+        ep_ctx.max_esti_payload_hi = ((max_packet * (max_burst + 1)) >> 8) & 0xff;
+        ep_ctx.max_esti_payload_lo = ((max_packet * (max_burst + 1))) & 0xff;
+        ep_ctx.interval = ep_interval;
+        ep_ctx.average_trb_len = 8; // todo: It's not sure how much to fill in this value
+        // ep_ctx.ep_type = XHCI_EP_TYPE_INTR_IN;
+        ep_ctx.ep_type = ((ep_num % 2) ? XHCI_EP_TYPE_INTR_IN : XHCI_EP_TYPE_INTR_OUT);
+
+        break;
+    default:
+        break;
+    }
 
-    // 设置初始值
-    ep_ctx.max_packet_size = max_packet;
-    ep_ctx.linear_stream_array = 0;
-    ep_ctx.max_primary_streams = 0;
-    ep_ctx.mult = 0;
-    ep_ctx.ep_state = XHCI_EP_STATE_DISABLED;
-    ep_ctx.hid = 0;
-    ep_ctx.ep_type = 4;
-    ep_ctx.average_trb_len = 8; // 所有的control ep的该值均为8
-    ep_ctx.err_cnt = 3;
-    ep_ctx.max_burst_size = 0;
-    ep_ctx.interval = ep_interval;
     // 将ep的信息写入到slot上下文中对应的ep的块中
     __write_ep(id, slot_vaddr, ep_num, &ep_ctx);
 }
@@ -1007,7 +976,7 @@ static int xhci_set_address(const int id, const uint64_t slot_vaddr, const int s
     struct xhci_slot_context_t slot;
     struct xhci_ep_context_t ep;
     // 创建输入上下文缓冲区
-    uint64_t input_ctx_buffer = (uint64_t)kzalloc(xhci_hc[id].context_size * 32, 0);
+    uint64_t input_ctx_buffer = (uint64_t)kzalloc(xhci_hc[id].context_size * 33, 0);
 
     // 置位input control context和slot context的add bit
     __write4b(input_ctx_buffer + 4, 0x3);
@@ -1063,7 +1032,7 @@ failed:;
  * @param direction 传输的方向
  * @return int 产生的TRB数量
  */
-static int xhci_setup_stage(struct xhci_ep_ring_info_t *ep, const struct usb_request_packet_t *packet, const uint8_t direction)
+static int xhci_setup_stage(struct xhci_ep_info_t *ep, const struct usb_request_packet_t *packet, const uint8_t direction)
 {
     // kdebug("ep->current_ep_ring_cycle=%d", ep->current_ep_ring_cycle);
     struct xhci_TRB_setup_stage_t trb = {0};
@@ -1097,7 +1066,7 @@ static int xhci_setup_stage(struct xhci_ep_ring_info_t *ep, const struct usb_req
  * @param status_vaddr event data TRB的缓冲区(4字节,且地址按照16字节对齐)
  * @return int 产生的TRB数量
  */
-static int xhci_data_stage(struct xhci_ep_ring_info_t *ep, uint64_t buf_vaddr, uint8_t trb_type, const uint32_t size, uint8_t direction, const int max_packet, const uint64_t status_vaddr)
+static int xhci_data_stage(struct xhci_ep_info_t *ep, uint64_t buf_vaddr, uint8_t trb_type, const uint32_t size, uint8_t direction, const int max_packet, const uint64_t status_vaddr)
 {
     if (size == 0)
         return 0;
@@ -1148,25 +1117,26 @@ static int xhci_data_stage(struct xhci_ep_ring_info_t *ep, uint64_t buf_vaddr, u
 /**
  * @brief 填写xhci status stage TRB到control ep的transfer ring
  *
- * @param id 主机控制器id
+ * @param ep 端点信息结构体
  * @param direction 方向:(h2d:0, d2h:1)
  * @param status_buf_vaddr
  * @return int 创建的TRB数量
  */
-static int xhci_status_stage(const int id, uint8_t direction, uint64_t status_buf_vaddr)
+static int xhci_status_stage(struct xhci_ep_info_t *ep, uint8_t direction, uint64_t status_buf_vaddr)
 {
     // kdebug("write status stage trb");
+
     {
         struct xhci_TRB_status_stage_t trb = {0};
 
         // 写入status stage trb
         trb.intr_target = 0;
-        trb.cycle = xhci_hc[id].control_ep_info.current_ep_ring_cycle;
+        trb.cycle = ep->current_ep_ring_cycle;
         trb.ent = 0;
         trb.ioc = 1;
         trb.TRB_type = TRB_TYPE_STATUS_STAGE;
         trb.dir = direction;
-        __xhci_write_trb(&xhci_hc[id].control_ep_info, (struct xhci_TRB_t *)&trb);
+        __xhci_write_trb(ep, (struct xhci_TRB_t *)&trb);
     }
 
     {
@@ -1177,10 +1147,11 @@ static int xhci_status_stage(const int id, uint8_t direction, uint64_t status_bu
         trb.TRB_type = TRB_TYPE_EVENT_DATA;
         trb.ioc = 1;
 
-        trb.cycle = xhci_hc[id].control_ep_info.current_ep_ring_cycle;
+        trb.cycle = ep->current_ep_ring_cycle;
 
-        __xhci_write_trb(&xhci_hc[id].control_ep_info, (struct xhci_TRB_t *)&trb);
+        __xhci_write_trb(ep, (struct xhci_TRB_t *)&trb);
     }
+
     return 2;
 }
 
@@ -1227,27 +1198,26 @@ static int xhci_wait_for_interrupt(const int id, uint64_t status_vaddr)
  * @brief 从指定插槽的control endpoint读取信息
  *
  * @param id 主机控制器id
+ * @param packet usb数据包
  * @param target 读取到的信息存放到的位置
- * @param in_size 要读取的数据的大小
- * @param slot_id 插槽id
+ * @param port_id 端口id
  * @param max_packet 最大数据包大小
  * @return int 读取到的数据的大小
  */
-static int xhci_control_in(const int id, void *target, const int in_size, const int slot_id, const int max_packet)
+static int xhci_control_in(const int id, struct usb_request_packet_t *packet, void *target, const int port_id, const int max_packet)
 {
 
     uint64_t status_buf_vaddr = (uint64_t)kzalloc(16, 0); // 本来是要申请4bytes的buffer的,但是因为xhci控制器需要16bytes对齐,因此申请16bytes
-    uint64_t data_buf_vaddr = (uint64_t)kzalloc(256, 0);
+    uint64_t data_buf_vaddr = 0;
     int retval = 0;
-    struct usb_request_packet_t packet = {0};
-    packet.request_type = USB_REQ_TYPE_GET_REQUEST;
-    packet.request = USB_REQ_GET_DESCRIPTOR;
-    packet.value = (USB_DT_DEVICE << 8);
-    packet.length = in_size;
 
     // 往control ep写入一个setup stage trb
-    xhci_setup_stage(&xhci_hc[id].control_ep_info, &packet, XHCI_DIR_IN);
-    xhci_data_stage(&xhci_hc[id].control_ep_info, data_buf_vaddr, TRB_TYPE_DATA_STAGE, in_size, XHCI_DIR_IN_BIT, max_packet, status_buf_vaddr);
+    xhci_setup_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], packet, XHCI_DIR_IN);
+    if (packet->length)
+    {
+        data_buf_vaddr = (uint64_t)kzalloc(packet->length, 0);
+        xhci_data_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], data_buf_vaddr, TRB_TYPE_DATA_STAGE, packet->length, XHCI_DIR_IN_BIT, max_packet, status_buf_vaddr);
+    }
 
 /*
     QEMU doesn't quite handle SETUP/DATA/STATUS transactions correctly.
@@ -1263,15 +1233,15 @@ static int xhci_control_in(const int id, void *target, const int in_size, const
 #ifndef __QEMU_EMULATION__
     // 如果不是qemu虚拟机,则可以直接发起传输
     // kdebug(" not qemu");
-    __xhci_write_doorbell(id, slot_id, XHCI_EP_CONTROL);
+    __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
     retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
     if (unlikely(retval != 0))
         goto failed;
 #endif
     memset((void *)status_buf_vaddr, 0, 16);
-    xhci_status_stage(id, XHCI_DIR_OUT_BIT, status_buf_vaddr);
+    xhci_status_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], XHCI_DIR_OUT_BIT, status_buf_vaddr);
 
-    __xhci_write_doorbell(id, slot_id, XHCI_EP_CONTROL);
+    __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
 
     retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
 
@@ -1279,8 +1249,9 @@ static int xhci_control_in(const int id, void *target, const int in_size, const
         goto failed;
 
     // 将读取到的数据拷贝到目标区域
-    memcpy(target, (void *)data_buf_vaddr, in_size);
-    retval = in_size;
+    if (packet->length)
+        memcpy(target, (void *)data_buf_vaddr, packet->length);
+    retval = packet->length;
     goto done;
 
 failed:;
@@ -1289,31 +1260,217 @@ failed:;
 done:;
     // 释放内存
     kfree((void *)status_buf_vaddr);
-    kfree((void *)data_buf_vaddr);
+    if (packet->length)
+        kfree((void *)data_buf_vaddr);
     return retval;
 }
 
 /**
- * @brief 获取端口的描述信息
+ * @brief 向指定插槽的control ep输出信息
  *
  * @param id 主机控制器id
+ * @param packet usb数据包
+ * @param target 返回的数据存放的位置
  * @param port_id 端口id
- * @return int 错误码
+ * @param max_packet 最大数据包大小
+ * @return int 读取到的数据的大小
  */
-static int xhci_get_descriptor(const int id, const int port_id)
+static int xhci_control_out(const int id, struct usb_request_packet_t *packet, void *target, const int port_id, const int max_packet)
 {
+    uint64_t status_buf_vaddr = (uint64_t)kzalloc(16, 0);
+    uint64_t data_buf_vaddr = 0;
     int retval = 0;
-    int count = 0;
-    struct usb_device_desc dev_desc = {0};
-    uint32_t dword;
-    // 计算port register set相对于operational registers基地址的偏移量
-    uint32_t port_register_offset = XHCI_OPS_PRS + 16 * port_id;
 
-    // 读取指定端口的port sc寄存器
-    dword = xhci_read_op_reg32(id, port_register_offset + XHCI_PORT_PORTSC);
+    // 往control ep写入一个setup stage trb
+    xhci_setup_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], packet, XHCI_DIR_OUT);
+
+    if (packet->length)
+    {
+        data_buf_vaddr = (uint64_t)kzalloc(packet->length, 0);
+        xhci_data_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], data_buf_vaddr, TRB_TYPE_DATA_STAGE, packet->length, XHCI_DIR_OUT_BIT, max_packet, status_buf_vaddr);
+    }
+
+#ifndef __QEMU_EMULATION__
+    // 如果不是qemu虚拟机,则可以直接发起传输
+    __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
+    retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
+    if (unlikely(retval != 0))
+        goto failed;
+#endif
+
+    memset((void *)status_buf_vaddr, 0, 16);
+    xhci_status_stage(&xhci_hc[id].ports[port_id].ep_info[XHCI_EP_CONTROL], XHCI_DIR_IN_BIT, status_buf_vaddr);
+
+    __xhci_write_doorbell(id, xhci_hc[id].ports[port_id].slot_id, XHCI_EP_CONTROL);
+#ifndef __QEMU_EMULATION__
+    // qemu对于这个操作的处理有问题,status_buf并不会被修改。而真机不存在该问题
+    retval = xhci_wait_for_interrupt(id, status_buf_vaddr);
+#endif
+
+    if (unlikely(retval != 0))
+        goto failed;
+
+    // 将读取到的数据拷贝到目标区域
+    if (packet->length)
+        memcpy(target, (void *)data_buf_vaddr, packet->length);
+    retval = packet->length;
+    goto done;
+failed:;
+    kdebug("wait 4 interrupt failed");
+    retval = 0;
+done:;
+    // 释放内存
+    kfree((void *)status_buf_vaddr);
+    if (packet->length)
+        kfree((void *)data_buf_vaddr);
+    return retval;
+}
+
+/**
+ * @brief 获取描述符
+ *
+ * @param id 控制器号
+ * @param port_id 端口号
+ * @param target 获取到的数据要拷贝到的地址
+ * @param desc_type 描述符类型
+ * @param desc_index 描述符的索引号
+ * @param lang_id 语言id(默认为0)
+ * @param length 要传输的数据长度
+ * @return int 错误码
+ */
+static inline int xhci_get_desc(const int id, const int port_id, void *target, const uint16_t desc_type, const uint8_t desc_index, const uint16_t lang_id, const uint16_t length)
+{
+    struct usb_device_desc *dev_desc = xhci_hc[id].ports[port_id].dev_desc;
+    int count;
+
+    BUG_ON(dev_desc == NULL);
+    // 设备端口没有对应的描述符
+    if (unlikely(dev_desc == NULL))
+        return -EINVAL;
+    DECLARE_USB_PACKET(ctrl_in_packet, USB_REQ_TYPE_GET_REQUEST, USB_REQ_GET_DESCRIPTOR, (desc_type << 8) | desc_index, lang_id, length);
+    count = xhci_control_in(id, &ctrl_in_packet, target, port_id, dev_desc->max_packet_size);
+    if (unlikely(count == 0))
+        return -EAGAIN;
+    return 0;
+}
+
+static inline int xhci_set_configuration(const int id, const int port_id, const uint8_t conf_value)
+{
+    struct usb_device_desc *dev_desc = xhci_hc[id].ports[port_id].dev_desc;
+    int count;
+
+    BUG_ON(dev_desc == NULL);
+    // 设备端口没有对应的描述符
+    if (unlikely(dev_desc == NULL))
+        return -EINVAL;
+    DECLARE_USB_PACKET(ctrl_out_packet, USB_REQ_TYPE_SET_REQUEST, USB_REQ_SET_CONFIGURATION, conf_value & 0xff, 0, 0);
+    kdebug("set conf: to control out");
+    count = xhci_control_out(id, &ctrl_out_packet, NULL, port_id, dev_desc->max_packet_size);
+    kdebug("set conf: count=%d", count);
+    return 0;
+}
+
+/**
+ * @brief 获取usb 设备的config_desc
+ *
+ * @param id 主机控制器id
+ * @param port_id 端口id
+ * @param conf_desc 要获取的conf_desc
+ * @return int 错误码
+ */
+static int xhci_get_config_desc(const int id, const int port_id, struct usb_config_desc *conf_desc)
+{
+    if (unlikely(conf_desc == NULL))
+        return -EINVAL;
+
+    kdebug("to get conf for port %d", port_id);
+    int retval = xhci_get_desc(id, port_id, conf_desc, USB_DT_CONFIG, 0, 0, 9);
+    if (unlikely(retval != 0))
+        return retval;
+    kdebug("port %d got conf ok. type=%d, len=%d, total_len=%d, num_interfaces=%d, max_power=%dmA", port_id, conf_desc->type, conf_desc->len, conf_desc->total_len, conf_desc->num_interfaces, (xhci_get_port_speed(id, port_id) == XHCI_PORT_SPEED_SUPER) ? (conf_desc->max_power * 8) : (conf_desc->max_power * 2));
+    return 0;
+}
+
+/**
+ * @brief 获取完整的config desc(包含conf、interface、endpoint)
+ *
+ * @param id 控制器id
+ * @param port_id 端口id
+ * @param conf_desc 之前已经获取好的config_desc
+ * @param target 最终结果要拷贝到的地址
+ * @return int 错误码
+ */
+static inline int xhci_get_config_desc_full(const int id, const int port_id, const struct usb_config_desc *conf_desc, void *target)
+{
+    if (unlikely(conf_desc == NULL || target == NULL))
+        return -EINVAL;
+
+    return xhci_get_desc(id, port_id, target, USB_DT_CONFIG, 0, 0, conf_desc->total_len);
+}
+
+/**
+ * @brief 从完整的conf_desc数据中获取指定的interface_desc的指针
+ *
+ * @param in_buf 存储了完整的conf_desc的缓冲区
+ * @param if_num 接口号
+ * @param if_desc 返回的指向接口结构体的指针
+ * @return int 错误码
+ */
+static int xhci_get_interface_desc(const void *in_buf, const uint8_t if_num, struct usb_interface_desc **if_desc)
+{
+    if (unlikely(if_desc == NULL || in_buf == NULL))
+        return -EINVAL;
+    kdebug("to get interface.");
+    // 判断接口index是否合理
+    if (if_num >= ((struct usb_config_desc *)in_buf)->num_interfaces)
+        return -EINVAL;
+    struct usb_interface_desc *ptr = (struct usb_interface_desc *)(in_buf + sizeof(struct usb_config_desc));
+    for (int i = 0; i < if_num; ++i)
+    {
+        ptr = (struct usb_interface_desc *)(((uint64_t)ptr) + sizeof(struct usb_interface_desc) + sizeof(struct usb_endpoint_desc) * ptr->num_endpoints);
+    }
+    // 返回结果
+    *if_desc = ptr;
+
+    kdebug("get interface desc ok. interface_number=%d, num_endpoints=%d, class=%d, subclass=%d", ptr->interface_number, ptr->num_endpoints, ptr->interface_class, ptr->interface_sub_class);
+    return 0;
+}
+
+/**
+ * @brief 获取端点描述符
+ *
+ * @param if_desc 接口描述符
+ * @param ep_num 端点号
+ * @param ep_desc 返回的指向端点描述符的指针
+ * @return int 错误码
+ */
+static inline int xhci_get_endpoint_desc(const struct usb_interface_desc *if_desc, const uint8_t ep_num, struct usb_endpoint_desc **ep_desc)
+{
+    if (unlikely(if_desc == NULL || ep_desc == NULL))
+        return -EINVAL;
+    BUG_ON(ep_num >= if_desc->num_endpoints);
+
+    *ep_desc = (struct usb_endpoint_desc *)((uint64_t)(if_desc + 1) + ep_num * sizeof(struct usb_endpoint_desc));
+    kdebug("get endpoint desc: ep_addr=%d, max_packet=%d, attr=%#06x, interval=%d", (*ep_desc)->endpoint_addr, (*ep_desc)->max_packet, (*ep_desc)->attributes, (*ep_desc)->interval);
+    return 0;
+}
 
+/**
+ * @brief 初始化设备端口,并获取端口的描述信息
+ *
+ * @param id 主机控制器id
+ * @param port_id 端口id
+ * @param dev_desc 设备描述符
+ * @return int 错误码
+ */
+static int xhci_get_descriptor(const int id, const int port_id, struct usb_device_desc *dev_desc)
+{
+    int retval = 0;
+    int count = 0;
+    if (unlikely(dev_desc == NULL))
+        return -EINVAL;
     // 读取端口速度。 full=1, low=2, high=3, super=4
-    uint32_t speed = ((dword >> 10) & 0xf);
+    uint32_t speed = xhci_get_port_speed(id, port_id);
 
     /*
      * Some devices will only send the first 8 bytes of the device descriptor
@@ -1349,11 +1506,15 @@ static int xhci_get_descriptor(const int id, const int port_id)
             break;
         }
     }
+    else
+        return -EAGAIN; // slot id 不合法
 
+    xhci_hc[id].ports[port_id].slot_id = slot_id;
     // kdebug("speed=%d", speed);
     // 初始化接口的上下文
-    uint64_t slot_vaddr = xhci_initialize_slot(id, slot_id, port_id, speed, max_packet);
+    uint64_t slot_vaddr = xhci_initialize_slot(id, port_id, speed, max_packet);
 
+    retval = xhci_set_address(id, slot_vaddr, slot_id, true);
     // kdebug("set addr again");
     // 再次发送 set_address命令
     // kdebug("to set addr again");
@@ -1361,16 +1522,19 @@ static int xhci_get_descriptor(const int id, const int port_id)
     if (retval != 0)
         return retval;
 
-    // kdebug("ctrl in again");
-
-    count = xhci_control_in(id, &dev_desc, 18, slot_id, max_packet);
+    memset(dev_desc, 0, sizeof(struct usb_device_desc));
+    DECLARE_USB_PACKET(ctrl_in_packet, USB_REQ_TYPE_GET_REQUEST, USB_REQ_GET_DESCRIPTOR, (USB_DT_DEVICE << 8), 0, 18);
+    count = xhci_control_in(id, &ctrl_in_packet, dev_desc, port_id, max_packet);
     if (unlikely(count == 0))
         return -EAGAIN;
     /*
-        TODO: if the dev_desc.max_packet was different than what we have as max_packet,
+        TODO: if the dev_desc->max_packet was different than what we have as max_packet,
           you would need to change it here and in the slot context by doing a
           evaluate_slot_context call.
     */
+
+    xhci_hc[id].ports[port_id].dev_desc = dev_desc;
+
     // print the descriptor
     printk("  Found USB Device:\n"
            "                port: %i\n"
@@ -1388,12 +1552,11 @@ static int xhci_get_descriptor(const int id, const int port_id)
            "       product index: %i\n"
            "        serial index: %i\n"
            "   number of configs: %i\n",
-           port_id, dev_desc.len, dev_desc.type, dev_desc.usb_version >> 8, dev_desc.usb_version & 0xFF, dev_desc._class, dev_desc.subclass,
-           dev_desc.protocol, dev_desc.max_packet_size, dev_desc.vendor_id, dev_desc.product_id,
-           (dev_desc.device_rel & 0xF000) >> 12, (dev_desc.device_rel & 0x0F00) >> 8,
-           (dev_desc.device_rel & 0x00F0) >> 4, (dev_desc.device_rel & 0x000F) >> 0,
-           dev_desc.manufacturer_index, dev_desc.procuct_index, dev_desc.serial_index, dev_desc.config);
-
+           port_id, dev_desc->len, dev_desc->type, dev_desc->usb_version >> 8, dev_desc->usb_version & 0xFF, dev_desc->_class, dev_desc->subclass,
+           dev_desc->protocol, dev_desc->max_packet_size, dev_desc->vendor_id, dev_desc->product_id,
+           (dev_desc->device_rel & 0xF000) >> 12, (dev_desc->device_rel & 0x0F00) >> 8,
+           (dev_desc->device_rel & 0x00F0) >> 4, (dev_desc->device_rel & 0x000F) >> 0,
+           dev_desc->manufacturer_index, dev_desc->procuct_index, dev_desc->serial_index, dev_desc->config);
     return 0;
 }
 
@@ -1422,8 +1585,12 @@ static int xhci_hc_start_ports(int id)
                                       // 否则,reset函数会把它给设置为未激活,并且标志配对的usb2端口是激活的
             {
                 // kdebug("reset port %d ok", id);
-                if (xhci_get_descriptor(id, i) == 0)
+                struct usb_device_desc dev_desc = {0};
+                if (xhci_get_descriptor(id, i, &dev_desc) == 0)
+                {
+                    xhci_configure_port(id, i);
                     ++cnt;
+                }
                 kdebug("usb3 port %d get desc ok", i);
             }
         }
@@ -1443,8 +1610,12 @@ static int xhci_hc_start_ports(int id)
             {
                 // kdebug("reset port %d ok", id);
 
-                if (xhci_get_descriptor(id, i) == 0)
+                struct usb_device_desc dev_desc = {0};
+                if (xhci_get_descriptor(id, i, &dev_desc) == 0)
+                {
+                    xhci_configure_port(id, i);
                     ++cnt;
+                }
                 kdebug("USB2 port %d get desc ok", i);
             }
         }
@@ -1453,6 +1624,151 @@ static int xhci_hc_start_ports(int id)
     return 0;
 }
 
+/**
+ * @brief 发送HID设备的IDLE数据包
+ *
+ * @param id 主机控制器号
+ * @param port_id 端口号
+ * @param if_desc 接口结构体
+ * @return int
+ */
+static int xhci_hid_set_idle(const int id, const int port_id, struct usb_interface_desc *if_desc)
+{
+    struct usb_device_desc *dev_desc = xhci_hc[id].ports[port_id].dev_desc;
+    if (unlikely(dev_desc) == NULL)
+    {
+        BUG_ON(1);
+        return -EINVAL;
+    }
+
+    DECLARE_USB_PACKET(ctrl_out_packet, USB_REQ_TYPE_SET_CLASS_INTERFACE, 0x0a, 0, 0, 0);
+    xhci_control_out(id, &ctrl_out_packet, NULL, port_id, dev_desc->max_packet_size);
+    kdebug("xhci set idle done!");
+    return 0;
+}
+
+/**
+ * @brief 配置端点上下文,并发送configure endpoint命令
+ *
+ * @param id 主机控制器id
+ * @param port_id 端口号
+ * @param ep_num 端点号
+ * @param ep_type 端点类型
+ * @param ep_desc 端点描述符
+ * @return int 错误码
+ */
+static int xhci_configure_endpoint(const int id, const int port_id, const uint8_t ep_num, const uint8_t ep_type, struct usb_endpoint_desc *ep_desc)
+{
+
+    int retval = 0;
+    uint64_t slot_context_vaddr = xhci_get_device_context_vaddr(id, port_id);
+
+    xhci_initialize_ep(id, slot_context_vaddr, port_id, ep_num, xhci_hc[id].ports[port_id].dev_desc->max_packet_size,
+                       usb_get_max_burst_from_ep(ep_desc), ep_type, (ep_num % 2) ? XHCI_DIR_IN_BIT : XHCI_DIR_OUT_BIT,
+                       xhci_get_port_speed(id, port_id), ep_desc->interval);
+
+    struct xhci_slot_context_t slot;
+    struct xhci_ep_context_t ep = {0};
+    // 创建输入上下文缓冲区
+    uint64_t input_ctx_buffer = (uint64_t)kzalloc(xhci_hc[id].context_size * 33, 0);
+    // 置位对应的add bit
+    __write4b(input_ctx_buffer + 4, (1 << ep_num)|1);
+    __write4b(input_ctx_buffer + 0x1c, 1);
+
+    // 拷贝slot上下文
+    __read_from_slot(&slot, slot_context_vaddr);
+    // 设置该端口的最大端点号。注意,必须设置这里,否则会出错
+    slot.entries = (ep_num > slot.entries) ? ep_num : slot.entries;
+
+    __write_slot(input_ctx_buffer + xhci_hc[id].context_size, &slot);
+
+    // __write_ep(id, input_ctx_buffer, 2, &ep);
+    // kdebug("ep_num=%d", ep_num);
+    // 拷贝将要被配置的端点的信息
+    __read_from_ep(id, slot_context_vaddr, ep_num, &ep);
+    // kdebug("ep.tr_dequeue_ptr=%#018lx", ep.tr_dequeue_ptr);
+    ep.err_cnt = 3;
+    // 加一是因为input_context头部比slot_context多了一个input_control_ctx
+    __write_ep(id, input_ctx_buffer, ep_num + 1, &ep);
+
+    struct xhci_TRB_normal_t trb = {0};
+    trb.buf_paddr = virt_2_phys(input_ctx_buffer);
+    trb.TRB_type = TRB_TYPE_CONFIG_EP;
+    trb.cycle = xhci_hc[id].cmd_trb_cycle;
+    trb.Reserved |= (((uint16_t)xhci_hc[id].ports[port_id].slot_id) << 8) & 0xffff;
+
+    // kdebug("addr=%#018lx", ((struct xhci_TRB_t *)&trb)->param);
+    // kdebug("status=%#018lx", ((struct xhci_TRB_t *)&trb)->status);
+    // kdebug("command=%#018lx", ((struct xhci_TRB_t *)&trb)->command);
+    retval = xhci_send_command(id, (struct xhci_TRB_t *)&trb, true);
+
+    if (unlikely(retval != 0))
+    {
+        kerror("port_id:%d, configure endpoint %d failed", port_id, ep_num);
+        goto failed;
+    }
+
+    struct xhci_TRB_cmd_complete_t *trb_done = (struct xhci_TRB_cmd_complete_t *)&trb;
+    if (trb_done->code == TRB_COMP_TRB_SUCCESS) // 成功执行
+    {
+        // 如果要从控制器获取刚刚设置的设备地址的话,可以在这里读取slot context
+        ksuccess("port_id:%d, ep:%d successfully configured.", port_id, ep_num);
+        retval = 0;
+    }
+    else
+        retval = -EAGAIN;
+done:;
+failed:;
+    kfree((void *)input_ctx_buffer);
+    return retval;
+}
+
+/**
+ * @brief 配置连接在指定端口上的设备
+ *
+ * @param id 主机控制器id
+ * @param port_id 端口id
+ * @param full_conf 完整的config
+ * @return int 错误码
+ */
+static int xhci_configure_port(const int id, const int port_id)
+{
+    void *full_conf = NULL;
+    struct usb_interface_desc *if_desc = NULL;
+    struct usb_endpoint_desc *ep_desc = NULL;
+    // hint: 暂时只考虑对键盘的初始化
+    // 获取完整的config
+    {
+        struct usb_config_desc conf_desc = {0};
+        xhci_get_config_desc(id, port_id, &conf_desc);
+        full_conf = kzalloc(conf_desc.total_len, 0);
+        xhci_get_config_desc_full(id, port_id, &conf_desc, full_conf);
+    }
+
+    xhci_get_interface_desc(full_conf, 0, &if_desc);
+    if (if_desc->interface_class == USB_CLASS_HID)
+    {
+        // 由于暂时只支持键盘,因此把键盘的驱动也写在这里
+        // todo: 分离usb键盘驱动
+
+        xhci_get_endpoint_desc(if_desc, 0, &ep_desc);
+
+        // kdebug("to set conf, val=%#010lx", ((struct usb_config_desc *)full_conf)->value);
+        xhci_set_configuration(id, port_id, ((struct usb_config_desc *)full_conf)->value);
+        // kdebug("set conf ok");
+
+        // todo: configure endpoint
+        xhci_configure_endpoint(id, port_id, ep_desc->endpoint_addr, USB_EP_INTERRUPT, ep_desc);
+        
+        xhci_hid_set_idle(id, port_id, if_desc);
+        
+        // 获取report desc
+        // todo: parse hid report
+
+    }
+    kfree(full_conf);
+    return 0;
+}
 /**
  * @brief 初始化xhci主机控制器的中断控制
  *

+ 25 - 7
kernel/driver/usb/xhci/xhci.h

@@ -492,6 +492,13 @@ struct xhci_ep_context_t
 #define XHCI_PROTOCOL_HAS_PAIR (1 << 2) // 当前位被置位,意味着当前端口具有一个与之配对的端口
 #define XHCI_PROTOCOL_ACTIVE (1 << 3)   // 当前端口是这个配对中,被激活的端口
 
+struct xhci_ep_info_t
+{
+    uint64_t ep_ring_vbase;         // transfer ring的基地址
+    uint64_t current_ep_ring_vaddr; // transfer ring下一个要写入的地址
+    uint8_t current_ep_ring_cycle;  // 当前ep的cycle bit
+};
+
 /**
  * @brief xhci端口信息
  *
@@ -502,14 +509,11 @@ struct xhci_port_info_t
     uint8_t paired_port_num; // 与当前端口所配对的另一个端口(相同物理接口的不同速度的port)
     uint8_t offset;          // offset of this port within this protocal
     uint8_t reserved;
+    uint8_t slot_id;                   // address device获得的slot id
+    struct usb_device_desc *dev_desc;  // 指向设备描述符结构体的指针
+    struct xhci_ep_info_t ep_info[32]; // 各个端点的信息
 } __attribute__((packed));
 
-struct xhci_ep_ring_info_t
-{
-    uint64_t ep_ring_vbase;         // transfer ring的基地址
-    uint64_t current_ep_ring_vaddr; // transfer ring下一个要写入的地址
-    uint8_t current_ep_ring_cycle;  // 当前ep的cycle bit
-};
 struct xhci_host_controller_t
 {
     struct pci_device_structure_general_device_t *pci_dev_hdr; // 指向pci header结构体的指针
@@ -537,7 +541,6 @@ struct xhci_host_controller_t
     uint64_t current_event_ring_vaddr;                      // 下一个要读取的event TRB的虚拟地址
     uint64_t scratchpad_buf_array_vaddr;                    // 草稿行缓冲区数组的虚拟地址
     struct xhci_port_info_t ports[XHCI_MAX_ROOT_HUB_PORTS]; // 指向端口信息数组的指针(由于端口offset是从1开始的,因此该数组第0项为空)
-    struct xhci_ep_ring_info_t control_ep_info;             // 控制端点的信息
 };
 
 // Common TRB types
@@ -623,6 +626,21 @@ enum
     /* 224 - 225 vendor defined info */
 };
 
+/**
+ * @brief xhci endpoint类型
+ * 
+ */
+enum
+{
+    XHCI_EP_TYPE_INVALID = 0,
+    XHCI_EP_TYPE_ISO_OUT,
+    XHCI_EP_TYPE_BULK_OUT,
+    XHCI_EP_TYPE_INTR_OUT,
+    XHCI_EP_TYPE_CONTROL,
+    XHCI_EP_TYPE_ISO_IN,
+    XHCI_EP_TYPE_BULK_IN,
+    XHCI_EP_TYPE_INTR_IN,
+};
 /**
  * @brief 初始化xhci控制器
  *

+ 4 - 6
kernel/filesystem/fat32/fat32.c

@@ -18,17 +18,15 @@ extern struct blk_gendisk ahci_gendisk0;
 /**
  * @brief 注册指定磁盘上的指定分区的fat32文件系统
  *
- * @param ahci_ctrl_num ahci控制器编号
- * @param ahci_port_num ahci控制器端口编号
+ * @param blk_dev 块设备结构体
  * @param part_num 磁盘分区编号
  *
  * @return struct vfs_super_block_t * 文件系统的超级块
  */
-struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
+struct vfs_superblock_t *fat32_register_partition(struct block_device *blk_dev, uint8_t part_num)
 {
-
     // 挂载文件系统到vfs
-    return vfs_mount_fs("/", "FAT32", (ahci_gendisk0.partition + 0));
+    return vfs_mount_fs("/", "FAT32", blk_dev);
 }
 
 /**
@@ -1259,6 +1257,6 @@ void fat32_init()
     vfs_register_filesystem(&fat32_fs_type);
 
     // 挂载根文件系统
-    fat32_register_partition(0, 0, 0);
+    fat32_register_partition(ahci_gendisk0.partition + 0, 0);
     kinfo("FAT32 initialized.");
 }

+ 5 - 4
kernel/filesystem/fat32/fat32.h

@@ -167,11 +167,12 @@ typedef struct fat32_inode_info_t fat32_inode_info_t;
 /**
  * @brief 注册指定磁盘上的指定分区的fat32文件系统
  *
- * @param ahci_ctrl_num ahci控制器编号
- * @param ahci_port_num ahci控制器端口编号
+ * @param blk_dev 块设备结构体
+ * @param part_num 磁盘分区编号
+ *
  * @return struct vfs_super_block_t * 文件系统的超级块
  */
-struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num);
+struct vfs_superblock_t *fat32_register_partition(struct block_device *blk_dev, uint8_t part_num);
 
 /**
  * @brief 创建fat32文件系统的超级块
@@ -179,7 +180,7 @@ struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t
  * @param blk 块设备结构体
  * @return struct vfs_superblock_t* 创建好的超级块
  */
-struct vfs_superblock_t *fat32_read_superblock(struct block_device* blk);
+struct vfs_superblock_t *fat32_read_superblock(struct block_device *blk);
 
 /**
  * @brief 创建新的文件

+ 3 - 2
kernel/process/process.c

@@ -486,7 +486,8 @@ ul initial_kernel_thread(ul arg)
     rootfs_umount();
 
     // 使用单独的内核线程来初始化usb驱动程序
-    int usb_pid = kernel_thread(usb_init, 0, 0);
+    // 注释:由于目前usb驱动程序不完善,因此先将其注释掉
+    // int usb_pid = kernel_thread(usb_init, 0, 0);
 
     kinfo("LZ4 lib Version=%s", LZ4_versionString());
 
@@ -495,7 +496,7 @@ ul initial_kernel_thread(ul arg)
         ktest_start(ktest_test_bitree, 0),
         ktest_start(ktest_test_kfifo, 0),
         ktest_start(ktest_test_mutex, 0),
-        usb_pid,
+        // usb_pid,
     };
     kinfo("Waiting test thread exit...");
     // 等待测试进程退出