1 #include "usb.h" 2 #include "xhci/xhci.h" 3 #include <common/kprint.h> 4 #include <common/errno.h> 5 #include <driver/pci/pci.h> 6 #include <debug/bug.h> 7 #include <common/spinlock.h> 8 9 extern spinlock_t xhci_controller_init_lock; // xhci控制器初始化锁 10 11 #define MAX_USB_NUM 8 // pci总线上的usb设备的最大数量 12 13 // 在pci总线上寻找到的usb设备控制器的header 14 static struct pci_device_structure_header_t *usb_pdevs[MAX_USB_NUM]; 15 static int usb_pdevs_count = 0; 16 17 /** 18 * @brief 初始化usb驱动程序 19 * 20 */ usb_init(void * unused)21int usb_init(void* unused) 22 { 23 kinfo("Initializing usb driver..."); 24 spin_init(&xhci_controller_init_lock); 25 26 // 获取所有usb-pci设备的列表 27 pci_get_device_structure(USB_CLASS, USB_SUBCLASS, usb_pdevs, &usb_pdevs_count); 28 29 if (WARN_ON(usb_pdevs_count == 0)) 30 { 31 kwarn("There is no usb hardware in this computer!"); 32 return 0; 33 } 34 kdebug("usb_pdevs_count=%d", usb_pdevs_count); 35 // 初始化每个usb控制器 36 for (volatile int i = 0; i < usb_pdevs_count; ++i) 37 { 38 io_mfence(); 39 switch (usb_pdevs[i]->ProgIF) 40 { 41 case USB_TYPE_UHCI: 42 case USB_TYPE_OHCI: 43 case USB_TYPE_EHCI: 44 case USB_TYPE_UNSPEC: 45 case USB_TYPE_DEVICE: 46 kwarn("Unsupported usb host type: %#02x", usb_pdevs[i]->ProgIF); 47 break; 48 49 case USB_TYPE_XHCI: 50 // 初始化对应的xhci控制器 51 io_mfence(); 52 xhci_init((struct pci_device_structure_general_device_t *)usb_pdevs[i]); 53 io_mfence(); 54 break; 55 56 default: 57 kerror("Error value of usb_pdevs[%d]->ProgIF: %#02x", i, usb_pdevs[i]->ProgIF); 58 return -EINVAL; 59 break; 60 } 61 } 62 kinfo("Successfully initialized all usb host controllers!"); 63 return 0; 64 }