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.

200 lines
5.9KB

  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. #include <Arduino.h>
  24. #include "USBHost.h"
  25. MIDIDevice::MIDIDevice()
  26. {
  27. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  28. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  29. driver_ready_for_device(this);
  30. }
  31. // Audio Class-Specific Descriptor Types (audio 1.0, page 99)
  32. // CS_UNDEFINED 0x20
  33. // CS_DEVICE 0x21
  34. // CS_CONFIGURATION 0x22
  35. // CS_STRING 0x23
  36. // CS_INTERFACE 0x24
  37. // CS_ENDPOINT 0x25
  38. // MS Class-Specific Interface Descriptor Subtypes (midi 1.0, page 36)
  39. // MS_DESCRIPTOR_UNDEFINED 0x00
  40. // MS_HEADER 0x01
  41. // MIDI_IN_JACK 0x02
  42. // MIDI_OUT_JACK 0x03
  43. // ELEMENT 0x04
  44. // MS Class-Specific Endpoint Descriptor Subtypes (midi 1.0, page 36)
  45. // DESCRIPTOR_UNDEFINED 0x00
  46. // MS_GENERAL 0x01
  47. // MS MIDI IN and OUT Jack types (midi 1.0, page 36)
  48. // JACK_TYPE_UNDEFINED 0x00
  49. // EMBEDDED 0x01
  50. // EXTERNAL 0x02
  51. // Endpoint Control Selectors (midi 1.0, page 36)
  52. // EP_CONTROL_UNDEFINED 0x00
  53. // ASSOCIATION_CONTROL 0x01
  54. bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  55. {
  56. // only claim at interface level
  57. if (type != 1) return false;
  58. println("MIDIDevice claim this=", (uint32_t)this, HEX);
  59. println("len = ", len);
  60. const uint8_t *p = descriptors;
  61. const uint8_t *end = p + len;
  62. if (p[0] != 9 || p[1] != 4) return false; // interface descriptor
  63. //println(" bInterfaceClass=", p[5]);
  64. //println(" bInterfaceSubClass=", p[6]);
  65. if (p[5] != 1) return false; // bInterfaceClass: 1 = Audio class
  66. if (p[6] != 3) return false; // bInterfaceSubClass: 3 = MIDI
  67. p += 9;
  68. println(" Interface is MIDI");
  69. rx_ep = 0;
  70. tx_ep = 0;
  71. while (p < end) {
  72. len = *p;
  73. if (len < 4) return false; // all audio desc are at least 4 bytes
  74. if (p + len > end) return false; // reject if beyond end of data
  75. uint32_t type = p[1];
  76. //println("type: ", type);
  77. if (type == 4 || type == 11) break; // interface or IAD, not for us
  78. if (type == 0x24) { // 0x24 = Audio CS_INTERFACE, audio 1.0, page 99
  79. uint32_t subtype = p[2];
  80. //println("subtype: ", subtype);
  81. if (subtype == 1) {
  82. // Interface Header, midi 1.0, page 21
  83. println(" MIDI Header (ignored)");
  84. } else if (subtype == 2) {
  85. // MIDI IN Jack, midi 1.0, page 22
  86. println(" MIDI IN Jack (ignored)");
  87. } else if (subtype == 3) {
  88. // MIDI OUT Jack, midi 1.0, page 22
  89. println(" MIDI OUT Jack (ignored)");
  90. } else if (subtype == 4) {
  91. // Element Descriptor, midi 1.0, page 23-24
  92. println(" MIDI Element (ignored)");
  93. } else {
  94. return false; // unknown
  95. }
  96. } else if (type == 5) {
  97. // endpoint descriptor
  98. if (p[0] < 7) return false; // at least 7 bytes
  99. if (p[3] != 2) return false; // must be bulk type
  100. println(" MIDI Endpoint: ", p[2], HEX);
  101. switch (p[2] & 0xF0) {
  102. case 0x80:
  103. // IN endpoint
  104. if (rx_ep == 0) {
  105. rx_ep = p[2] & 0x0F;
  106. rx_size = p[4] | (p[5] << 8);
  107. println(" rx_size = ", rx_size);
  108. }
  109. break;
  110. case 0x00:
  111. // OUT endpoint
  112. if (tx_ep == 0) {
  113. tx_ep = p[2];
  114. tx_size = p[4] | (p[5] << 8);
  115. println(" tx_size = ", tx_size);
  116. }
  117. break;
  118. default:
  119. return false;
  120. }
  121. } else if (type == 37) {
  122. // MIDI endpoint info, midi 1.0: 6.2.2, page 26
  123. println(" MIDI Endpoint Jack Association (ignored)");
  124. } else {
  125. return false; // unknown
  126. }
  127. p += len;
  128. }
  129. // if an IN endpoint was found, create its pipe
  130. if (rx_ep && rx_size <= BUFFERSIZE) {
  131. rxpipe = new_Pipe(dev, 2, rx_ep, 1, rx_size);
  132. if (rxpipe) {
  133. rxpipe->callback_function = rx_callback;
  134. queue_Data_Transfer(rxpipe, buffer, rx_size, this);
  135. }
  136. } else {
  137. rxpipe = NULL;
  138. }
  139. // if an OUT endpoint was found, create its pipe
  140. if (tx_ep && tx_size <= BUFFERSIZE) {
  141. txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
  142. if (txpipe) {
  143. txpipe->callback_function = tx_callback;
  144. }
  145. } else {
  146. rxpipe = NULL;
  147. }
  148. // claim if either pipe created
  149. return (rxpipe || txpipe);
  150. }
  151. void MIDIDevice::rx_callback(const Transfer_t *transfer)
  152. {
  153. if (transfer->driver) {
  154. ((MIDIDevice *)(transfer->driver))->rx_data(transfer);
  155. }
  156. }
  157. void MIDIDevice::tx_callback(const Transfer_t *transfer)
  158. {
  159. if (transfer->driver) {
  160. ((MIDIDevice *)(transfer->driver))->tx_data(transfer);
  161. }
  162. }
  163. void MIDIDevice::rx_data(const Transfer_t *transfer)
  164. {
  165. println("MIDIDevice Receive");
  166. print(" MIDI Data: ");
  167. print_hexbytes(transfer->buffer, rx_size);
  168. // TODO: parse the new data
  169. queue_Data_Transfer(rxpipe, buffer, rx_size, this);
  170. }
  171. void MIDIDevice::tx_data(const Transfer_t *transfer)
  172. {
  173. println("MIDIDevice transmit complete");
  174. print(" MIDI Data: ");
  175. print_hexbytes(transfer->buffer, tx_size);
  176. // TODO: return the buffer to the pool...
  177. }
  178. void MIDIDevice::disconnect()
  179. {
  180. // TODO: free resources
  181. }