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.

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