Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

433 linhas
15KB

  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. static void contribute_Devices(Device_t *devices, uint32_t num);
  154. static void contribute_Pipes(Pipe_t *pipes, uint32_t num);
  155. static void contribute_Transfers(Transfer_t *transfers, uint32_t num);
  156. static volatile bool enumeration_busy;
  157. private:
  158. static void isr();
  159. static void claim_drivers(Device_t *dev);
  160. static uint32_t assign_address(void);
  161. static bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
  162. static void init_Device_Pipe_Transfer_memory(void);
  163. static Device_t * allocate_Device(void);
  164. static void delete_Pipe(Pipe_t *pipe);
  165. static void free_Device(Device_t *q);
  166. static Pipe_t * allocate_Pipe(void);
  167. static void free_Pipe(Pipe_t *q);
  168. static Transfer_t * allocate_Transfer(void);
  169. static void free_Transfer(Transfer_t *q);
  170. static bool allocate_interrupt_pipe_bandwidth(Pipe_t *pipe,
  171. uint32_t maxlen, uint32_t interval);
  172. static void add_qh_to_periodic_schedule(Pipe_t *pipe);
  173. protected:
  174. static void print(const Transfer_t *transfer);
  175. static void print(const Transfer_t *first, const Transfer_t *last);
  176. static void print_token(uint32_t token);
  177. static void print(const Pipe_t *pipe);
  178. static void print_driverlist(const char *name, const USBDriver *driver);
  179. static void print_qh_list(const Pipe_t *list);
  180. static void print_hexbytes(const void *ptr, uint32_t len);
  181. static void print(const char *s) { Serial.print(s); }
  182. static void print(int n) { Serial.print(n); }
  183. static void print(unsigned int n) { Serial.print(n); }
  184. static void print(long n) { Serial.print(n); }
  185. static void print(unsigned long n) { Serial.print(n); }
  186. static void println(const char *s) { Serial.println(s); }
  187. static void println(int n) { Serial.println(n); }
  188. static void println(unsigned int n) { Serial.println(n); }
  189. static void println(long n) { Serial.println(n); }
  190. static void println(unsigned long n) { Serial.println(n); }
  191. static void println() { Serial.println(); }
  192. static void print(uint32_t n, uint8_t b) { Serial.print(n, b); }
  193. static void println(uint32_t n, uint8_t b) { Serial.println(n, b); }
  194. static void println(const char *s, int n) {
  195. Serial.print(s); Serial.println(n); }
  196. static void println(const char *s, unsigned int n) {
  197. Serial.print(s); Serial.println(n); }
  198. static void println(const char *s, long n) {
  199. Serial.print(s); Serial.println(n); }
  200. static void println(const char *s, unsigned long n) {
  201. Serial.print(s); Serial.println(n); }
  202. static void println(const char *s, int n, uint8_t b) {
  203. Serial.print(s); Serial.println(n, b); }
  204. static void println(const char *s, unsigned int n, uint8_t b) {
  205. Serial.print(s); Serial.println(n, b); }
  206. static void println(const char *s, long n, uint8_t b) {
  207. Serial.print(s); Serial.println(n, b); }
  208. static void println(const char *s, unsigned long n, uint8_t b) {
  209. Serial.print(s); Serial.println(n, b); }
  210. static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  211. uint32_t wValue, uint32_t wIndex, uint32_t wLength) {
  212. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  213. s.word2 = wIndex | (wLength << 16);
  214. }
  215. };
  216. /************************************************/
  217. /* USB Device Driver Common Base Class */
  218. /************************************************/
  219. // All USB device drivers inherit from this base class.
  220. class USBDriver : public USBHost {
  221. public:
  222. // TODO: user-level functions
  223. // check if device is bound/active/online
  224. // query vid, pid
  225. // query string: manufacturer, product, serial number
  226. protected:
  227. USBDriver() : next(NULL), device(NULL) {}
  228. // Check if a driver wishes to claim a device or interface or group
  229. // of interfaces within a device. When this function returns true,
  230. // the driver is considered bound or loaded for that device. When
  231. // new devices are detected, enumeration.cpp calls this function on
  232. // all unbound driver objects, to give them an opportunity to bind
  233. // to the new device.
  234. // device has its vid&pid, class/subclass fields initialized
  235. // type is 0 for device level, 1 for interface level, 2 for IAD
  236. // descriptors points to the specific descriptor data
  237. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  238. // When an unknown (not chapter 9) control transfer completes, this
  239. // function is called for all drivers bound to the device. Return
  240. // true means this driver originated this control transfer, so no
  241. // more drivers need to be offered an opportunity to process it.
  242. // This function is optional, only needed if the driver uses control
  243. // transfers and wishes to be notified when they complete.
  244. virtual void control(const Transfer_t *transfer) { }
  245. // When any of the USBDriverTimer objects a driver creates generates
  246. // a timer event, this function is called.
  247. virtual void timer_event(USBDriverTimer *whichTimer) { }
  248. // When a device disconnects from the USB, this function is called.
  249. // The driver must free all resources it allocated and update any
  250. // internal state necessary to deal with the possibility of user
  251. // code continuing to call its API. However, pipes and transfers
  252. // are the handled by lower layers, so device drivers do not free
  253. // pipes they created or cancel transfers they had in progress.
  254. virtual void disconnect();
  255. // Drivers are managed by this single-linked list. All inactive
  256. // (not bound to any device) drivers are linked from
  257. // available_drivers in enumeration.cpp. When bound to a device,
  258. // drivers are linked from that Device_t drivers list.
  259. USBDriver *next;
  260. // The device this object instance is bound to. In words, this
  261. // is the specific device this driver is using. When not bound
  262. // to any device, this must be NULL.
  263. Device_t *device;
  264. friend class USBHost;
  265. };
  266. // Device drivers may create these timer objects to schedule a timer call
  267. class USBDriverTimer {
  268. public:
  269. USBDriverTimer() { }
  270. USBDriverTimer(USBDriver *d) : driver(d) { }
  271. void init(USBDriver *d) { driver = d; };
  272. void start(uint32_t microseconds);
  273. void *pointer;
  274. uint32_t integer;
  275. uint32_t started_micros; // testing only
  276. private:
  277. USBDriver *driver;
  278. uint32_t usec;
  279. USBDriverTimer *next;
  280. USBDriverTimer *prev;
  281. friend class USBHost;
  282. };
  283. /************************************************/
  284. /* USB Device Drivers */
  285. /************************************************/
  286. class USBHub : public USBDriver {
  287. public:
  288. USBHub();
  289. // Hubs with more more than 7 ports are built from two tiers of hubs
  290. // using 4 or 7 port hub chips. While the USB spec seems to allow
  291. // hubs to have up to 255 ports, in practice all hub chips on the
  292. // market are only 2, 3, 4 or 7 ports.
  293. enum { MAXPORTS = 7 };
  294. typedef uint8_t portbitmask_t;
  295. enum {
  296. PORT_OFF = 0,
  297. PORT_DISCONNECT = 1,
  298. PORT_DEBOUNCE1 = 2,
  299. PORT_DEBOUNCE2 = 3,
  300. PORT_DEBOUNCE3 = 4,
  301. PORT_DEBOUNCE4 = 5,
  302. PORT_DEBOUNCE5 = 6,
  303. PORT_RESET = 7,
  304. PORT_RECOVERY = 8,
  305. PORT_ACTIVE = 9
  306. };
  307. protected:
  308. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  309. virtual void control(const Transfer_t *transfer);
  310. virtual void timer_event(USBDriverTimer *whichTimer);
  311. virtual void disconnect();
  312. bool can_send_control_now();
  313. void send_poweron(uint32_t port);
  314. void send_getstatus(uint32_t port);
  315. void send_clearstatus_connect(uint32_t port);
  316. void send_clearstatus_enable(uint32_t port);
  317. void send_clearstatus_suspend(uint32_t port);
  318. void send_clearstatus_overcurrent(uint32_t port);
  319. void send_clearstatus_reset(uint32_t port);
  320. void send_setreset(uint32_t port);
  321. static void callback(const Transfer_t *transfer);
  322. void status_change(const Transfer_t *transfer);
  323. void new_port_status(uint32_t port, uint32_t status);
  324. void start_debounce_timer(uint32_t port);
  325. void stop_debounce_timer(uint32_t port);
  326. static volatile bool reset_busy;
  327. USBDriverTimer debouncetimer;
  328. USBDriverTimer resettimer;
  329. setup_t setup;
  330. Pipe_t *changepipe;
  331. Device_t *devicelist[MAXPORTS];
  332. uint32_t changebits;
  333. uint32_t statusbits;
  334. uint8_t hub_desc[16];
  335. uint8_t endpoint;
  336. uint8_t interval;
  337. uint8_t numports;
  338. uint8_t characteristics;
  339. uint8_t powertime;
  340. uint8_t sending_control_transfer;
  341. uint8_t port_doing_reset;
  342. uint8_t port_doing_reset_speed;
  343. uint8_t portstate[MAXPORTS];
  344. portbitmask_t send_pending_poweron;
  345. portbitmask_t send_pending_getstatus;
  346. portbitmask_t send_pending_clearstatus_connect;
  347. portbitmask_t send_pending_clearstatus_enable;
  348. portbitmask_t send_pending_clearstatus_suspend;
  349. portbitmask_t send_pending_clearstatus_overcurrent;
  350. portbitmask_t send_pending_clearstatus_reset;
  351. portbitmask_t send_pending_setreset;
  352. portbitmask_t debounce_in_use;
  353. Device_t mydevices[MAXPORTS];
  354. Pipe_t mypipes[2] __attribute__ ((aligned(32)));
  355. Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
  356. };
  357. class KeyboardController : public USBDriver {
  358. public:
  359. KeyboardController();
  360. int available();
  361. int read();
  362. uint8_t getKey();
  363. uint8_t getModifiers();
  364. uint8_t getOemKey();
  365. void attachPress(void (*keyPressed)());
  366. void attachRelease(void (*keyReleased)());
  367. protected:
  368. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  369. virtual void control(const Transfer_t *transfer);
  370. virtual void disconnect();
  371. static void callback(const Transfer_t *transfer);
  372. void new_data(const Transfer_t *transfer);
  373. private:
  374. void (*keyPressedFunction)();
  375. void (*keyReleasedFunction)();
  376. Pipe_t *datapipe;
  377. setup_t setup;
  378. uint8_t report[8];
  379. Pipe_t mypipes[2] __attribute__ ((aligned(32)));
  380. Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
  381. };
  382. class MIDIDevice : public USBDriver {
  383. public:
  384. MIDIDevice();
  385. protected:
  386. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  387. virtual void disconnect();
  388. static void rx_callback(const Transfer_t *transfer);
  389. static void tx_callback(const Transfer_t *transfer);
  390. void rx_data(const Transfer_t *transfer);
  391. void tx_data(const Transfer_t *transfer);
  392. private:
  393. Pipe_t *rxpipe;
  394. Pipe_t *txpipe;
  395. enum { BUFFERSIZE = 64 };
  396. uint8_t buffer[BUFFERSIZE * 2];
  397. uint8_t rx_ep;
  398. uint8_t tx_ep;
  399. uint16_t rx_size;
  400. uint16_t tx_size;
  401. Pipe_t mypipes[3] __attribute__ ((aligned(32)));
  402. Transfer_t mytransfers[7] __attribute__ ((aligned(32)));
  403. };
  404. #endif