My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_protocol.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * \file
  3. *
  4. * \brief USB protocol definitions.
  5. *
  6. * This file contains the USB definitions and data structures provided by the
  7. * USB 2.0 specification.
  8. *
  9. * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
  10. *
  11. * \asf_license_start
  12. *
  13. * \page License
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * 3. The name of Atmel may not be used to endorse or promote products derived
  26. * from this software without specific prior written permission.
  27. *
  28. * 4. This software may only be redistributed and used in connection with an
  29. * Atmel microcontroller product.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  32. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  34. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  35. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  40. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * \asf_license_stop
  44. *
  45. */
  46. /*
  47. * Support and FAQ: visit <a href="https://www.atmel.com/design-support/">Atmel Support</a>
  48. */
  49. #ifndef _USB_PROTOCOL_H_
  50. #define _USB_PROTOCOL_H_
  51. /**
  52. * \ingroup usb_group
  53. * \defgroup usb_protocol_group USB Protocol Definitions
  54. *
  55. * This module defines constants and data structures provided by the USB
  56. * 2.0 specification.
  57. *
  58. * @{
  59. */
  60. //! Value for field bcdUSB
  61. #define USB_V2_0 0x0200 //!< USB Specification version 2.00
  62. #define USB_V2_1 0x0201 //!< USB Specification version 2.01
  63. /*! \name Generic definitions (Class, subclass and protocol)
  64. */
  65. //! @{
  66. #define NO_CLASS 0x00
  67. #define CLASS_VENDOR_SPECIFIC 0xFF
  68. #define NO_SUBCLASS 0x00
  69. #define NO_PROTOCOL 0x00
  70. //! @}
  71. //! \name IAD (Interface Association Descriptor) constants
  72. //! @{
  73. #define CLASS_IAD 0xEF
  74. #define SUB_CLASS_IAD 0x02
  75. #define PROTOCOL_IAD 0x01
  76. //! @}
  77. /**
  78. * \brief USB request data transfer direction (bmRequestType)
  79. */
  80. #define USB_REQ_DIR_OUT (0<<7) //!< Host to device
  81. #define USB_REQ_DIR_IN (1<<7) //!< Device to host
  82. #define USB_REQ_DIR_MASK (1<<7) //!< Mask
  83. /**
  84. * \brief USB request types (bmRequestType)
  85. */
  86. #define USB_REQ_TYPE_STANDARD (0<<5) //!< Standard request
  87. #define USB_REQ_TYPE_CLASS (1<<5) //!< Class-specific request
  88. #define USB_REQ_TYPE_VENDOR (2<<5) //!< Vendor-specific request
  89. #define USB_REQ_TYPE_MASK (3<<5) //!< Mask
  90. /**
  91. * \brief USB recipient codes (bmRequestType)
  92. */
  93. #define USB_REQ_RECIP_DEVICE (0<<0) //!< Recipient device
  94. #define USB_REQ_RECIP_INTERFACE (1<<0) //!< Recipient interface
  95. #define USB_REQ_RECIP_ENDPOINT (2<<0) //!< Recipient endpoint
  96. #define USB_REQ_RECIP_OTHER (3<<0) //!< Recipient other
  97. #define USB_REQ_RECIP_MASK (0x1F) //!< Mask
  98. /**
  99. * \brief Standard USB requests (bRequest)
  100. */
  101. enum usb_reqid {
  102. USB_REQ_GET_STATUS = 0,
  103. USB_REQ_CLEAR_FEATURE = 1,
  104. USB_REQ_SET_FEATURE = 3,
  105. USB_REQ_SET_ADDRESS = 5,
  106. USB_REQ_GET_DESCRIPTOR = 6,
  107. USB_REQ_SET_DESCRIPTOR = 7,
  108. USB_REQ_GET_CONFIGURATION = 8,
  109. USB_REQ_SET_CONFIGURATION = 9,
  110. USB_REQ_GET_INTERFACE = 10,
  111. USB_REQ_SET_INTERFACE = 11,
  112. USB_REQ_SYNCH_FRAME = 12,
  113. };
  114. /**
  115. * \brief Standard USB device status flags
  116. *
  117. */
  118. enum usb_device_status {
  119. USB_DEV_STATUS_BUS_POWERED = 0,
  120. USB_DEV_STATUS_SELF_POWERED = 1,
  121. USB_DEV_STATUS_REMOTEWAKEUP = 2
  122. };
  123. /**
  124. * \brief Standard USB Interface status flags
  125. *
  126. */
  127. enum usb_interface_status {
  128. USB_IFACE_STATUS_RESERVED = 0
  129. };
  130. /**
  131. * \brief Standard USB endpoint status flags
  132. *
  133. */
  134. enum usb_endpoint_status {
  135. USB_EP_STATUS_HALTED = 1,
  136. };
  137. /**
  138. * \brief Standard USB device feature flags
  139. *
  140. * \note valid for SetFeature request.
  141. */
  142. enum usb_device_feature {
  143. USB_DEV_FEATURE_REMOTE_WAKEUP = 1, //!< Remote wakeup enabled
  144. USB_DEV_FEATURE_TEST_MODE = 2, //!< USB test mode
  145. USB_DEV_FEATURE_OTG_B_HNP_ENABLE = 3,
  146. USB_DEV_FEATURE_OTG_A_HNP_SUPPORT = 4,
  147. USB_DEV_FEATURE_OTG_A_ALT_HNP_SUPPORT = 5
  148. };
  149. /**
  150. * \brief Test Mode possible on HS USB device
  151. *
  152. * \note valid for USB_DEV_FEATURE_TEST_MODE request.
  153. */
  154. enum usb_device_hs_test_mode {
  155. USB_DEV_TEST_MODE_J = 1,
  156. USB_DEV_TEST_MODE_K = 2,
  157. USB_DEV_TEST_MODE_SE0_NAK = 3,
  158. USB_DEV_TEST_MODE_PACKET = 4,
  159. USB_DEV_TEST_MODE_FORCE_ENABLE = 5,
  160. };
  161. /**
  162. * \brief Standard USB endpoint feature/status flags
  163. */
  164. enum usb_endpoint_feature {
  165. USB_EP_FEATURE_HALT = 0,
  166. };
  167. /**
  168. * \brief Standard USB Test Mode Selectors
  169. */
  170. enum usb_test_mode_selector {
  171. USB_TEST_J = 0x01,
  172. USB_TEST_K = 0x02,
  173. USB_TEST_SE0_NAK = 0x03,
  174. USB_TEST_PACKET = 0x04,
  175. USB_TEST_FORCE_ENABLE = 0x05,
  176. };
  177. /**
  178. * \brief Standard USB descriptor types
  179. */
  180. enum usb_descriptor_type {
  181. USB_DT_DEVICE = 1,
  182. USB_DT_CONFIGURATION = 2,
  183. USB_DT_STRING = 3,
  184. USB_DT_INTERFACE = 4,
  185. USB_DT_ENDPOINT = 5,
  186. USB_DT_DEVICE_QUALIFIER = 6,
  187. USB_DT_OTHER_SPEED_CONFIGURATION = 7,
  188. USB_DT_INTERFACE_POWER = 8,
  189. USB_DT_OTG = 9,
  190. USB_DT_IAD = 0x0B,
  191. USB_DT_BOS = 0x0F,
  192. USB_DT_DEVICE_CAPABILITY = 0x10,
  193. };
  194. /**
  195. * \brief USB Device Capability types
  196. */
  197. enum usb_capability_type {
  198. USB_DC_USB20_EXTENSION = 0x02,
  199. };
  200. /**
  201. * \brief USB Device Capability - USB 2.0 Extension
  202. * To fill bmAttributes field of usb_capa_ext_desc_t structure.
  203. */
  204. enum usb_capability_extension_attr {
  205. USB_DC_EXT_LPM = 0x00000002,
  206. };
  207. #define HIRD_50_US 0
  208. #define HIRD_125_US 1
  209. #define HIRD_200_US 2
  210. #define HIRD_275_US 3
  211. #define HIRD_350_US 4
  212. #define HIRD_425_US 5
  213. #define HIRD_500_US 6
  214. #define HIRD_575_US 7
  215. #define HIRD_650_US 8
  216. #define HIRD_725_US 9
  217. #define HIRD_800_US 10
  218. #define HIRD_875_US 11
  219. #define HIRD_950_US 12
  220. #define HIRD_1025_US 13
  221. #define HIRD_1100_US 14
  222. #define HIRD_1175_US 15
  223. /** Fields definition from a LPM TOKEN */
  224. #define USB_LPM_ATTRIBUT_BLINKSTATE_MASK (0xF << 0)
  225. #define USB_LPM_ATTRIBUT_FIRD_MASK (0xF << 4)
  226. #define USB_LPM_ATTRIBUT_REMOTEWAKE_MASK (1 << 8)
  227. #define USB_LPM_ATTRIBUT_BLINKSTATE(value) ((value & 0xF) << 0)
  228. #define USB_LPM_ATTRIBUT_FIRD(value) ((value & 0xF) << 4)
  229. #define USB_LPM_ATTRIBUT_REMOTEWAKE(value) ((value & 1) << 8)
  230. #define USB_LPM_ATTRIBUT_BLINKSTATE_L1 USB_LPM_ATTRIBUT_BLINKSTATE(1)
  231. /**
  232. * \brief Standard USB endpoint transfer types
  233. */
  234. enum usb_ep_type {
  235. USB_EP_TYPE_CONTROL = 0x00,
  236. USB_EP_TYPE_ISOCHRONOUS = 0x01,
  237. USB_EP_TYPE_BULK = 0x02,
  238. USB_EP_TYPE_INTERRUPT = 0x03,
  239. USB_EP_TYPE_MASK = 0x03,
  240. };
  241. /**
  242. * \brief Standard USB language IDs for string descriptors
  243. */
  244. enum usb_langid {
  245. USB_LANGID_EN_US = 0x0409, //!< English (United States)
  246. };
  247. /**
  248. * \brief Mask selecting the index part of an endpoint address
  249. */
  250. #define USB_EP_ADDR_MASK 0x0F
  251. //! \brief USB address identifier
  252. typedef uint8_t usb_add_t;
  253. /**
  254. * \brief Endpoint transfer direction is IN
  255. */
  256. #define USB_EP_DIR_IN 0x80
  257. /**
  258. * \brief Endpoint transfer direction is OUT
  259. */
  260. #define USB_EP_DIR_OUT 0x00
  261. //! \brief Endpoint identifier
  262. typedef uint8_t usb_ep_t;
  263. /**
  264. * \brief Maximum length in bytes of a USB descriptor
  265. *
  266. * The maximum length of a USB descriptor is limited by the 8-bit
  267. * bLength field.
  268. */
  269. #define USB_MAX_DESC_LEN 255
  270. /*
  271. * 2-byte alignment requested for all USB structures.
  272. */
  273. COMPILER_PACK_SET(1)
  274. /**
  275. * \brief A USB Device SETUP request
  276. *
  277. * The data payload of SETUP packets always follows this structure.
  278. */
  279. typedef struct {
  280. uint8_t bmRequestType;
  281. uint8_t bRequest;
  282. le16_t wValue;
  283. le16_t wIndex;
  284. le16_t wLength;
  285. } usb_setup_req_t;
  286. /**
  287. * \brief Standard USB device descriptor structure
  288. */
  289. typedef struct {
  290. uint8_t bLength;
  291. uint8_t bDescriptorType;
  292. le16_t bcdUSB;
  293. uint8_t bDeviceClass;
  294. uint8_t bDeviceSubClass;
  295. uint8_t bDeviceProtocol;
  296. uint8_t bMaxPacketSize0;
  297. le16_t idVendor;
  298. le16_t idProduct;
  299. le16_t bcdDevice;
  300. uint8_t iManufacturer;
  301. uint8_t iProduct;
  302. uint8_t iSerialNumber;
  303. uint8_t bNumConfigurations;
  304. } usb_dev_desc_t;
  305. /**
  306. * \brief Standard USB device qualifier descriptor structure
  307. *
  308. * This descriptor contains information about the device when running at
  309. * the "other" speed (i.e. if the device is currently operating at high
  310. * speed, this descriptor can be used to determine what would change if
  311. * the device was operating at full speed.)
  312. */
  313. typedef struct {
  314. uint8_t bLength;
  315. uint8_t bDescriptorType;
  316. le16_t bcdUSB;
  317. uint8_t bDeviceClass;
  318. uint8_t bDeviceSubClass;
  319. uint8_t bDeviceProtocol;
  320. uint8_t bMaxPacketSize0;
  321. uint8_t bNumConfigurations;
  322. uint8_t bReserved;
  323. } usb_dev_qual_desc_t;
  324. /**
  325. * \brief USB Device BOS descriptor structure
  326. *
  327. * The BOS descriptor (Binary device Object Store) defines a root
  328. * descriptor that is similar to the configuration descriptor, and is
  329. * the base descriptor for accessing a family of related descriptors.
  330. * A host can read a BOS descriptor and learn from the wTotalLength field
  331. * the entire size of the device-level descriptor set, or it can read in
  332. * the entire BOS descriptor set of device capabilities.
  333. * The host accesses this descriptor using the GetDescriptor() request.
  334. * The descriptor type in the GetDescriptor() request is set to BOS.
  335. */
  336. typedef struct {
  337. uint8_t bLength;
  338. uint8_t bDescriptorType;
  339. le16_t wTotalLength;
  340. uint8_t bNumDeviceCaps;
  341. } usb_dev_bos_desc_t;
  342. /**
  343. * \brief USB Device Capabilities - USB 2.0 Extension Descriptor structure
  344. *
  345. * Defines the set of USB 1.1-specific device level capabilities.
  346. */
  347. typedef struct {
  348. uint8_t bLength;
  349. uint8_t bDescriptorType;
  350. uint8_t bDevCapabilityType;
  351. le32_t bmAttributes;
  352. } usb_dev_capa_ext_desc_t;
  353. /**
  354. * \brief USB Device LPM Descriptor structure
  355. *
  356. * The BOS descriptor and capabilities descriptors for LPM.
  357. */
  358. typedef struct {
  359. usb_dev_bos_desc_t bos;
  360. usb_dev_capa_ext_desc_t capa_ext;
  361. } usb_dev_lpm_desc_t;
  362. /**
  363. * \brief Standard USB Interface Association Descriptor structure
  364. */
  365. typedef struct {
  366. uint8_t bLength; //!< size of this descriptor in bytes
  367. uint8_t bDescriptorType; //!< INTERFACE descriptor type
  368. uint8_t bFirstInterface; //!< Number of interface
  369. uint8_t bInterfaceCount; //!< value to select alternate setting
  370. uint8_t bFunctionClass; //!< Class code assigned by the USB
  371. uint8_t bFunctionSubClass;//!< Sub-class code assigned by the USB
  372. uint8_t bFunctionProtocol;//!< Protocol code assigned by the USB
  373. uint8_t iFunction; //!< Index of string descriptor
  374. } usb_association_desc_t;
  375. /**
  376. * \brief Standard USB configuration descriptor structure
  377. */
  378. typedef struct {
  379. uint8_t bLength;
  380. uint8_t bDescriptorType;
  381. le16_t wTotalLength;
  382. uint8_t bNumInterfaces;
  383. uint8_t bConfigurationValue;
  384. uint8_t iConfiguration;
  385. uint8_t bmAttributes;
  386. uint8_t bMaxPower;
  387. } usb_conf_desc_t;
  388. #define USB_CONFIG_ATTR_MUST_SET (1 << 7) //!< Must always be set
  389. #define USB_CONFIG_ATTR_BUS_POWERED (0 << 6) //!< Bus-powered
  390. #define USB_CONFIG_ATTR_SELF_POWERED (1 << 6) //!< Self-powered
  391. #define USB_CONFIG_ATTR_REMOTE_WAKEUP (1 << 5) //!< remote wakeup supported
  392. #define USB_CONFIG_MAX_POWER(ma) (((ma) + 1) / 2) //!< Max power in mA
  393. /**
  394. * \brief Standard USB association descriptor structure
  395. */
  396. typedef struct {
  397. uint8_t bLength; //!< Size of this descriptor in bytes
  398. uint8_t bDescriptorType; //!< Interface descriptor type
  399. uint8_t bFirstInterface; //!< Number of interface
  400. uint8_t bInterfaceCount; //!< value to select alternate setting
  401. uint8_t bFunctionClass; //!< Class code assigned by the USB
  402. uint8_t bFunctionSubClass; //!< Sub-class code assigned by the USB
  403. uint8_t bFunctionProtocol; //!< Protocol code assigned by the USB
  404. uint8_t iFunction; //!< Index of string descriptor
  405. } usb_iad_desc_t;
  406. /**
  407. * \brief Standard USB interface descriptor structure
  408. */
  409. typedef struct {
  410. uint8_t bLength;
  411. uint8_t bDescriptorType;
  412. uint8_t bInterfaceNumber;
  413. uint8_t bAlternateSetting;
  414. uint8_t bNumEndpoints;
  415. uint8_t bInterfaceClass;
  416. uint8_t bInterfaceSubClass;
  417. uint8_t bInterfaceProtocol;
  418. uint8_t iInterface;
  419. } usb_iface_desc_t;
  420. /**
  421. * \brief Standard USB endpoint descriptor structure
  422. */
  423. typedef struct {
  424. uint8_t bLength;
  425. uint8_t bDescriptorType;
  426. uint8_t bEndpointAddress;
  427. uint8_t bmAttributes;
  428. le16_t wMaxPacketSize;
  429. uint8_t bInterval;
  430. } usb_ep_desc_t;
  431. /**
  432. * \brief A standard USB string descriptor structure
  433. */
  434. typedef struct {
  435. uint8_t bLength;
  436. uint8_t bDescriptorType;
  437. } usb_str_desc_t;
  438. typedef struct {
  439. usb_str_desc_t desc;
  440. le16_t string[1];
  441. } usb_str_lgid_desc_t;
  442. COMPILER_PACK_RESET()
  443. //! @}
  444. #endif /* _USB_PROTOCOL_H_ */