usb.c 1.6 KB

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