usb.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. int 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. kdebug("usb_pdevs_count=%d", usb_pdevs_count);
  28. // 初始化每个usb控制器
  29. for (volatile int i = 0; i < usb_pdevs_count; ++i)
  30. {
  31. io_mfence();
  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. io_mfence();
  44. xhci_init((struct pci_device_structure_general_device_t *)usb_pdevs[i]);
  45. io_mfence();
  46. break;
  47. default:
  48. kerror("Error value of usb_pdevs[%d]->ProgIF: %#02x", i, usb_pdevs[i]->ProgIF);
  49. return;
  50. break;
  51. }
  52. }
  53. kinfo("Successfully initialized all usb host controllers!");
  54. return 0;
  55. }