usb_stack.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. This work is licensed under the Creative Commons Attribution 3.0 Unported License.
  3. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/
  4. or send a letter to
  5. Creative Commons,
  6. 171 Second Street,
  7. Suite 300,
  8. San Francisco,
  9. California,
  10. 94105,
  11. USA.
  12. */
  13. #ifndef __USB_STACK_H__
  14. #define __USB_STACK_H__
  15. // JTR v0.2a
  16. #include "../dp_usb/picusb.h" // JTR combined PIC_18F and PIC_24F header. This is the only included header.
  17. #define USB_TOKEN_Mask 0b00111100
  18. #define USB_TOKEN_OUT 0b00000100
  19. #define USB_TOKEN_IN 0b00100100
  20. #define USB_TOKEN_SOF 0b00010100
  21. #define USB_TOKEN_SETUP 0b00110100
  22. #define USB_TOKEN_DATA0 0b00001100
  23. #define USB_TOKEN_DATA1 0b00101100
  24. #define USB_TOKEN_DATA2 0b00011100 /* High speed isochronous endpoints only */
  25. #define USB_TOKEN_MDATA 0b00111100 /* High speed isochronous enpoints and hub devices only */
  26. #define USB_TOKEN_ACK 0b00001000
  27. #define USB_TOKEN_NAK 0b00101000
  28. #define USB_TOKEN_STALL 0b00111000
  29. #define USB_TOKEN_NYET 0b00011000 /* High speed devices only */
  30. #define USB_TOKEN_PRE 0b00110000
  31. #define USB_TOKEN_ERR 0b00110000
  32. #define USB_TOKEN_SPLIT 0b00100000 /* Hub devices only */
  33. #define USB_TOKEN_PING 0b00010000 /* High speed devices only */
  34. /* Descriptor Types */
  35. #define USB_DEVICE_DESCRIPTOR_TYPE 1u
  36. #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2u
  37. #define USB_STRING_DESCRIPTOR_TYPE 3u
  38. #define USB_INTERFACE_DESCRIPTOR_TYPE 4u
  39. #define USB_ENDPOINT_DESCRIPTOR_TYPE 5u
  40. #define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE 6u
  41. #define USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR_TYPE 7u
  42. #define USB_INTERFACE_POWER_DESCRIPTOR_TYPE 8u
  43. #define USB_OTG_DESCRIPTOR_TYPE 9u
  44. #define USB_DEBUG_DESCRIPTOR_TYPE 10u
  45. #define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE 11u
  46. #define USB_bmRequestType 0
  47. #define USB_bRequest 1
  48. #define USB_wValue 2
  49. #define USB_bDescriptorIndex 2
  50. #define USB_wValueHigh 3
  51. #define USB_bDescriptorType 3
  52. #define USB_wIndex 4
  53. #define USB_bInterface 4
  54. #define USB_wIndexHigh 5
  55. #define USB_wLength 6
  56. #define USB_wLengthHigh 7
  57. #define USB_bmRequestType_PhaseMask 0b10000000
  58. #define USB_bmRequestType_H2D 0b00000000
  59. #define USB_bmRequestType_D2H 0b10000000
  60. #define USB_bmRequestType_TypeMask 0b01100000
  61. #define USB_bmRequestType_Standard 0b00000000
  62. #define USB_bmRequestType_Class 0b00100000
  63. #define USB_bmRequestType_Vendor 0b01000000
  64. #define USB_bmRequestType_RecipientMask 0b00000011
  65. #define USB_bmRequestType_Device 0b00000000
  66. #define USB_bmRequestType_Interface 0b00000001
  67. #define USB_bmRequestType_Endpoint 0b00000010
  68. #define USB_bmRequestType_Other 0b00000011
  69. #define USB_REQUEST_GET_STATUS 0
  70. #define USB_REQUEST_CLEAR_FEATURE 1
  71. #define USB_REQUEST_SET_FEATURE 3
  72. #define USB_REQUEST_SET_ADDRESS 5
  73. #define USB_REQUEST_GET_DESCRIPTOR 6
  74. #define USB_REQUEST_SET_DESCRIPTOR 7
  75. #define USB_REQUEST_GET_CONFIGURATION 8
  76. #define USB_REQUEST_SET_CONFIGURATION 9
  77. #define USB_REQUEST_GET_INTERFACE 10
  78. #define USB_REQUEST_SET_INTERFACE 11
  79. #define USB_REQUEST_SYNCH_FRAME 12
  80. typedef enum
  81. {
  82. DETACHED_STATE = 0x00,
  83. ATTACHED_STATE = 0x01,
  84. POWERED_STATE = 0x02,
  85. DEFAULT_STATE = 0x04,
  86. ADR_PENDING_STATE = 0x08,
  87. ADDRESS_STATE = 0x10,
  88. CONFIGURED_STATE = 0x20
  89. } USB_DEVICE_STATE;
  90. /* Structs for defining endpoints */
  91. //typedef void(*)(BYTE *) usb_ep_callback;
  92. typedef void(*usb_handler_t)(void);
  93. typedef struct USB_EP_TYPE {
  94. // usb_uep_t type;
  95. // unsigned int buffer_size;
  96. // BYTE *in_buffer, *out_buffer;
  97. usb_handler_t in_handler, out_handler;
  98. } usb_ep_t;
  99. /* Misc */
  100. #define HIGHB(x) ((x)>>8)
  101. #define LOWB(x) ((x) & 0xFF)
  102. #define XCAT(x,y) x ## y
  103. #define CAT(x,y) XCAT(x,y)
  104. /* Descriptors */
  105. #if USB_NUM_CONFIGURATIONS > 1
  106. #error "More than 1 configuration not supported yet"
  107. #endif
  108. extern BDentry usb_bdt[];
  109. typedef struct USB_DEVICE_REQUEST {
  110. BYTE bmRequestType;
  111. BYTE bRequest;
  112. unsigned int wValue;
  113. unsigned int wIndex;
  114. unsigned int wLength;
  115. } usb_device_request;
  116. extern BYTE trn_status;
  117. extern BDentry *EP0_Outbdp, *EP0_Inbdp;
  118. void usb_init(ROMPTR const BYTE *dev_descriptor,
  119. ROMPTR const BYTE *config_descriptor,
  120. ROMPTR const BYTE *string_descriptor, int num_string_descriptors);
  121. /* Forward Reference Prototypes */
  122. void usb_start(void);
  123. void usb_register_sof_handler(usb_handler_t handler);
  124. void usb_register_class_setup_handler(usb_handler_t handler);
  125. void usb_register_vendor_setup_handler(usb_handler_t handler);
  126. void usb_set_in_handler(int ep, usb_handler_t handler);
  127. void usb_set_out_handler(int ep, usb_handler_t handler);
  128. void usb_handle_transaction(void);
  129. void usb_handler(void);
  130. void usb_RequestError(void);
  131. void USBWakeFromSuspend(void);
  132. void USBSuspend(void);
  133. void usb_ack_dat1(int bdcnt); // JTR added standard for both STD and CLASS
  134. void usb_handle_error(void);
  135. void usb_handle_reset(void);
  136. void usb_handle_setup(void);
  137. void usb_handle_out(void);
  138. void usb_handle_in(void);
  139. void usb_handle_StandardDeviceRequest(BDentry*);
  140. void usb_handle_StandardInterfaceRequest(BDentry*);
  141. void usb_handle_StandardEndpointRequest(BDentry*);
  142. void usb_RequestError(void);
  143. void usb_set_address(void);
  144. void usb_send_rom(void);
  145. void ClearUSBtoDefault(void);
  146. #define usb_unset_in_handler(ep) usb_set_in_handler(ep, (usb_handler_t) 0)
  147. #define usb_unset_out_handler(ep) usb_set_out_handler(ep, (usb_handler_t) 0)
  148. #define flag_usb_RequestError() usbrequesterrorflag = 1
  149. #define USB_LANGID_Chinese_Macau_SAR 0x1404
  150. #define USB_LANGID_Croatian 0x041a
  151. #define USB_LANGID_Czech 0x0405
  152. #define USB_LANGID_Danish 0x0406
  153. #define USB_LANGID_Dutch_Netherlands 0x0413
  154. #define USB_LANGID_Dutch_Belgium 0x0813
  155. #define USB_LANGID_English_United_States 0x0409
  156. #define USB_LANGID_English_United_Kingdom 0x0809
  157. #define USB_LANGID_English_Australian 0x0c09
  158. #define USB_LANGID_English_Canadian 0x1009
  159. #define USB_LANGID_English_New_Zealand 0x1409
  160. #define USB_LANGID_English_Ireland 0x1809
  161. #define USB_LANGID_English_South_Africa 0x1c09
  162. #define USB_LANGID_English_Jamaica 0x2009
  163. #define USB_LANGID_English_Caribbean 0x2409
  164. #define USB_LANGID_English_Belize 0x2809
  165. #define USB_LANGID_English_Trinidad 0x2c09
  166. #define USB_LANGID_English_Zimbabwe 0x3009
  167. #define USB_LANGID_English_Philippines 0x3409
  168. #define USB_LANGID_Estonian 0x0425
  169. #define USB_LANGID_Faeroese 0x0438
  170. #define USB_LANGID_Farsi 0x0429
  171. #define USB_LANGID_Finnish 0x040b
  172. #define USB_LANGID_French_Standard 0x040c
  173. #define USB_LANGID_French_Belgian 0x080c
  174. #define USB_LANGID_French_Canadian 0x0c0c
  175. #define USB_LANGID_French_Switzerland 0x100c
  176. #define USB_LANGID_French_Luxembourg 0x140c
  177. #define USB_LANGID_French_Monaco 0x180c
  178. #define USB_LANGID_Georgian 0x0437
  179. #define USB_LANGID_German_Standard 0x0407
  180. #define USB_LANGID_German_Switzerland 0x0807
  181. #define USB_LANGID_German_Austria 0x0c07
  182. #define USB_LANGID_German_Luxembourg 0x1007
  183. #define USB_LANGID_German_Liechtenstein 0x1407
  184. #define USB_LANGID_Greek 0x0408
  185. #define USB_LANGID_Gujarati 0x0447
  186. #define USB_LANGID_Hebrew 0x040d
  187. #define USB_LANGID_Hindi 0x0439
  188. #define USB_LANGID_Hungarian 0x040e
  189. #define USB_LANGID_Icelandic 0x040f
  190. #define USB_LANGID_Indonesian 0x0421
  191. #define USB_LANGID_Italian_Standard 0x0410
  192. #define USB_LANGID_Italian_Switzerland 0x0810
  193. #define USB_LANGID_Japanese 0x0411
  194. #define USB_LANGID_Kannada 0x044b
  195. #define USB_LANGID_Kashmiri_India 0x0860
  196. #define USB_LANGID_Kazakh 0x043f
  197. #define USB_LANGID_Konkani 0x0457
  198. #define USB_LANGID_Korean 0x0412
  199. #define USB_LANGID_Korean_Johab 0x0812
  200. #define USB_LANGID_Latvian 0x0426
  201. #define USB_LANGID_Lithuanian 0x0427
  202. #define USB_LANGID_Lithuanian_Classic 0x0827
  203. #define USB_LANGID_Macedonian 0x042f
  204. #define USB_LANGID_Malay_Malaysian 0x043e
  205. #define USB_LANGID_Malay_Brunei_Darussalam 0x083e
  206. #define USB_LANGID_Malayalam 0x044c
  207. #define USB_LANGID_Manipuri 0x0458
  208. #define USB_LANGID_Marathi 0x044e
  209. #define USB_LANGID_Nepali_India 0x0861
  210. #define USB_LANGID_Norwegian_Bokmal 0x0414
  211. #define USB_LANGID_Norwegian_Nynorsk 0x0814
  212. #define USB_LANGID_Oriya 0x0448
  213. #define USB_LANGID_Polish 0x0415
  214. #define USB_LANGID_Portuguese_Brazil 0x0416
  215. #define USB_LANGID_Portuguese_Standard 0x0816
  216. #define USB_LANGID_Punjabi 0x0446
  217. #define USB_LANGID_Romanian 0x0418
  218. #define USB_LANGID_Russian 0x0419
  219. #define USB_LANGID_Sanskrit 0x044f
  220. #define USB_LANGID_Serbian_Cyrillic 0x0c1a
  221. #define USB_LANGID_Serbian_Latin 0x081a
  222. #define USB_LANGID_Sindhi 0x0459
  223. #define USB_LANGID_Slovak 0x041b
  224. #define USB_LANGID_Slovenian 0x0424
  225. #define USB_LANGID_Spanish_Traditional_Sort 0x040a
  226. #define USB_LANGID_Spanish_Mexican 0x080a
  227. #define USB_LANGID_Spanish_Modern_Sort 0x0c0a
  228. #define USB_LANGID_Spanish_Guatemala 0x100a
  229. #define USB_LANGID_Spanish_Costa_Rica 0x140a
  230. #define USB_LANGID_Spanish_Panama 0x180a
  231. #define USB_LANGID_Spanish_Dominican_Republic 0x1c0a
  232. #define USB_LANGID_Spanish_Venezuela 0x200a
  233. #define USB_LANGID_Spanish_Colombia 0x240a
  234. #define USB_LANGID_Spanish_Peru 0x280a
  235. #define USB_LANGID_Spanish_Argentina 0x2c0a
  236. #define USB_LANGID_Spanish_Ecuador 0x300a
  237. #define USB_LANGID_Spanish_Chile 0x340a
  238. #define USB_LANGID_Spanish_Uruguay 0x380a
  239. #define USB_LANGID_Spanish_Paraguay 0x3c0a
  240. #define USB_LANGID_Spanish_Bolivia 0x400a
  241. #define USB_LANGID_Spanish_El_Salvador 0x440a
  242. #define USB_LANGID_Spanish_Honduras 0x480a
  243. #define USB_LANGID_Spanish_Nicaragua 0x4c0a
  244. #define USB_LANGID_Spanish_Puerto_Rico 0x500a
  245. #define USB_LANGID_Sutu 0x0430
  246. #define USB_LANGID_Swahili_Kenya 0x0441
  247. #define USB_LANGID_Swedish 0x041d
  248. #define USB_LANGID_Swedish_Finland 0x081d
  249. #define USB_LANGID_Tamil 0x0449
  250. #define USB_LANGID_Tatar_Tatarstan 0x0444
  251. #define USB_LANGID_Telugu 0x044a
  252. #define USB_LANGID_Thai 0x041e
  253. #define USB_LANGID_Turkish 0x041f
  254. #define USB_LANGID_Ukrainian 0x0422
  255. #define USB_LANGID_Urdu_Pakistan 0x0420
  256. #define USB_LANGID_Urdu_India 0x0820
  257. #define USB_LANGID_Uzbek_Latin 0x0443
  258. #define USB_LANGID_Uzbek_Cyrillic 0x0843
  259. #define USB_LANGID_Vietnamese 0x042a
  260. #define USB_LANGID_HID_UDD 0x04ff
  261. #define USB_LANGID_HID_Vendor1 0xf0ff
  262. #define USB_LANGID_HID_Vendor2 0xf4ff
  263. #define USB_LANGID_HID_Vendor3 0xf8ff
  264. #define USB_LANGID_HID_Vendor4 0xfcff
  265. #endif /* USB_STACK_H */