@@ -38,10 +38,14 @@ spin_t transfer_lock = SPIN_INIT;
3838
3939// 分配DMA内存(对齐到64字节)
4040static void * xhci_alloc_dma (size_t size , uint64_t * phys_addr ) {
41- void * ptr = malloc ((size + 63 ) & ~63 );
41+ size_t aligned_bytes = (size + PAGE_SIZE - 1 ) & ~(PAGE_SIZE - 1 );
42+ size_t required_pages = aligned_bytes / PAGE_SIZE ;
43+ uint64_t phys = alloc_frames (required_pages );
44+ page_map_range_to (get_current_directory (),phys ,aligned_bytes ,KERNEL_PTE_FLAGS );
45+ void * ptr = phys_to_virt (phys );
4246 if (ptr ) {
4347 memset (ptr , 0 , size );
44- if (phys_addr ) { * phys_addr = page_virt_to_phys (( uint64_t ) ptr ) ; }
48+ if (phys_addr ) { * phys_addr = phys ; }
4549 }
4650 return ptr ;
4751}
@@ -70,7 +74,9 @@ xhci_ring_t *xhci_alloc_ring(uint32_t num_trbs) {
7074void xhci_free_ring (xhci_ring_t * ring ) {
7175 if (!ring ) return ;
7276
73- if (ring -> trbs ) { free (ring -> trbs ); }
77+ if (ring -> trbs ) {
78+ unmap_page_range (get_current_directory (),(uint64_t )ring -> trbs ,ring -> size );
79+ }
7480 free (ring );
7581}
7682
@@ -317,15 +323,18 @@ static int xhci_hcd_init(usb_hcd_t *hcd) {
317323 if (xhci -> num_scratchpads > 0 ) {
318324 xhci -> scratchpad_array =
319325 (uint64_t * )xhci_alloc_dma (xhci -> num_scratchpads * sizeof (uint64_t ), NULL );
320- xhci -> scratchpad_buffers = (void * * )malloc (xhci -> num_scratchpads * sizeof (void * ));
326+
327+ uint64_t scphy = alloc_frames (PADDING_REQ (xhci -> num_scratchpads * sizeof (void * ),PAGE_SIZE ));
328+ page_map_range_to (get_current_directory (),scphy ,xhci -> num_scratchpads * sizeof (void * ),KERNEL_PTE_FLAGS );
329+ xhci -> scratchpad_buffers = (void * * )phys_to_virt (scphy );
321330
322331 for (uint32_t i = 0 ; i < xhci -> num_scratchpads ; i ++ ) {
323332 uint64_t phys ;
324333 xhci -> scratchpad_buffers [i ] = xhci_alloc_dma (4096 , & phys );
325334 xhci -> scratchpad_array [i ] = phys ;
326335 }
327336
328- xhci -> dcbaa [0 ] = page_virt_to_phys ((uint64_t )xhci -> scratchpad_array );
337+ xhci -> dcbaa [0 ] = ( uint64_t ) virt_to_phys ((uint64_t )xhci -> scratchpad_array );
329338 }
330339
331340 // 设置DCBAA指针
@@ -559,7 +568,7 @@ int xhci_setup_default_endpoint(usb_hcd_t *hcd, usb_device_t *device) {
559568 return 0 ;
560569}
561570
562- spin_t xhci_command_lock = { 0 } ;
571+ spin_t xhci_command_lock = SPIN_INIT ;
563572
564573// 启用槽位 - 使用等待机制
565574static int xhci_enable_slot (usb_hcd_t * hcd , usb_device_t * device ) {
@@ -1026,7 +1035,7 @@ static int xhci_configure_endpoint(usb_hcd_t *hcd, usb_endpoint_t *endpoint) {
10261035 return ret ;
10271036}
10281037
1029- spin_t xhci_transfer_lock = { 0 } ;
1038+ spin_t xhci_transfer_lock = SPIN_INIT ;
10301039
10311040// 控制传输 - 使用等待机制
10321041static int xhci_control_transfer (usb_hcd_t * hcd , usb_transfer_t * transfer ,
@@ -1258,7 +1267,7 @@ typedef struct enumerater_arg {
12581267 uint8_t speed ;
12591268} enumerater_arg_t ;
12601269
1261- spin_t enumerate_lock = { 0 } ;
1270+ spin_t enumerate_lock = SPIN_INIT ;
12621271
12631272void xhci_device_enumerater (enumerater_arg_t * arg ) {
12641273 spin_lock (enumerate_lock );
@@ -1387,7 +1396,7 @@ pci_device_t *deviceA = NULL;
13871396
13881397__attribute__((used )) __attribute__((visibility ("default" ))) int dlstart (void ) {
13891398 usb_hcd_t * xhci_hcd = xhci_init (mmio_vaddr , deviceA );
1390- printk ("xhci: %s inited - port:%d \n" , xhci_hcd -> name , xhci_hcd -> devices -> port );
1399+ printk ("xhci: %s inited。 \n" , xhci_hcd -> name );
13911400 return EOK ;
13921401}
13931402
0 commit comments