usb_std.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2016-2017, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef _USB_STD_H_
  29. #define _USB_STD_H_
  30. /*- Includes ----------------------------------------------------------------*/
  31. #include <stdint.h>
  32. #include <stdbool.h>
  33. #include "utils.h"
  34. /*- Definitions -------------------------------------------------------------*/
  35. enum
  36. {
  37. USB_GET_STATUS = 0,
  38. USB_CLEAR_FEATURE = 1,
  39. USB_SET_FEATURE = 3,
  40. USB_SET_ADDRESS = 5,
  41. USB_GET_DESCRIPTOR = 6,
  42. USB_SET_DESCRIPTOR = 7,
  43. USB_GET_CONFIGURATION = 8,
  44. USB_SET_CONFIGURATION = 9,
  45. USB_GET_INTERFACE = 10,
  46. USB_SET_INTERFACE = 11,
  47. USB_SYNCH_FRAME = 12,
  48. };
  49. enum
  50. {
  51. USB_DEVICE_DESCRIPTOR = 1,
  52. USB_CONFIGURATION_DESCRIPTOR = 2,
  53. USB_STRING_DESCRIPTOR = 3,
  54. USB_INTERFACE_DESCRIPTOR = 4,
  55. USB_ENDPOINT_DESCRIPTOR = 5,
  56. USB_DEVICE_QUALIFIER_DESCRIPTOR = 6,
  57. USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR = 7,
  58. USB_INTERFACE_POWER_DESCRIPTOR = 8,
  59. USB_OTG_DESCRIPTOR = 9,
  60. USB_DEBUG_DESCRIPTOR = 10,
  61. USB_INTERFACE_ASSOCIATION_DESCRIPTOR = 11,
  62. USB_BINARY_OBJECT_STORE_DESCRIPTOR = 15,
  63. USB_DEVICE_CAPABILITY_DESCRIPTOR = 16,
  64. USB_CS_INTERFACE_DESCRIPTOR = 36,
  65. };
  66. enum
  67. {
  68. USB_DEVICE_RECIPIENT = 0,
  69. USB_INTERFACE_RECIPIENT = 1,
  70. USB_ENDPOINT_RECIPIENT = 2,
  71. USB_OTHER_RECIPIENT = 3,
  72. };
  73. enum
  74. {
  75. USB_STANDARD_REQUEST = 0,
  76. USB_CLASS_REQUEST = 1,
  77. USB_VENDOR_REQUEST = 2,
  78. };
  79. enum
  80. {
  81. USB_OUT_TRANSFER = 0,
  82. USB_IN_TRANSFER = 1,
  83. };
  84. enum
  85. {
  86. USB_IN_ENDPOINT = 0x80,
  87. USB_OUT_ENDPOINT = 0x00,
  88. USB_INDEX_MASK = 0x7f,
  89. USB_DIRECTION_MASK = 0x80,
  90. };
  91. enum
  92. {
  93. USB_CONTROL_ENDPOINT = 0 << 0,
  94. USB_ISOCHRONOUS_ENDPOINT = 1 << 0,
  95. USB_BULK_ENDPOINT = 2 << 0,
  96. USB_INTERRUPT_ENDPOINT = 3 << 0,
  97. USB_NO_SYNCHRONIZATION = 0 << 2,
  98. USB_ASYNCHRONOUS = 1 << 2,
  99. USB_ADAPTIVE = 2 << 2,
  100. USB_SYNCHRONOUS = 3 << 2,
  101. USB_DATA_ENDPOINT = 0 << 4,
  102. USB_FEEDBACK_ENDPOINT = 1 << 4,
  103. USB_IMP_FB_DATA_ENDPOINT = 2 << 4,
  104. };
  105. enum
  106. {
  107. USB_DEVICE_CLASS_MISCELLANEOUS = 0xef,
  108. };
  109. enum
  110. {
  111. USB_DEVICE_SUBCLASS_COMMON = 0x02,
  112. };
  113. enum
  114. {
  115. USB_DEVICE_PROTOCOL_INTERFACE_ASSOCIATION = 0x01,
  116. };
  117. #define USB_CMD(dir, rcpt, type, cmd) \
  118. ((USB_##cmd << 8) | (USB_##dir##_TRANSFER << 7) | \
  119. (USB_##type##_REQUEST << 5) | (USB_##rcpt##_RECIPIENT << 0))
  120. /*- Types -------------------------------------------------------------------*/
  121. typedef struct PACK
  122. {
  123. uint8_t bmRequestType;
  124. uint8_t bRequest;
  125. uint16_t wValue;
  126. uint16_t wIndex;
  127. uint16_t wLength;
  128. } usb_request_t;
  129. typedef struct PACK
  130. {
  131. uint8_t bLength;
  132. uint8_t bDescriptorType;
  133. } usb_descriptor_header_t;
  134. typedef struct PACK
  135. {
  136. uint8_t bLength;
  137. uint8_t bDescriptorType;
  138. uint16_t bcdUSB;
  139. uint8_t bDeviceClass;
  140. uint8_t bDeviceSubClass;
  141. uint8_t bDeviceProtocol;
  142. uint8_t bMaxPacketSize0;
  143. uint16_t idVendor;
  144. uint16_t idProduct;
  145. uint16_t bcdDevice;
  146. uint8_t iManufacturer;
  147. uint8_t iProduct;
  148. uint8_t iSerialNumber;
  149. uint8_t bNumConfigurations;
  150. } usb_device_descriptor_t;
  151. typedef struct PACK
  152. {
  153. uint8_t bLength;
  154. uint8_t bDescriptorType;
  155. uint16_t wTotalLength;
  156. uint8_t bNumInterfaces;
  157. uint8_t bConfigurationValue;
  158. uint8_t iConfiguration;
  159. uint8_t bmAttributes;
  160. uint8_t bMaxPower;
  161. } usb_configuration_descriptor_t;
  162. typedef struct PACK
  163. {
  164. uint8_t bLength;
  165. uint8_t bDescriptorType;
  166. uint8_t bInterfaceNumber;
  167. uint8_t bAlternateSetting;
  168. uint8_t bNumEndpoints;
  169. uint8_t bInterfaceClass;
  170. uint8_t bInterfaceSubClass;
  171. uint8_t bInterfaceProtocol;
  172. uint8_t iInterface;
  173. } usb_interface_descriptor_t;
  174. typedef struct PACK
  175. {
  176. uint8_t bLength;
  177. uint8_t bDescriptorType;
  178. uint8_t bEndpointAddress;
  179. uint8_t bmAttributes;
  180. uint16_t wMaxPacketSize;
  181. uint8_t bInterval;
  182. } usb_endpoint_descriptor_t;
  183. typedef struct PACK
  184. {
  185. uint8_t bLength;
  186. uint8_t bDescriptorType;
  187. uint16_t wLANGID;
  188. } usb_string_descriptor_zero_t;
  189. typedef struct PACK
  190. {
  191. uint8_t bLength;
  192. uint8_t bDescriptorType;
  193. uint16_t bString;
  194. } usb_string_descriptor_t;
  195. typedef struct PACK
  196. {
  197. uint8_t bLength;
  198. uint8_t bDescriptorType;
  199. uint8_t bFirstInterface;
  200. uint8_t bInterfaceCount;
  201. uint8_t bFunctionClass;
  202. uint8_t bFunctionSubClass;
  203. uint8_t bFunctionProtocol;
  204. uint8_t iFunction;
  205. } usb_interface_association_descriptor_t;
  206. /*- Prototypes --------------------------------------------------------------*/
  207. void usb_init(void);
  208. void usb_set_callback(int ep, void (*callback)(int size));
  209. bool usb_handle_standard_request(usb_request_t *request);
  210. void usb_send_callback(int ep);
  211. void usb_recv_callback(int ep, int size);
  212. #endif // _USB_STD_H_