usb.c 1.7 KB

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