usb.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include <common/sys/types.h>
  3. // usb设备在pci总线上的class
  4. #define USB_CLASS 0xC
  5. #define USB_SUBCLASS 0x3
  6. // 不同的usb设备在pci总线上的prog IF
  7. #define USB_TYPE_UHCI 0x0
  8. #define USB_TYPE_OHCI 0x10
  9. #define USB_TYPE_EHCI 0x20
  10. #define USB_TYPE_XHCI 0x30
  11. #define USB_TYPE_UNSPEC 0x80 // Unspecified
  12. #define USB_TYPE_DEVICE 0xfe // USB Device(Not controller)
  13. // Reset wait times(milliseconds) ,USB 2.0 specs, page 153, section 7.1.7.5, paragraph 3
  14. #define USB_TIME_RST_RH 50 // reset on a root hub
  15. #define USB_TIME_RST_MIN 10 // minimum delay for a reset
  16. #define USB_TIME_RST_NOMORE 3 // No more than this between resets for root hubs
  17. #define USB_TIME_RST_REC 10 // reset recovery
  18. /**
  19. * @brief usb 设备描述符
  20. *
  21. */
  22. struct usb_device_desc
  23. {
  24. uint8_t len;
  25. uint8_t type;
  26. uint16_t usb_version;
  27. uint8_t _class;
  28. uint8_t subclass;
  29. uint8_t protocol;
  30. uint8_t max_packet_size;
  31. uint16_t vendor_id;
  32. uint16_t product_id;
  33. uint16_t device_rel;
  34. uint8_t manufacturer_index;
  35. uint8_t procuct_index;
  36. uint8_t serial_index;
  37. uint8_t config; // number of configurations
  38. };
  39. /**
  40. * @brief usb设备请求包
  41. *
  42. */
  43. struct usb_request_packet_t
  44. {
  45. uint8_t request_type;
  46. uint8_t request;
  47. uint16_t value;
  48. uint16_t index;
  49. uint16_t length;
  50. };
  51. // usb设备请求包的request_type字段的值
  52. #define __USB_REQ_TYPE_H2D 0x00
  53. #define __USB_REQ_TYPE_D2H 0x80
  54. #define __USB_REQ_TYPE_STANDARD 0x00
  55. #define __USB_REQ_TYPE_CLASS 0x20
  56. #define __USB_REQ_TYPE_VENDOR 0x40
  57. #define __USB_REQ_TYPE_RSVD 0x60
  58. #define __USB_REQ_TYPE_DEVICE 0x00
  59. #define __USB_REQ_TYPE_INTERFACE 0x01
  60. #define __USB_REQ_TYPE_ENDPOINT 0x02
  61. #define __USB_REQ_TYPE_OTHER 0x03
  62. #define USB_REQ_TYPE_GET_REQUEST (__USB_REQ_TYPE_D2H | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_DEVICE)
  63. #define USB_REQ_TYPE_SET_REQUEST (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_DEVICE)
  64. #define USB_REQ_TYPE_SET_INTERFACE (__USB_REQ_TYPE_H2D | __USB_REQ_TYPE_STANDARD | __USB_REQ_TYPE_INTERFACE)
  65. // device requests
  66. enum
  67. {
  68. USB_REQ_GET_STATUS = 0,
  69. USB_REQ_CLEAR_FEATURE,
  70. USB_REQ_SET_FEATURE = 3,
  71. USB_REQ_SET_ADDRESS = 5,
  72. USB_REQ_GET_DESCRIPTOR = 6,
  73. USB_REQ_SET_DESCRIPTOR,
  74. USB_REQ_GET_CONFIGURATION,
  75. USB_REQ_SET_CONFIGURATION,
  76. // interface requests
  77. USB_REQ_GET_INTERFACE,
  78. USB_REQ_SET_INTERFACE,
  79. // standard endpoint requests
  80. USB_REQ_SYNCH_FRAME,
  81. // Device specific
  82. USB_REQ_GET_MAX_LUNS = 0xFE,
  83. USB_REQ_BULK_ONLY_RESET
  84. };
  85. // Descriptor types
  86. enum
  87. {
  88. USB_DT_DEVICE = 1,
  89. USB_DT_CONFIG,
  90. USB_DT_STRING,
  91. USB_DT_INTERFACE,
  92. USB_DT_ENDPOINT,
  93. USB_DT_DEVICE_QUALIFIER,
  94. USB_DT_OTHER_SPEED_CONFIG,
  95. USB_DT_INTERFACE_POWER,
  96. USB_DT_OTG,
  97. USB_DT_DEBUG,
  98. USB_DT_INTERFACE_ASSOSIATION,
  99. USB_DT_HID = 0x21,
  100. USB_DT_HID_REPORT,
  101. USB_DT_HID_PHYSICAL,
  102. USB_DT_INTERFACE_FUNCTION = 0x24,
  103. USB_DT_ENDPOINT_FUNCTION,
  104. HUB = 0x29
  105. };
  106. // transfer types (Endpoint types) (USB 2.0 page 270)
  107. enum
  108. {
  109. USB_EP_CONTROL = 0,
  110. USB_EP_ISOCHRONOUS,
  111. USB_EP_BULK,
  112. USB_EP_INTERRUPT
  113. };
  114. /**
  115. * @brief 初始化usb驱动程序
  116. *
  117. */
  118. int usb_init();