usb.c 1.6 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. // 在pci总线上寻找到的usb设备控制器的header
  10. static 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. io_mfence();
  31. switch (usb_pdevs[i]->ProgIF)
  32. {
  33. case USB_TYPE_UHCI:
  34. case USB_TYPE_OHCI:
  35. case USB_TYPE_EHCI:
  36. case USB_TYPE_UNSPEC:
  37. case USB_TYPE_DEVICE:
  38. kwarn("Unsupported usb host type: %#02x", usb_pdevs[i]->ProgIF);
  39. break;
  40. case USB_TYPE_XHCI:
  41. // 初始化对应的xhci控制器
  42. xhci_init((struct pci_device_structure_general_device_t *)usb_pdevs[i]);
  43. io_mfence();
  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. }