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.

USBHost.h 13KB

7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef USB_HOST_TEENSY36_
  24. #define USB_HOST_TEENSY36_
  25. #include <stdint.h>
  26. /************************************************/
  27. /* Data Structure Definitions */
  28. /************************************************/
  29. class USBHost;
  30. class USBDriver;
  31. typedef struct Device_struct Device_t;
  32. typedef struct Pipe_struct Pipe_t;
  33. typedef struct Transfer_struct Transfer_t;
  34. // setup_t holds the 8 byte USB SETUP packet data.
  35. // These unions & structs allow convenient access to
  36. // the setup fields.
  37. typedef union {
  38. struct {
  39. union {
  40. struct {
  41. uint8_t bmRequestType;
  42. uint8_t bRequest;
  43. };
  44. uint16_t wRequestAndType;
  45. };
  46. uint16_t wValue;
  47. uint16_t wIndex;
  48. uint16_t wLength;
  49. };
  50. struct {
  51. uint32_t word1;
  52. uint32_t word2;
  53. };
  54. } setup_t;
  55. // Device_t holds all the information about a USB device
  56. struct Device_struct {
  57. Pipe_t *control_pipe;
  58. Pipe_t *data_pipes;
  59. Device_t *next;
  60. USBDriver *drivers;
  61. uint8_t speed; // 0=12, 1=1.5, 2=480 Mbit/sec
  62. uint8_t address;
  63. uint8_t hub_address;
  64. uint8_t hub_port;
  65. uint8_t enum_state;
  66. uint8_t bDeviceClass;
  67. uint8_t bDeviceSubClass;
  68. uint8_t bDeviceProtocol;
  69. uint8_t bmAttributes;
  70. uint8_t bMaxPower;
  71. uint16_t idVendor;
  72. uint16_t idProduct;
  73. uint16_t LanguageID;
  74. };
  75. // Pipe_t holes all information about each USB endpoint/pipe
  76. // The first half is an EHCI QH structure for the pipe.
  77. struct Pipe_struct {
  78. // Queue Head (QH), EHCI page 46-50
  79. struct { // must be aligned to 32 byte boundary
  80. volatile uint32_t horizontal_link;
  81. volatile uint32_t capabilities[2];
  82. volatile uint32_t current;
  83. volatile uint32_t next;
  84. volatile uint32_t alt_next;
  85. volatile uint32_t token;
  86. volatile uint32_t buffer[5];
  87. } qh;
  88. Device_t *device;
  89. uint8_t type; // 0=control, 1=isochronous, 2=bulk, 3=interrupt
  90. uint8_t direction; // 0=out, 1=in (changes for control, others fixed)
  91. uint8_t start_mask; // TODO: is this redundant?
  92. uint8_t complete_mask; // TODO: is this redundant?
  93. Pipe_t *next;
  94. void (*callback_function)(const Transfer_t *);
  95. uint16_t periodic_interval;
  96. uint16_t periodic_offset; // TODO: is this redundant?
  97. uint32_t unused1;
  98. uint32_t unused2;
  99. uint32_t unused3;
  100. uint32_t unused4;
  101. uint32_t unused5;
  102. uint32_t unused6;
  103. uint32_t unused7;
  104. };
  105. // Transfer_t represents a single transaction on the USB bus.
  106. // The first portion is an EHCI qTD structure. Transfer_t are
  107. // allocated as-needed from a memory pool, loaded with pointers
  108. // to the actual data buffers, linked into a followup list,
  109. // and placed on ECHI Queue Heads. When the ECHI interrupt
  110. // occurs, the followup lists are used to find the Transfer_t
  111. // in memory. Callbacks are made, and then the Transfer_t are
  112. // returned to the memory pool.
  113. struct Transfer_struct {
  114. // Queue Element Transfer Descriptor (qTD), EHCI pg 40-45
  115. struct { // must be aligned to 32 byte boundary
  116. volatile uint32_t next;
  117. volatile uint32_t alt_next;
  118. volatile uint32_t token;
  119. volatile uint32_t buffer[5];
  120. } qtd;
  121. // Linked list of queued, not-yet-completed transfers
  122. Transfer_t *next_followup;
  123. Transfer_t *prev_followup;
  124. // Data to be used by callback function. When a group
  125. // of Transfer_t are created, these fields and the
  126. // interrupt-on-complete bit in the qTD token are only
  127. // set in the last Transfer_t of the list.
  128. Pipe_t *pipe;
  129. void *buffer;
  130. uint32_t length;
  131. setup_t *setup;
  132. USBDriver *driver;
  133. uint32_t unused;
  134. };
  135. /************************************************/
  136. /* Main USB EHCI Controller */
  137. /************************************************/
  138. class USBHost {
  139. public:
  140. static void begin();
  141. protected:
  142. static Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  143. uint32_t direction, uint32_t maxlen, uint32_t interval=0);
  144. static bool queue_Control_Transfer(Device_t *dev, setup_t *setup,
  145. void *buf, USBDriver *driver);
  146. static bool queue_Data_Transfer(Pipe_t *pipe, void *buffer,
  147. uint32_t len, USBDriver *driver);
  148. static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
  149. static void disconnect_Device(Device_t *dev);
  150. static void enumeration(const Transfer_t *transfer);
  151. static void driver_ready_for_device(USBDriver *driver);
  152. private:
  153. static void isr();
  154. static void claim_drivers(Device_t *dev);
  155. static uint32_t assign_address(void);
  156. static bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
  157. static void init_Device_Pipe_Transfer_memory(void);
  158. static Device_t * allocate_Device(void);
  159. static void delete_Pipe(Pipe_t *pipe);
  160. static void free_Device(Device_t *q);
  161. static Pipe_t * allocate_Pipe(void);
  162. static void free_Pipe(Pipe_t *q);
  163. static Transfer_t * allocate_Transfer(void);
  164. static void free_Transfer(Transfer_t *q);
  165. static bool allocate_interrupt_pipe_bandwidth(Pipe_t *pipe,
  166. uint32_t maxlen, uint32_t interval);
  167. static void add_qh_to_periodic_schedule(Pipe_t *pipe);
  168. protected:
  169. static void print(const Transfer_t *transfer);
  170. static void print(const Transfer_t *first, const Transfer_t *last);
  171. static void print_token(uint32_t token);
  172. static void print(const Pipe_t *pipe);
  173. static void print_driverlist(const char *name, const USBDriver *driver);
  174. static void print_qh_list(const Pipe_t *list);
  175. static void print_hexbytes(const void *ptr, uint32_t len);
  176. static void print(const char *s) { Serial.print(s); }
  177. static void print(int n) { Serial.print(n); }
  178. static void print(unsigned int n) { Serial.print(n); }
  179. static void print(long n) { Serial.print(n); }
  180. static void print(unsigned long n) { Serial.print(n); }
  181. static void println(const char *s) { Serial.println(s); }
  182. static void println(int n) { Serial.println(n); }
  183. static void println(unsigned int n) { Serial.println(n); }
  184. static void println(long n) { Serial.println(n); }
  185. static void println(unsigned long n) { Serial.println(n); }
  186. static void println() { Serial.println(); }
  187. static void print(uint32_t n, uint8_t b) { Serial.print(n, b); }
  188. static void println(uint32_t n, uint8_t b) { Serial.print(n, b); }
  189. static void println(const char *s, int n) {
  190. Serial.print(s); Serial.println(n); }
  191. static void println(const char *s, unsigned int n) {
  192. Serial.print(s); Serial.println(n); }
  193. static void println(const char *s, long n) {
  194. Serial.print(s); Serial.println(n); }
  195. static void println(const char *s, unsigned long n) {
  196. Serial.print(s); Serial.println(n); }
  197. static void println(const char *s, int n, uint8_t b) {
  198. Serial.print(s); Serial.println(n, b); }
  199. static void println(const char *s, unsigned int n, uint8_t b) {
  200. Serial.print(s); Serial.println(n, b); }
  201. static void println(const char *s, long n, uint8_t b) {
  202. Serial.print(s); Serial.println(n, b); }
  203. static void println(const char *s, unsigned long n, uint8_t b) {
  204. Serial.print(s); Serial.println(n, b); }
  205. static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  206. uint32_t wValue, uint32_t wIndex, uint32_t wLength) {
  207. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  208. s.word2 = wIndex | (wLength << 16);
  209. }
  210. };
  211. /************************************************/
  212. /* USB Device Driver Common Base Class */
  213. /************************************************/
  214. // All USB device drivers inherit from this base class.
  215. class USBDriver : public USBHost {
  216. protected:
  217. USBDriver() : next(NULL), device(NULL) {}
  218. // Check if a driver wishes to claim a device or interface or group
  219. // of interfaces within a device. When this function returns true,
  220. // the driver is considered bound or loaded for that device. When
  221. // new devices are detected, enumeration.cpp calls this function on
  222. // all unbound driver objects, to give them an opportunity to bind
  223. // to the new device.
  224. // device has its vid&pid, class/subclass fields initialized
  225. // type is 0 for device level, 1 for interface level, 2 for IAD
  226. // descriptors points to the specific descriptor data
  227. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  228. // When an unknown (not chapter 9) control transfer completes, this
  229. // function is called for all drivers bound to the device. Return
  230. // true means this driver originated this control transfer, so no
  231. // more drivers need to be offered an opportunity to process it.
  232. // This function is optional, only needed if the driver uses control
  233. // transfers and wishes to be notified when they complete.
  234. virtual void control(const Transfer_t *transfer) { }
  235. // When a device disconnects from the USB, this function is called.
  236. // The driver must free all resources it allocated and update any
  237. // internal state necessary to deal with the possibility of user
  238. // code continuing to call its API. However, pipes and transfers
  239. // are the handled by lower layers, so device drivers do not free
  240. // pipes they created or cancel transfers they had in progress.
  241. virtual void disconnect();
  242. // Drivers are managed by this single-linked list. All inactive
  243. // (not bound to any device) drivers are linked from
  244. // available_drivers in enumeration.cpp. When bound to a device,
  245. // drivers are linked from that Device_t drivers list.
  246. USBDriver *next;
  247. // The device this object instance is bound to. In words, this
  248. // is the specific device this driver is using. When not bound
  249. // to any device, this must be NULL.
  250. Device_t *device;
  251. friend class USBHost;
  252. public:
  253. // TODO: user-level functions
  254. // check if device is bound/active/online
  255. // query vid, pid
  256. // query string: manufacturer, product, serial number
  257. };
  258. /************************************************/
  259. /* USB Device Drivers */
  260. /************************************************/
  261. class USBHub : public USBDriver {
  262. public:
  263. USBHub();
  264. protected:
  265. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  266. virtual void control(const Transfer_t *transfer);
  267. virtual void disconnect();
  268. void poweron(uint32_t port);
  269. void getstatus(uint32_t port);
  270. void clearstatus(uint32_t port);
  271. void reset(uint32_t port);
  272. static void callback(const Transfer_t *transfer);
  273. void status_change(const Transfer_t *transfer);
  274. void new_port_status(uint32_t port, uint32_t status);
  275. void update_status();
  276. setup_t setup;
  277. uint8_t hub_desc[16];
  278. uint8_t endpoint;
  279. uint8_t numports;
  280. uint8_t characteristics;
  281. uint8_t powertime;
  282. uint8_t state;
  283. Pipe_t *changepipe;
  284. uint32_t changebits;
  285. uint32_t statusbits;
  286. uint16_t portstatus[7];
  287. uint8_t portstate[7];
  288. };
  289. class KeyboardController : public USBDriver {
  290. public:
  291. KeyboardController();
  292. int available();
  293. int read();
  294. uint8_t getKey();
  295. uint8_t getModifiers();
  296. uint8_t getOemKey();
  297. void attachPress(void (*keyPressed)());
  298. void attachRelease(void (*keyReleased)());
  299. protected:
  300. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  301. virtual void disconnect();
  302. static void callback(const Transfer_t *transfer);
  303. void new_data(const Transfer_t *transfer);
  304. private:
  305. void (*keyPressedFunction)();
  306. void (*keyReleasedFunction)();
  307. Pipe_t *datapipe;
  308. uint8_t report[8];
  309. };
  310. class MIDIDevice : public USBDriver {
  311. public:
  312. MIDIDevice();
  313. protected:
  314. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  315. virtual void disconnect();
  316. static void rx_callback(const Transfer_t *transfer);
  317. static void tx_callback(const Transfer_t *transfer);
  318. void rx_data(const Transfer_t *transfer);
  319. void tx_data(const Transfer_t *transfer);
  320. private:
  321. Pipe_t *rxpipe;
  322. Pipe_t *txpipe;
  323. enum { BUFFERSIZE = 64 };
  324. uint8_t buffer[BUFFERSIZE * 2];
  325. uint8_t rx_ep;
  326. uint8_t tx_ep;
  327. uint16_t rx_size;
  328. uint16_t tx_size;
  329. };
  330. #endif