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.

500 satır
18KB

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