Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 7 roky
před 7 roky
před 7 roky
před 7 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. class USBDriverTimer;
  32. typedef struct Device_struct Device_t;
  33. typedef struct Pipe_struct Pipe_t;
  34. typedef struct Transfer_struct Transfer_t;
  35. //typedef struct DriverTimer_struct DriverTimer_t;
  36. // setup_t holds the 8 byte USB SETUP packet data.
  37. // These unions & structs allow convenient access to
  38. // the setup fields.
  39. typedef union {
  40. struct {
  41. union {
  42. struct {
  43. uint8_t bmRequestType;
  44. uint8_t bRequest;
  45. };
  46. uint16_t wRequestAndType;
  47. };
  48. uint16_t wValue;
  49. uint16_t wIndex;
  50. uint16_t wLength;
  51. };
  52. struct {
  53. uint32_t word1;
  54. uint32_t word2;
  55. };
  56. } setup_t;
  57. // Device_t holds all the information about a USB device
  58. struct Device_struct {
  59. Pipe_t *control_pipe;
  60. Pipe_t *data_pipes;
  61. Device_t *next;
  62. USBDriver *drivers;
  63. uint8_t speed; // 0=12, 1=1.5, 2=480 Mbit/sec
  64. uint8_t address;
  65. uint8_t hub_address;
  66. uint8_t hub_port;
  67. uint8_t enum_state;
  68. uint8_t bDeviceClass;
  69. uint8_t bDeviceSubClass;
  70. uint8_t bDeviceProtocol;
  71. uint8_t bmAttributes;
  72. uint8_t bMaxPower;
  73. uint16_t idVendor;
  74. uint16_t idProduct;
  75. uint16_t LanguageID;
  76. };
  77. // Pipe_t holes all information about each USB endpoint/pipe
  78. // The first half is an EHCI QH structure for the pipe.
  79. struct Pipe_struct {
  80. // Queue Head (QH), EHCI page 46-50
  81. struct { // must be aligned to 32 byte boundary
  82. volatile uint32_t horizontal_link;
  83. volatile uint32_t capabilities[2];
  84. volatile uint32_t current;
  85. volatile uint32_t next;
  86. volatile uint32_t alt_next;
  87. volatile uint32_t token;
  88. volatile uint32_t buffer[5];
  89. } qh;
  90. Device_t *device;
  91. uint8_t type; // 0=control, 1=isochronous, 2=bulk, 3=interrupt
  92. uint8_t direction; // 0=out, 1=in (changes for control, others fixed)
  93. uint8_t start_mask; // TODO: is this redundant?
  94. uint8_t complete_mask; // TODO: is this redundant?
  95. Pipe_t *next;
  96. void (*callback_function)(const Transfer_t *);
  97. uint16_t periodic_interval;
  98. uint16_t periodic_offset; // TODO: is this redundant?
  99. uint32_t unused1;
  100. uint32_t unused2;
  101. uint32_t unused3;
  102. uint32_t unused4;
  103. uint32_t unused5;
  104. uint32_t unused6;
  105. uint32_t unused7;
  106. };
  107. // Transfer_t represents a single transaction on the USB bus.
  108. // The first portion is an EHCI qTD structure. Transfer_t are
  109. // allocated as-needed from a memory pool, loaded with pointers
  110. // to the actual data buffers, linked into a followup list,
  111. // and placed on ECHI Queue Heads. When the ECHI interrupt
  112. // occurs, the followup lists are used to find the Transfer_t
  113. // in memory. Callbacks are made, and then the Transfer_t are
  114. // returned to the memory pool.
  115. struct Transfer_struct {
  116. // Queue Element Transfer Descriptor (qTD), EHCI pg 40-45
  117. struct { // must be aligned to 32 byte boundary
  118. volatile uint32_t next;
  119. volatile uint32_t alt_next;
  120. volatile uint32_t token;
  121. volatile uint32_t buffer[5];
  122. } qtd;
  123. // Linked list of queued, not-yet-completed transfers
  124. Transfer_t *next_followup;
  125. Transfer_t *prev_followup;
  126. Pipe_t *pipe;
  127. // Data to be used by callback function. When a group
  128. // of Transfer_t are created, these fields and the
  129. // interrupt-on-complete bit in the qTD token are only
  130. // set in the last Transfer_t of the list.
  131. void *buffer;
  132. uint32_t length;
  133. setup_t setup;
  134. USBDriver *driver;
  135. };
  136. /************************************************/
  137. /* Main USB EHCI Controller */
  138. /************************************************/
  139. class USBHost {
  140. public:
  141. static void begin();
  142. protected:
  143. static Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  144. uint32_t direction, uint32_t maxlen, uint32_t interval=0);
  145. static bool queue_Control_Transfer(Device_t *dev, setup_t *setup,
  146. void *buf, USBDriver *driver);
  147. static bool queue_Data_Transfer(Pipe_t *pipe, void *buffer,
  148. uint32_t len, USBDriver *driver);
  149. static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
  150. static void disconnect_Device(Device_t *dev);
  151. static void enumeration(const Transfer_t *transfer);
  152. static void driver_ready_for_device(USBDriver *driver);
  153. private:
  154. static void isr();
  155. static void claim_drivers(Device_t *dev);
  156. static uint32_t assign_address(void);
  157. static bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
  158. static void init_Device_Pipe_Transfer_memory(void);
  159. static Device_t * allocate_Device(void);
  160. static void delete_Pipe(Pipe_t *pipe);
  161. static void free_Device(Device_t *q);
  162. static Pipe_t * allocate_Pipe(void);
  163. static void free_Pipe(Pipe_t *q);
  164. static Transfer_t * allocate_Transfer(void);
  165. static void free_Transfer(Transfer_t *q);
  166. static bool allocate_interrupt_pipe_bandwidth(Pipe_t *pipe,
  167. uint32_t maxlen, uint32_t interval);
  168. static void add_qh_to_periodic_schedule(Pipe_t *pipe);
  169. protected:
  170. static void print(const Transfer_t *transfer);
  171. static void print(const Transfer_t *first, const Transfer_t *last);
  172. static void print_token(uint32_t token);
  173. static void print(const Pipe_t *pipe);
  174. static void print_driverlist(const char *name, const USBDriver *driver);
  175. static void print_qh_list(const Pipe_t *list);
  176. static void print_hexbytes(const void *ptr, uint32_t len);
  177. static void print(const char *s) { Serial.print(s); }
  178. static void print(int n) { Serial.print(n); }
  179. static void print(unsigned int n) { Serial.print(n); }
  180. static void print(long n) { Serial.print(n); }
  181. static void print(unsigned long n) { Serial.print(n); }
  182. static void println(const char *s) { Serial.println(s); }
  183. static void println(int n) { Serial.println(n); }
  184. static void println(unsigned int n) { Serial.println(n); }
  185. static void println(long n) { Serial.println(n); }
  186. static void println(unsigned long n) { Serial.println(n); }
  187. static void println() { Serial.println(); }
  188. static void print(uint32_t n, uint8_t b) { Serial.print(n, b); }
  189. static void println(uint32_t n, uint8_t b) { Serial.println(n, b); }
  190. static void println(const char *s, int n) {
  191. Serial.print(s); Serial.println(n); }
  192. static void println(const char *s, unsigned int n) {
  193. Serial.print(s); Serial.println(n); }
  194. static void println(const char *s, long n) {
  195. Serial.print(s); Serial.println(n); }
  196. static void println(const char *s, unsigned long n) {
  197. Serial.print(s); Serial.println(n); }
  198. static void println(const char *s, int n, uint8_t b) {
  199. Serial.print(s); Serial.println(n, b); }
  200. static void println(const char *s, unsigned int n, uint8_t b) {
  201. Serial.print(s); Serial.println(n, b); }
  202. static void println(const char *s, long n, uint8_t b) {
  203. Serial.print(s); Serial.println(n, b); }
  204. static void println(const char *s, unsigned long n, uint8_t b) {
  205. Serial.print(s); Serial.println(n, b); }
  206. static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  207. uint32_t wValue, uint32_t wIndex, uint32_t wLength) {
  208. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  209. s.word2 = wIndex | (wLength << 16);
  210. }
  211. };
  212. /************************************************/
  213. /* USB Device Driver Common Base Class */
  214. /************************************************/
  215. // All USB device drivers inherit from this base class.
  216. class USBDriver : public USBHost {
  217. public:
  218. // TODO: user-level functions
  219. // check if device is bound/active/online
  220. // query vid, pid
  221. // query string: manufacturer, product, serial number
  222. protected:
  223. USBDriver() : next(NULL), device(NULL) {}
  224. // Check if a driver wishes to claim a device or interface or group
  225. // of interfaces within a device. When this function returns true,
  226. // the driver is considered bound or loaded for that device. When
  227. // new devices are detected, enumeration.cpp calls this function on
  228. // all unbound driver objects, to give them an opportunity to bind
  229. // to the new device.
  230. // device has its vid&pid, class/subclass fields initialized
  231. // type is 0 for device level, 1 for interface level, 2 for IAD
  232. // descriptors points to the specific descriptor data
  233. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  234. // When an unknown (not chapter 9) control transfer completes, this
  235. // function is called for all drivers bound to the device. Return
  236. // true means this driver originated this control transfer, so no
  237. // more drivers need to be offered an opportunity to process it.
  238. // This function is optional, only needed if the driver uses control
  239. // transfers and wishes to be notified when they complete.
  240. virtual void control(const Transfer_t *transfer) { }
  241. // When any of the USBDriverTimer objects a driver creates generates
  242. // a timer event, this function is called.
  243. virtual void timer_event(USBDriverTimer *whichTimer) { }
  244. // When a device disconnects from the USB, this function is called.
  245. // The driver must free all resources it allocated and update any
  246. // internal state necessary to deal with the possibility of user
  247. // code continuing to call its API. However, pipes and transfers
  248. // are the handled by lower layers, so device drivers do not free
  249. // pipes they created or cancel transfers they had in progress.
  250. virtual void disconnect();
  251. // Drivers are managed by this single-linked list. All inactive
  252. // (not bound to any device) drivers are linked from
  253. // available_drivers in enumeration.cpp. When bound to a device,
  254. // drivers are linked from that Device_t drivers list.
  255. USBDriver *next;
  256. // The device this object instance is bound to. In words, this
  257. // is the specific device this driver is using. When not bound
  258. // to any device, this must be NULL.
  259. Device_t *device;
  260. friend class USBHost;
  261. };
  262. // Device drivers may create these timer objects to schedule a timer call
  263. class USBDriverTimer {
  264. public:
  265. USBDriverTimer() { }
  266. USBDriverTimer(USBDriver *d) : driver(d) { }
  267. void init(USBDriver *d) { driver = d; };
  268. void start(uint32_t microseconds);
  269. void *pointer;
  270. uint32_t integer;
  271. uint32_t started_micros; // testing only
  272. private:
  273. USBDriver *driver;
  274. uint32_t usec;
  275. USBDriverTimer *next;
  276. USBDriverTimer *prev;
  277. friend class USBHost;
  278. };
  279. /************************************************/
  280. /* USB Device Drivers */
  281. /************************************************/
  282. class USBHub : public USBDriver {
  283. public:
  284. USBHub();
  285. enum { MAXPORTS = 7 };
  286. typedef uint8_t portbitmask_t;
  287. enum {
  288. PORT_OFF = 0,
  289. PORT_DISCONNECT = 1,
  290. PORT_DEBOUNCE1 = 2,
  291. PORT_DEBOUNCE2 = 3,
  292. PORT_DEBOUNCE3 = 4,
  293. PORT_DEBOUNCE4 = 5,
  294. PORT_DEBOUNCE5 = 6,
  295. PORT_RESET = 7,
  296. PORT_RECOVERY = 8,
  297. PORT_ACTIVE = 9
  298. };
  299. protected:
  300. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  301. virtual void control(const Transfer_t *transfer);
  302. virtual void timer_event(USBDriverTimer *whichTimer);
  303. virtual void disconnect();
  304. bool can_send_control_now();
  305. void send_poweron(uint32_t port);
  306. void send_getstatus(uint32_t port);
  307. void send_clearstatus_connect(uint32_t port);
  308. void send_clearstatus_enable(uint32_t port);
  309. void send_clearstatus_suspend(uint32_t port);
  310. void send_clearstatus_overcurrent(uint32_t port);
  311. void send_clearstatus_reset(uint32_t port);
  312. void send_setreset(uint32_t port);
  313. static void callback(const Transfer_t *transfer);
  314. void status_change(const Transfer_t *transfer);
  315. void new_port_status(uint32_t port, uint32_t status);
  316. void start_debounce_timer(uint32_t port);
  317. void stop_debounce_timer(uint32_t port);
  318. USBDriverTimer debouncetimer;
  319. USBDriverTimer mytimer;
  320. USBDriverTimer othertimer;
  321. USBDriverTimer mytimers[MAXPORTS];
  322. uint32_t debounce_in_use;
  323. setup_t setup[MAXPORTS+1];
  324. uint32_t statusbits[MAXPORTS+1];
  325. uint8_t hub_desc[16];
  326. uint8_t endpoint;
  327. uint8_t interval;
  328. uint8_t numports;
  329. uint8_t characteristics;
  330. uint8_t powertime;
  331. uint8_t sending_control_transfer;
  332. portbitmask_t send_pending_poweron;
  333. portbitmask_t send_pending_getstatus;
  334. portbitmask_t send_pending_clearstatus_connect;
  335. portbitmask_t send_pending_clearstatus_enable;
  336. portbitmask_t send_pending_clearstatus_suspend;
  337. portbitmask_t send_pending_clearstatus_overcurrent;
  338. portbitmask_t send_pending_clearstatus_reset;
  339. portbitmask_t send_pending_setreset;
  340. Pipe_t *changepipe;
  341. uint32_t changebits;
  342. uint8_t portstate[MAXPORTS];
  343. };
  344. class KeyboardController : public USBDriver {
  345. public:
  346. KeyboardController();
  347. int available();
  348. int read();
  349. uint8_t getKey();
  350. uint8_t getModifiers();
  351. uint8_t getOemKey();
  352. void attachPress(void (*keyPressed)());
  353. void attachRelease(void (*keyReleased)());
  354. protected:
  355. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  356. virtual void disconnect();
  357. static void callback(const Transfer_t *transfer);
  358. void new_data(const Transfer_t *transfer);
  359. private:
  360. void (*keyPressedFunction)();
  361. void (*keyReleasedFunction)();
  362. Pipe_t *datapipe;
  363. uint8_t report[8];
  364. };
  365. class MIDIDevice : public USBDriver {
  366. public:
  367. MIDIDevice();
  368. protected:
  369. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  370. virtual void disconnect();
  371. static void rx_callback(const Transfer_t *transfer);
  372. static void tx_callback(const Transfer_t *transfer);
  373. void rx_data(const Transfer_t *transfer);
  374. void tx_data(const Transfer_t *transfer);
  375. private:
  376. Pipe_t *rxpipe;
  377. Pipe_t *txpipe;
  378. enum { BUFFERSIZE = 64 };
  379. uint8_t buffer[BUFFERSIZE * 2];
  380. uint8_t rx_ep;
  381. uint8_t tx_ep;
  382. uint16_t rx_size;
  383. uint16_t tx_size;
  384. };
  385. #endif