usb.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef USBSTACK_H_
  2. #define USBSTACK_H_
  3. /*
  4. * Tiny USB stack
  5. *
  6. * Copyright (C) 2009 Michael Buesch <m@bues.ch>
  7. *
  8. * USB data structure and constant definitions
  9. * taken from libusb-0.1 and the Linux kernel.
  10. * Copyright (c) 2000-2003 Johannes Erdfelt and others
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include "usb_config.h"
  22. #include <stdint.h>
  23. #include <avr/pgmspace.h>
  24. #if USBCFG_ARCH_BE
  25. # define le16_to_cpu(value) usb_swap16(value)
  26. # define le32_to_cpu(value) usb_swap32(value)
  27. # define cpu_to_le16(value) usb_swap16(value)
  28. # define cpu_to_le32(value) usb_swap32(value)
  29. #else
  30. # define le16_to_cpu(value) (value)
  31. # define le32_to_cpu(value) (value)
  32. # define cpu_to_le16(value) (value)
  33. # define cpu_to_le32(value) (value)
  34. #endif
  35. #define usb_swap16(value) (((value & 0xFF00) >> 8) | \
  36. ((value & 0x00FF) << 8))
  37. #define usb_swap32(value) (((value & 0xFF000000) >> 24) | \
  38. ((value & 0x00FF0000) >> 8) | \
  39. ((value & 0x0000FF00) << 8) | \
  40. ((value & 0x000000FF) << 24))
  41. #undef DBG
  42. #if DEBUG
  43. # define DBG(x) x
  44. #else
  45. # define DBG(x) /* nothing */
  46. #endif
  47. struct usb_device_descriptor {
  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_config_descriptor {
  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. /* from config descriptor bmAttributes */
  74. #define USB_CONFIG_ATT_ONE (1 << 7) /* must be set */
  75. #define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */
  76. #define USB_CONFIG_ATT_WAKEUP (1 << 5) /* can wakeup */
  77. #define USB_CONFIG_ATT_BATTERY (1 << 4) /* battery powered */
  78. struct usb_string_descriptor {
  79. uint8_t bLength;
  80. uint8_t bDescriptorType;
  81. uint8_t string[0];
  82. } __attribute__ ((packed));
  83. struct usb_interface_descriptor {
  84. uint8_t bLength;
  85. uint8_t bDescriptorType;
  86. uint8_t bInterfaceNumber;
  87. uint8_t bAlternateSetting;
  88. uint8_t bNumEndpoints;
  89. uint8_t bInterfaceClass;
  90. uint8_t bInterfaceSubClass;
  91. uint8_t bInterfaceProtocol;
  92. uint8_t iInterface;
  93. } __attribute__ ((packed));
  94. struct usb_endpoint_descriptor {
  95. uint8_t bLength;
  96. uint8_t bDescriptorType;
  97. uint8_t bEndpointAddress;
  98. uint8_t bmAttributes;
  99. uint16_t wMaxPacketSize;
  100. uint8_t bInterval;
  101. } __attribute__ ((packed));
  102. struct usb_audio_endpoint_descriptor {
  103. uint8_t bLength;
  104. uint8_t bDescriptorType;
  105. uint8_t bEndpointAddress;
  106. uint8_t bmAttributes;
  107. uint16_t wMaxPacketSize;
  108. uint8_t bInterval;
  109. uint8_t bRefresh;
  110. uint8_t bSynchAddress;
  111. } __attribute__ ((packed));
  112. #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
  113. #define USB_ENDPOINT_XFER_CONTROL 0
  114. #define USB_ENDPOINT_XFER_ISOC 1
  115. #define USB_ENDPOINT_XFER_BULK 2
  116. #define USB_ENDPOINT_XFER_INT 3
  117. #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
  118. struct usb_ctrl {
  119. uint8_t bRequestType;
  120. uint8_t bRequest;
  121. uint16_t wValue;
  122. uint16_t wIndex;
  123. uint16_t wLength;
  124. } __attribute__ ((packed));
  125. /* Device and/or Interface Class codes */
  126. #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
  127. #define USB_CLASS_AUDIO 1
  128. #define USB_CLASS_COMM 2
  129. #define USB_CLASS_HID 3
  130. #define USB_CLASS_PRINTER 7
  131. #define USB_CLASS_PTP 6
  132. #define USB_CLASS_MASS_STORAGE 8
  133. #define USB_CLASS_HUB 9
  134. #define USB_CLASS_DATA 10
  135. #define USB_CLASS_VENDOR_SPEC 0xff
  136. /* Descriptor types */
  137. #define USB_DT_DEVICE 0x01
  138. #define USB_DT_CONFIG 0x02
  139. #define USB_DT_STRING 0x03
  140. #define USB_DT_INTERFACE 0x04
  141. #define USB_DT_ENDPOINT 0x05
  142. #define USB_DT_HID 0x21
  143. #define USB_DT_REPORT 0x22
  144. #define USB_DT_PHYSICAL 0x23
  145. #define USB_DT_HUB 0x29
  146. /* Descriptor sizes per descriptor type */
  147. #define USB_DT_DEVICE_SIZE 18
  148. #define USB_DT_CONFIG_SIZE 9
  149. #define USB_DT_INTERFACE_SIZE 9
  150. #define USB_DT_ENDPOINT_SIZE 7
  151. #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
  152. #define USB_DT_HUB_NONVAR_SIZE 7
  153. /* Request types */
  154. #define USB_REQ_GET_STATUS 0x00
  155. #define USB_REQ_CLEAR_FEATURE 0x01
  156. /* 0x02 is reserved */
  157. #define USB_REQ_SET_FEATURE 0x03
  158. /* 0x04 is reserved */
  159. #define USB_REQ_SET_ADDRESS 0x05
  160. #define USB_REQ_GET_DESCRIPTOR 0x06
  161. #define USB_REQ_SET_DESCRIPTOR 0x07
  162. #define USB_REQ_GET_CONFIGURATION 0x08
  163. #define USB_REQ_SET_CONFIGURATION 0x09
  164. #define USB_REQ_GET_INTERFACE 0x0A
  165. #define USB_REQ_SET_INTERFACE 0x0B
  166. #define USB_REQ_SYNCH_FRAME 0x0C
  167. #define USB_TYPE_STANDARD (0x00 << 5)
  168. #define USB_TYPE_CLASS (0x01 << 5)
  169. #define USB_TYPE_VENDOR (0x02 << 5)
  170. #define USB_TYPE_RESERVED (0x03 << 5)
  171. #define USB_TYPE_MASK (0x03 << 5)
  172. #define USB_RECIP_DEVICE 0x00
  173. #define USB_RECIP_INTERFACE 0x01
  174. #define USB_RECIP_ENDPOINT 0x02
  175. #define USB_RECIP_OTHER 0x03
  176. #define USB_RECIP_MASK 0x03
  177. #define USB_ENDPOINT_IN 0x80
  178. #define USB_ENDPOINT_OUT 0x00
  179. static inline uint8_t usb_ep_is_in(uint8_t ep)
  180. {
  181. return ep & USB_ENDPOINT_IN;
  182. }
  183. static inline uint8_t usb_ep_is_out(uint8_t ep)
  184. {
  185. return !(ep & USB_ENDPOINT_IN);
  186. }
  187. static inline uint8_t usb_ctrl_is_in(const struct usb_ctrl *ctrl)
  188. {
  189. return (ctrl->bRequestType & USB_ENDPOINT_IN);
  190. }
  191. static inline uint8_t usb_ctrl_is_out(const struct usb_ctrl *ctrl)
  192. {
  193. return !(ctrl->bRequestType & USB_ENDPOINT_IN);
  194. }
  195. /* USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
  196. * are read as a bit array returned by USB_REQ_GET_STATUS. (So there
  197. * are at most sixteen features of each type.) Hubs may also support a
  198. * new USB_REQ_TEST_AND_SET_FEATURE to put ports into L1 suspend. */
  199. #define USB_DEVICE_SELF_POWERED 0 /* (read only) */
  200. #define USB_DEVICE_REMOTE_WAKEUP 1 /* dev may initiate wakeup */
  201. #define USB_DEVICE_TEST_MODE 2 /* (wired high speed only) */
  202. #define USB_DEVICE_BATTERY 2 /* (wireless) */
  203. #define USB_DEVICE_B_HNP_ENABLE 3 /* (otg) dev may initiate HNP */
  204. #define USB_DEVICE_WUSB_DEVICE 3 /* (wireless)*/
  205. #define USB_DEVICE_A_HNP_SUPPORT 4 /* (otg) RH port supports HNP */
  206. #define USB_DEVICE_A_ALT_HNP_SUPPORT 5 /* (otg) other RH port does */
  207. #define USB_DEVICE_DEBUG_MODE 6 /* (special devices only) */
  208. #define USB_ENDPOINT_HALT 0 /* IN/OUT will STALL */
  209. /*** Device driver calls ***/
  210. /** usb_reset - Reset the USB statemachine.
  211. * Called by the lowlevel device driver. */
  212. void usb_reset(void);
  213. /** usb_control_rx - Received setup token on control-EP.
  214. * Returns enum usb_rx_returncode.
  215. * Called by the lowlevel device driver. */
  216. uint8_t usb_control_setup_rx(struct usb_ctrl *ctl);
  217. /** usb_control_rx - Received data on control-EP.
  218. * Returns enum usb_rx_returncode.
  219. * Called by the lowlevel device driver. */
  220. uint8_t usb_control_rx(void *data, uint8_t size);
  221. #define USB_TX_POLL_NONE 0xFFu
  222. /** usb_control_tx_poll - Poll TX data on control-EP.
  223. * Returns the number of octets or USB_TX_POLL_NONE on error.
  224. * Called by the lowlevel device driver. */
  225. uint8_t usb_control_tx_poll(void **data, uint8_t chunksize);
  226. /** usb_ep1_rx - Received data on EP1.
  227. * Returns enum usb_rx_returncode.
  228. * Called by the lowlevel device driver. */
  229. uint8_t usb_ep1_rx(void *data, uint8_t size);
  230. /** usb_ep1_tx_poll - Poll TX data on EP1.
  231. * Returns the number of octets or USB_TX_POLL_NONE on error.
  232. * Called by the lowlevel device driver. */
  233. uint8_t usb_ep1_tx_poll(void **data, uint8_t chunksize);
  234. /** usb_ep2_rx - Received data on EP2.
  235. * Returns enum usb_rx_returncode.
  236. * Called by the lowlevel device driver. */
  237. uint8_t usb_ep2_rx(void *data, uint8_t size);
  238. /** usb_ep2_tx_poll - Poll TX data on EP2.
  239. * Returns the number of octets or USB_TX_POLL_NONE on error.
  240. * Called by the lowlevel device driver. */
  241. uint8_t usb_ep2_tx_poll(void **data, uint8_t chunksize);
  242. /** enum usb_rx_returncode - Returncode to usb_*_rx() */
  243. enum usb_rx_returncode {
  244. USB_RX_DONE, /* Everything is done */
  245. USB_RX_ERROR, /* An error occured while processing the frame */
  246. };
  247. /*** Callbacks to device driver - Defined in the lowlevel driver ***/
  248. extern void usb_set_address(uint8_t address);
  249. extern void usb_enable_endpoints(uint8_t enable);
  250. extern void usb_stall_endpoint(uint8_t ep);
  251. extern void usb_unstall_endpoint(uint8_t ep);
  252. extern uint8_t usb_endpoint_is_stalled(uint8_t ep);
  253. #endif /* USBSTACK_H_ */