usb_std.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2016-2022, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. #ifndef _USB_STD_H_
  4. #define _USB_STD_H_
  5. /*- Includes ----------------------------------------------------------------*/
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include <stdalign.h>
  11. /*- Definitions -------------------------------------------------------------*/
  12. enum
  13. {
  14. USB_GET_STATUS = 0,
  15. USB_CLEAR_FEATURE = 1,
  16. USB_SET_FEATURE = 3,
  17. USB_SET_ADDRESS = 5,
  18. USB_GET_DESCRIPTOR = 6,
  19. USB_SET_DESCRIPTOR = 7,
  20. USB_GET_CONFIGURATION = 8,
  21. USB_SET_CONFIGURATION = 9,
  22. USB_GET_INTERFACE = 10,
  23. USB_SET_INTERFACE = 11,
  24. USB_SYNCH_FRAME = 12,
  25. };
  26. enum
  27. {
  28. USB_DEVICE_DESCRIPTOR = 1,
  29. USB_CONFIGURATION_DESCRIPTOR = 2,
  30. USB_STRING_DESCRIPTOR = 3,
  31. USB_INTERFACE_DESCRIPTOR = 4,
  32. USB_ENDPOINT_DESCRIPTOR = 5,
  33. USB_DEVICE_QUALIFIER_DESCRIPTOR = 6,
  34. USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR = 7,
  35. USB_INTERFACE_POWER_DESCRIPTOR = 8,
  36. USB_OTG_DESCRIPTOR = 9,
  37. USB_DEBUG_DESCRIPTOR = 10,
  38. USB_INTERFACE_ASSOCIATION_DESCRIPTOR = 11,
  39. USB_BINARY_OBJECT_STORE_DESCRIPTOR = 15,
  40. USB_DEVICE_CAPABILITY_DESCRIPTOR = 16,
  41. USB_CS_INTERFACE_DESCRIPTOR = 36,
  42. };
  43. enum
  44. {
  45. USB_DEVICE_RECIPIENT = 0,
  46. USB_INTERFACE_RECIPIENT = 1,
  47. USB_ENDPOINT_RECIPIENT = 2,
  48. USB_OTHER_RECIPIENT = 3,
  49. };
  50. enum
  51. {
  52. USB_STANDARD_REQUEST = 0,
  53. USB_CLASS_REQUEST = 1,
  54. USB_VENDOR_REQUEST = 2,
  55. };
  56. enum
  57. {
  58. USB_OUT_TRANSFER = 0,
  59. USB_IN_TRANSFER = 1,
  60. };
  61. enum
  62. {
  63. USB_IN_ENDPOINT = 0x80,
  64. USB_OUT_ENDPOINT = 0x00,
  65. USB_INDEX_MASK = 0x7f,
  66. USB_DIRECTION_MASK = 0x80,
  67. };
  68. enum
  69. {
  70. USB_CONTROL_ENDPOINT = 0 << 0,
  71. USB_ISOCHRONOUS_ENDPOINT = 1 << 0,
  72. USB_BULK_ENDPOINT = 2 << 0,
  73. USB_INTERRUPT_ENDPOINT = 3 << 0,
  74. USB_NO_SYNCHRONIZATION = 0 << 2,
  75. USB_ASYNCHRONOUS = 1 << 2,
  76. USB_ADAPTIVE = 2 << 2,
  77. USB_SYNCHRONOUS = 3 << 2,
  78. USB_DATA_ENDPOINT = 0 << 4,
  79. USB_FEEDBACK_ENDPOINT = 1 << 4,
  80. USB_IMP_FB_DATA_ENDPOINT = 2 << 4,
  81. };
  82. enum
  83. {
  84. USB_ATTRIBUTE_REMOTE_WAKEUP = 0x20,
  85. USB_ATTRIBUTE_SELF_POWERED = 0x40,
  86. USB_ATTRIBUTE_BUS_POWERED = 0x80,
  87. };
  88. enum
  89. {
  90. USB_DEVICE_CAPABILITY_WIRELESS_USB = 1,
  91. USB_DEVICE_CAPABILITY_USB_2_0_EXTENSION = 2,
  92. USB_DEVICE_CAPABILITY_SUPERSPEED_USB = 3,
  93. USB_DEVICE_CAPABILITY_CONTAINER_ID = 4,
  94. USB_DEVICE_CAPABILITY_PLATFORM = 5,
  95. USB_DEVICE_CAPABILITY_POWER_DELIVERY = 6,
  96. USB_DEVICE_CAPABILITY_BATTERY_INFO = 7,
  97. USB_DEVICE_CAPABILITY_PD_CONSUMER_PORT = 8,
  98. USB_DEVICE_CAPABILITY_PD_PROVIDER_PORT = 9,
  99. USB_DEVICE_CAPABILITY_SUPERSPEED_PLUS = 10,
  100. USB_DEVICE_CAPABILITY_PRECISION_TIME_MEASUREMENT = 11,
  101. USB_DEVICE_CAPABILITY_WIRELESS_USB_EXT = 12,
  102. };
  103. enum
  104. {
  105. USB_DEVICE_CLASS_MISCELLANEOUS = 0xef,
  106. USB_DEVICE_CLASS_VENDOR_SPECIFIC = 0xff,
  107. };
  108. enum
  109. {
  110. USB_DEVICE_SUBCLASS_COMMON = 0x02,
  111. };
  112. enum
  113. {
  114. USB_DEVICE_PROTOCOL_INTERFACE_ASSOCIATION = 0x01,
  115. };
  116. #define USB_CTRL_EP_SIZE 64
  117. #define USB_LANGID_ENGLISH 0x0409 // English (United States)
  118. #define USB_MAX_POWER(ma) ((ma) / 2)
  119. #define USB_PACK __attribute__((packed))
  120. #define USB_LIMIT(a, b) (((int)(a) > (int)(b)) ? (int)(b) : (int)(a))
  121. #define USB_ARRAY_SIZE(x) ((int)(sizeof(x) / sizeof(0[x])))
  122. #define USB_CMD_VALUE(req) (((req)->bRequest << 8) | (req)->bmRequestType)
  123. #define USB_CMD(dir, rcpt, type, cmd) \
  124. ((USB_##cmd << 8) | (USB_##dir##_TRANSFER << 7) | \
  125. (USB_##type##_REQUEST << 5) | (USB_##rcpt##_RECIPIENT << 0))
  126. /*- Types -------------------------------------------------------------------*/
  127. typedef struct USB_PACK
  128. {
  129. uint8_t bmRequestType;
  130. uint8_t bRequest;
  131. uint16_t wValue;
  132. uint16_t wIndex;
  133. uint16_t wLength;
  134. } usb_request_t;
  135. typedef struct USB_PACK
  136. {
  137. uint8_t bLength;
  138. uint8_t bDescriptorType;
  139. } usb_descriptor_header_t;
  140. typedef struct USB_PACK
  141. {
  142. uint8_t bLength;
  143. uint8_t bDescriptorType;
  144. uint16_t bcdUSB;
  145. uint8_t bDeviceClass;
  146. uint8_t bDeviceSubClass;
  147. uint8_t bDeviceProtocol;
  148. uint8_t bMaxPacketSize0;
  149. uint16_t idVendor;
  150. uint16_t idProduct;
  151. uint16_t bcdDevice;
  152. uint8_t iManufacturer;
  153. uint8_t iProduct;
  154. uint8_t iSerialNumber;
  155. uint8_t bNumConfigurations;
  156. } usb_device_descriptor_t;
  157. typedef struct USB_PACK
  158. {
  159. uint8_t bLength;
  160. uint8_t bDescriptorType;
  161. uint16_t wTotalLength;
  162. uint8_t bNumInterfaces;
  163. uint8_t bConfigurationValue;
  164. uint8_t iConfiguration;
  165. uint8_t bmAttributes;
  166. uint8_t bMaxPower;
  167. } usb_configuration_descriptor_t;
  168. typedef struct USB_PACK
  169. {
  170. uint8_t bLength;
  171. uint8_t bDescriptorType;
  172. uint8_t bInterfaceNumber;
  173. uint8_t bAlternateSetting;
  174. uint8_t bNumEndpoints;
  175. uint8_t bInterfaceClass;
  176. uint8_t bInterfaceSubClass;
  177. uint8_t bInterfaceProtocol;
  178. uint8_t iInterface;
  179. } usb_interface_descriptor_t;
  180. typedef struct USB_PACK
  181. {
  182. uint8_t bLength;
  183. uint8_t bDescriptorType;
  184. uint8_t bEndpointAddress;
  185. uint8_t bmAttributes;
  186. uint16_t wMaxPacketSize;
  187. uint8_t bInterval;
  188. } usb_endpoint_descriptor_t;
  189. typedef struct USB_PACK
  190. {
  191. uint8_t bLength;
  192. uint8_t bDescriptorType;
  193. uint16_t wLANGID;
  194. } usb_string_descriptor_zero_t;
  195. typedef struct USB_PACK
  196. {
  197. uint8_t bLength;
  198. uint8_t bDescriptorType;
  199. uint16_t bString;
  200. } usb_string_descriptor_t;
  201. typedef struct USB_PACK
  202. {
  203. uint8_t bLength;
  204. uint8_t bDescriptorType;
  205. uint8_t bFirstInterface;
  206. uint8_t bInterfaceCount;
  207. uint8_t bFunctionClass;
  208. uint8_t bFunctionSubClass;
  209. uint8_t bFunctionProtocol;
  210. uint8_t iFunction;
  211. } usb_interface_association_descriptor_t;
  212. typedef struct USB_PACK
  213. {
  214. uint8_t bLength;
  215. uint8_t bDescriptorType;
  216. uint16_t wTotalLength;
  217. uint8_t bNumDeviceCaps;
  218. } usb_binary_object_store_descriptor_t;
  219. typedef bool (*usb_class_handler_t)(usb_request_t *request);
  220. /*- Prototypes --------------------------------------------------------------*/
  221. void usb_init(void);
  222. void usb_set_send_callback(int ep, void (*callback)(void));
  223. void usb_set_recv_callback(int ep, void (*callback)(int size));
  224. bool usb_handle_standard_request(usb_request_t *request);
  225. void usb_send_callback(int ep);
  226. void usb_recv_callback(int ep, int size);
  227. #endif // _USB_STD_H_