Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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