usb_proto.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * usb_proto.h
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #pragma once
  24. #include <stdint.h>
  25. // Descriptors
  26. // -----------
  27. enum usb_desc_type {
  28. USB_DT_DEV = 1,
  29. USB_DT_CONF = 2,
  30. USB_DT_STR = 3,
  31. USB_DT_INTF = 4,
  32. USB_DT_EP = 5,
  33. USB_DT_DEV_QUAL = 6,
  34. USB_DT_OTHER_SPEED_CONF = 7,
  35. USB_DT_INTF_PWR = 8,
  36. USB_DT_OTG = 9,
  37. USB_DT_DEBUG = 10,
  38. USB_DT_INTF_ASSOC = 11,
  39. USB_DT_DFU = 33,
  40. USB_DT_CS_INTF = 36,
  41. USB_DT_CS_EP = 37,
  42. };
  43. struct usb_desc_hdr {
  44. uint8_t bLength;
  45. uint8_t bDescriptorType;
  46. } __attribute__((packed));
  47. struct usb_dev_desc {
  48. uint8_t bLength;
  49. uint8_t bDescriptorType;
  50. uint16_t bcdUSB;
  51. uint8_t bDeviceClass;
  52. uint8_t bDeviceSubClass;
  53. uint8_t bDeviceProtocol;
  54. uint8_t bMaxPacketSize0;
  55. uint16_t idVendor;
  56. uint16_t idProduct;
  57. uint16_t bcdDevice;
  58. uint8_t iManufacturer;
  59. uint8_t iProduct;
  60. uint8_t iSerialNumber;
  61. uint8_t bNumConfigurations;
  62. } __attribute__((packed));
  63. struct usb_conf_desc {
  64. uint8_t bLength;
  65. uint8_t bDescriptorType;
  66. uint16_t wTotalLength;
  67. uint8_t bNumInterfaces;
  68. uint8_t bConfigurationValue;
  69. uint8_t iConfiguration;
  70. uint8_t bmAttributes;
  71. uint8_t bMaxPower;
  72. } __attribute__((packed));
  73. struct usb_intf_desc {
  74. uint8_t bLength;
  75. uint8_t bDescriptorType;
  76. uint8_t bInterfaceNumber;
  77. uint8_t bAlternateSetting;
  78. uint8_t bNumEndpoints;
  79. uint8_t bInterfaceClass;
  80. uint8_t bInterfaceSubClass;
  81. uint8_t bInterfaceProtocol;
  82. uint8_t iInterface;
  83. } __attribute__((packed));
  84. struct usb_ep_desc {
  85. uint8_t bLength;
  86. uint8_t bDescriptorType;
  87. uint8_t bEndpointAddress;
  88. uint8_t bmAttributes;
  89. uint16_t wMaxPacketSize;
  90. uint8_t bInterval;
  91. } __attribute__((packed));
  92. struct usb_intf_assoc_desc {
  93. uint8_t bLength;
  94. uint8_t bDescriptorType;
  95. uint8_t bFirstInterface;
  96. uint8_t bInterfaceCount;
  97. uint8_t bFunctionClass;
  98. uint8_t bFunctionSubClass;
  99. uint8_t bFunctionProtocol;
  100. uint8_t iFunction;
  101. } __attribute__((packed));
  102. struct usb_str_desc {
  103. uint8_t bLength;
  104. uint8_t bDescriptorType;
  105. uint16_t wString[];
  106. } __attribute__((packed));
  107. struct usb_dfu_desc {
  108. uint8_t bLength;
  109. uint8_t bDescriptorType;
  110. uint8_t bmAttributes;
  111. uint16_t wDetachTimeOut;
  112. uint16_t wTransferSize;
  113. uint16_t bcdDFUVersion;
  114. } __attribute__((packed));
  115. struct usb_cs_intf_hdr_desc {
  116. uint8_t bLength;
  117. uint8_t bDescriptorType;
  118. uint8_t bDescriptorsubtype;
  119. uint16_t bcdCDC;
  120. } __attribute__((packed));
  121. struct usb_cs_intf_acm_desc {
  122. uint8_t bLength;
  123. uint8_t bDescriptorType;
  124. uint8_t bDescriptorsubtype;
  125. uint8_t bmCapabilities;
  126. } __attribute__((packed));
  127. struct usb_cs_intf_union_desc {
  128. uint8_t bLength;
  129. uint8_t bDescriptorType;
  130. uint8_t bDescriptorsubtype;
  131. uint8_t bMasterInterface;
  132. /* uint8_t bSlaveInterface[]; */
  133. } __attribute__((packed));
  134. struct usb_cs_intf_call_mgmt_desc {
  135. uint8_t bLength;
  136. uint8_t bDescriptorType;
  137. uint8_t bDescriptorsubtype;
  138. uint8_t bmCapabilities;
  139. uint8_t bDataInterface;
  140. } __attribute__((packed));
  141. // Control requests
  142. // ----------------
  143. struct usb_ctrl_req {
  144. union {
  145. struct {
  146. uint8_t bmRequestType;
  147. uint8_t bRequest;
  148. };
  149. uint16_t wRequestAndType;
  150. };
  151. uint16_t wValue;
  152. uint16_t wIndex;
  153. uint16_t wLength;
  154. } __attribute__((packed,aligned(4)));
  155. #define USB_REQ_RCPT_MSK (0x1f)
  156. #define USB_REQ_RCPT(req) ((req)->bmRequestType & USB_REQ_RCPT_MSK)
  157. #define USB_REQ_RCPT_DEV (0 << 0)
  158. #define USB_REQ_RCPT_INTF (1 << 0)
  159. #define USB_REQ_RCPT_EP (2 << 0)
  160. #define USB_REQ_RCPT_OTHER (3 << 0)
  161. #define USB_REQ_TYPE_MSK (0x60)
  162. #define USB_REQ_TYPE(req) ((req)->bmRequestType & USB_REQ_TYPE_MSK)
  163. #define USB_REQ_TYPE_STD (0 << 5)
  164. #define USB_REQ_TYPE_CLASS (1 << 5)
  165. #define USB_REQ_TYPE_VENDOR (2 << 5)
  166. #define USB_REQ_TYPE_RSVD (3 << 5)
  167. #define USB_REQ_TYPE_RCPT(req) ((req)->bmRequestType & (USB_REQ_RCPT_MSK | USB_REQ_TYPE_MSK))
  168. #define USB_REQ_READ (1 << 7)
  169. #define USB_REQ_IS_READ(req) ( (req)->bmRequestType & USB_REQ_READ )
  170. #define USB_REQ_IS_WRITE(req) (!((req)->bmRequestType & USB_REQ_READ))
  171. /* wRequestAndType */
  172. #define USB_RT_GET_STATUS_DEV (( 0 << 8) | 0x80)
  173. #define USB_RT_GET_STATUS_INTF (( 0 << 8) | 0x81)
  174. #define USB_RT_GET_STATUS_EP (( 0 << 8) | 0x82)
  175. #define USB_RT_CLEAR_FEATURE_DEV (( 1 << 8) | 0x00)
  176. #define USB_RT_CLEAR_FEATURE_INTF (( 1 << 8) | 0x01)
  177. #define USB_RT_CLEAR_FEATURE_EP (( 1 << 8) | 0x02)
  178. #define USB_RT_SET_FEATURE_DEV (( 3 << 8) | 0x00)
  179. #define USB_RT_SET_FEATURE_INTF (( 3 << 8) | 0x01)
  180. #define USB_RT_SET_FEATURE_EP (( 3 << 8) | 0x02)
  181. #define USB_RT_SET_ADDRESS (( 5 << 8) | 0x00)
  182. #define USB_RT_GET_DESCRIPTOR (( 6 << 8) | 0x80)
  183. #define USB_RT_SET_DESCRIPTOR (( 7 << 8) | 0x00)
  184. #define USB_RT_GET_CONFIGURATION (( 8 << 8) | 0x80)
  185. #define USB_RT_SET_CONFIGURATION (( 9 << 8) | 0x00)
  186. #define USB_RT_GET_INTERFACE ((10 << 8) | 0x81)
  187. #define USB_RT_SET_INTERFACE ((11 << 8) | 0x01)
  188. #define USB_RT_SYNCHFRAME ((12 << 8) | 0x82)