No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

487 líneas
17KB

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