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.

189 lines
5.8KB

  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 USBHostDriver;
  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. Device_t *next;
  59. setup_t setup;
  60. uint8_t speed; // 0=12, 1=1.5, 2=480 Mbit/sec
  61. uint8_t address;
  62. uint8_t hub_address;
  63. uint8_t hub_port;
  64. uint8_t enum_state;
  65. uint8_t bDeviceClass;
  66. uint8_t bDeviceSubClass;
  67. uint8_t bDeviceProtocol;
  68. uint16_t idVendor;
  69. uint16_t idProduct;
  70. uint16_t LanguageID;
  71. };
  72. // Pipe_t holes all information about each USB endpoint/pipe
  73. // The first half is an EHCI QH structure for the pipe.
  74. struct Pipe_struct {
  75. // Queue Head (QH), EHCI page 46-50
  76. struct { // must be aligned to 32 byte boundary
  77. volatile uint32_t horizontal_link;
  78. volatile uint32_t capabilities[2];
  79. volatile uint32_t current;
  80. volatile uint32_t next;
  81. volatile uint32_t alt_next;
  82. volatile uint32_t token;
  83. volatile uint32_t buffer[5];
  84. } qh;
  85. Device_t *device;
  86. uint8_t type; // 0=control, 1=isochronous, 2=bulk, 3=interrupt
  87. uint8_t direction; // 0=out, 1=in (changes for control, others fixed)
  88. uint8_t unusedbyte[2];
  89. void *callback_object; // TODO: C++ callbacks??
  90. void (*callback_function)(const Transfer_t *);
  91. };
  92. // Transfer_t represents a single transaction on the USB bus.
  93. // The first portion is an EHCI qTD structure. Transfer_t are
  94. // allocated as-needed from a memory pool, loaded with pointers
  95. // to the actual data buffers, linked into a followup list,
  96. // and placed on ECHI Queue Heads. When the ECHI interrupt
  97. // occurs, the followup lists are used to find the Transfer_t
  98. // in memory. Callbacks are made, and then the Transfer_t are
  99. // returned to the memory pool.
  100. struct Transfer_struct {
  101. // Queue Element Transfer Descriptor (qTD), EHCI pg 40-45
  102. struct { // must be aligned to 32 byte boundary
  103. volatile uint32_t next;
  104. volatile uint32_t alt_next;
  105. volatile uint32_t token;
  106. volatile uint32_t buffer[5];
  107. } qtd;
  108. // Linked list of queued, not-yet-completed transfers
  109. Transfer_t *next_followup;
  110. Transfer_t *prev_followup;
  111. // Data to be used by callback function. When a group
  112. // of Transfer_t are created, these fields and the
  113. // interrupt-on-complete bit in the qTD token are only
  114. // set in the last Transfer_t of the list.
  115. Pipe_t *pipe;
  116. void *buffer;
  117. uint32_t length;
  118. uint32_t unused[3];
  119. };
  120. /************************************************/
  121. /* Main USB EHCI Controller */
  122. /************************************************/
  123. class USBHost {
  124. public:
  125. static void begin();
  126. protected:
  127. static Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  128. uint32_t direction, uint32_t max_packet_len);
  129. static bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len);
  130. static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
  131. static void enumeration(const Transfer_t *transfer);
  132. private:
  133. static void isr();
  134. static void init_Device_Pipe_Transfer_memory(void);
  135. static Device_t * allocate_Device(void);
  136. static void free_Device(Device_t *q);
  137. static Pipe_t * allocate_Pipe(void);
  138. static void free_Pipe(Pipe_t *q);
  139. static Transfer_t * allocate_Transfer(void);
  140. static void free_Transfer(Transfer_t *q);
  141. protected:
  142. static void print(const Transfer_t *transfer);
  143. static void print(const Transfer_t *first, const Transfer_t *last);
  144. static void print_token(uint32_t token);
  145. static void print(const Pipe_t *pipe);
  146. static void print_hexbytes(const void *ptr, uint32_t len);
  147. static void print(const char *s);
  148. static void print(const char *s, int num);
  149. };
  150. /************************************************/
  151. /* USB Device Drivers */
  152. /************************************************/
  153. class USBHostDriver : public USBHost {
  154. public:
  155. virtual bool claim_device(Device_t *device) {
  156. return false;
  157. }
  158. virtual bool claim_interface(Device_t *device) {
  159. return false;
  160. }
  161. virtual void disconnect() {
  162. }
  163. USBHostDriver *next;
  164. };
  165. class USBHub : public USBHostDriver {
  166. };
  167. #endif