usb.c 1.8 KB

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