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.

285 lines
9.6KB

  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. Device_t *next;
  59. USBDriver *drivers;
  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. uint8_t bmAttributes;
  69. uint8_t bMaxPower;
  70. uint16_t idVendor;
  71. uint16_t idProduct;
  72. uint16_t LanguageID;
  73. };
  74. // Pipe_t holes all information about each USB endpoint/pipe
  75. // The first half is an EHCI QH structure for the pipe.
  76. struct Pipe_struct {
  77. // Queue Head (QH), EHCI page 46-50
  78. struct { // must be aligned to 32 byte boundary
  79. volatile uint32_t horizontal_link;
  80. volatile uint32_t capabilities[2];
  81. volatile uint32_t current;
  82. volatile uint32_t next;
  83. volatile uint32_t alt_next;
  84. volatile uint32_t token;
  85. volatile uint32_t buffer[5];
  86. } qh;
  87. Device_t *device;
  88. uint8_t type; // 0=control, 1=isochronous, 2=bulk, 3=interrupt
  89. uint8_t direction; // 0=out, 1=in (changes for control, others fixed)
  90. uint8_t unusedbyte[2];
  91. USBDriver *callback_object;
  92. void (*callback_function)(const Transfer_t *);
  93. };
  94. // Transfer_t represents a single transaction on the USB bus.
  95. // The first portion is an EHCI qTD structure. Transfer_t are
  96. // allocated as-needed from a memory pool, loaded with pointers
  97. // to the actual data buffers, linked into a followup list,
  98. // and placed on ECHI Queue Heads. When the ECHI interrupt
  99. // occurs, the followup lists are used to find the Transfer_t
  100. // in memory. Callbacks are made, and then the Transfer_t are
  101. // returned to the memory pool.
  102. struct Transfer_struct {
  103. // Queue Element Transfer Descriptor (qTD), EHCI pg 40-45
  104. struct { // must be aligned to 32 byte boundary
  105. volatile uint32_t next;
  106. volatile uint32_t alt_next;
  107. volatile uint32_t token;
  108. volatile uint32_t buffer[5];
  109. } qtd;
  110. // Linked list of queued, not-yet-completed transfers
  111. Transfer_t *next_followup;
  112. Transfer_t *prev_followup;
  113. // Data to be used by callback function. When a group
  114. // of Transfer_t are created, these fields and the
  115. // interrupt-on-complete bit in the qTD token are only
  116. // set in the last Transfer_t of the list.
  117. Pipe_t *pipe;
  118. void *buffer;
  119. uint32_t length;
  120. setup_t *setup;
  121. USBDriver *driver;
  122. uint32_t unused;
  123. };
  124. /************************************************/
  125. /* Main USB EHCI Controller */
  126. /************************************************/
  127. class USBHost {
  128. public:
  129. static void begin();
  130. protected:
  131. static Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  132. uint32_t direction, uint32_t maxlen, uint32_t interval=0);
  133. static bool queue_Control_Transfer(Device_t *dev, setup_t *setup,
  134. void *buf, USBDriver *driver);
  135. static bool queue_Data_Transfer(Pipe_t *pipe, void *buffer,
  136. uint32_t len, USBDriver *driver);
  137. static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
  138. static void enumeration(const Transfer_t *transfer);
  139. static void driver_ready_for_device(USBDriver *driver);
  140. private:
  141. static void isr();
  142. static void claim_drivers(Device_t *dev);
  143. static bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
  144. static void init_Device_Pipe_Transfer_memory(void);
  145. static Device_t * allocate_Device(void);
  146. static void free_Device(Device_t *q);
  147. static Pipe_t * allocate_Pipe(void);
  148. static void free_Pipe(Pipe_t *q);
  149. static Transfer_t * allocate_Transfer(void);
  150. static void free_Transfer(Transfer_t *q);
  151. protected:
  152. static void print(const Transfer_t *transfer);
  153. static void print(const Transfer_t *first, const Transfer_t *last);
  154. static void print_token(uint32_t token);
  155. static void print(const Pipe_t *pipe);
  156. static void print_hexbytes(const void *ptr, uint32_t len);
  157. static void print(const char *s);
  158. static void print(const char *s, int num);
  159. static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  160. uint32_t wValue, uint32_t wIndex, uint32_t wLength) {
  161. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  162. s.word2 = wIndex | (wLength << 16);
  163. }
  164. };
  165. /************************************************/
  166. /* USB Device Driver Common Base Class */
  167. /************************************************/
  168. // All USB device drivers inherit from this base class.
  169. class USBDriver : public USBHost {
  170. protected:
  171. USBDriver() : next(NULL), device(NULL) {}
  172. // Check if a driver wishes to claim a device or interface or group
  173. // of interfaces within a device. When this function returns true,
  174. // the driver is considered bound or loaded for that device. When
  175. // new devices are detected, enumeration.cpp calls this function on
  176. // all unbound driver objects, to give them an opportunity to bind
  177. // to the new device.
  178. // device has its vid&pid, class/subclass fields initialized
  179. // type is 0 for device level, 1 for interface level, 2 for IAD
  180. // descriptors points to the specific descriptor data
  181. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  182. // When an unknown (not chapter 9) control transfer completes, this
  183. // function is called for all drivers bound to the device. Return
  184. // true means this driver originated this control transfer, so no
  185. // more drivers need to be offered an opportunity to process it.
  186. // This function is optional, only needed if the driver uses control
  187. // transfers and wishes to be notified when they complete.
  188. virtual void control(const Transfer_t *transfer) { }
  189. // When a device disconnects from the USB, this function is called.
  190. // The driver must free all resources it has allocated.
  191. virtual void disconnect();
  192. // Drivers are managed by this single-linked list. All inactive
  193. // (not bound to any device) drivers are linked from
  194. // available_drivers in enumeration.cpp. When bound to a device,
  195. // drivers are linked from that Device_t drivers list.
  196. USBDriver *next;
  197. // The device this object instance is bound to. In words, this
  198. // is the specific device this driver is using. When not bound
  199. // to any device, this must be NULL.
  200. Device_t *device;
  201. friend class USBHost;
  202. public:
  203. // TODO: user-level functions
  204. // check if device is bound/active/online
  205. // query vid, pid
  206. // query string: manufacturer, product, serial number
  207. };
  208. /************************************************/
  209. /* USB Device Drivers */
  210. /************************************************/
  211. class USBHub : public USBDriver {
  212. public:
  213. USBHub();
  214. protected:
  215. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  216. virtual void control(const Transfer_t *transfer);
  217. virtual void disconnect();
  218. void poweron(uint32_t port);
  219. void getstatus(uint32_t port);
  220. void clearstatus(uint32_t port);
  221. void reset(uint32_t port);
  222. static void callback(const Transfer_t *transfer);
  223. void status_change(const Transfer_t *transfer);
  224. void new_port_status(uint32_t port, uint32_t status);
  225. void update_status();
  226. setup_t setup;
  227. uint8_t hub_desc[16];
  228. uint8_t endpoint;
  229. uint8_t numports;
  230. uint8_t characteristics;
  231. uint8_t powertime;
  232. uint8_t state;
  233. Pipe_t *changepipe;
  234. uint32_t changebits;
  235. uint32_t statusbits;
  236. uint16_t portstatus[7];
  237. uint8_t portstate[7];
  238. };
  239. class KeyboardController : public USBDriver {
  240. public:
  241. KeyboardController();
  242. int available();
  243. int read();
  244. uint8_t getKey();
  245. uint8_t getModifiers();
  246. uint8_t getOemKey();
  247. void attachPress(void (*keyPressed)());
  248. void attachRelease(void (*keyReleased)());
  249. protected:
  250. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  251. virtual void disconnect();
  252. static void callback(const Transfer_t *transfer);
  253. void new_data(const Transfer_t *transfer);
  254. private:
  255. void (*keyPressedFunction)();
  256. void (*keyReleasedFunction)();
  257. Pipe_t *datapipe;
  258. uint8_t report[8];
  259. };
  260. #endif