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.

564 lines
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. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #define print USBHost::print_
  26. #define println USBHost::println_
  27. void MIDIDevice::init()
  28. {
  29. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  30. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  31. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  32. handleNoteOff = NULL;
  33. handleNoteOn = NULL;
  34. handleVelocityChange = NULL;
  35. handleControlChange = NULL;
  36. handleProgramChange = NULL;
  37. handleAfterTouch = NULL;
  38. handlePitchChange = NULL;
  39. handleSysExPartial = NULL;
  40. handleSysExComplete = NULL;
  41. handleTimeCodeQuarterFrame = NULL;
  42. handleSongPosition = NULL;
  43. handleSongSelect = NULL;
  44. handleTuneRequest = NULL;
  45. handleClock = NULL;
  46. handleStart = NULL;
  47. handleContinue = NULL;
  48. handleStop = NULL;
  49. handleActiveSensing = NULL;
  50. handleSystemReset = NULL;
  51. handleRealTimeSystem = NULL;
  52. rx_head = 0;
  53. rx_tail = 0;
  54. rxpipe = NULL;
  55. txpipe = NULL;
  56. driver_ready_for_device(this);
  57. }
  58. // Audio Class-Specific Descriptor Types (audio 1.0, page 99)
  59. // CS_UNDEFINED 0x20
  60. // CS_DEVICE 0x21
  61. // CS_CONFIGURATION 0x22
  62. // CS_STRING 0x23
  63. // CS_INTERFACE 0x24
  64. // CS_ENDPOINT 0x25
  65. // MS Class-Specific Interface Descriptor Subtypes (midi 1.0, page 36)
  66. // MS_DESCRIPTOR_UNDEFINED 0x00
  67. // MS_HEADER 0x01
  68. // MIDI_IN_JACK 0x02
  69. // MIDI_OUT_JACK 0x03
  70. // ELEMENT 0x04
  71. // MS Class-Specific Endpoint Descriptor Subtypes (midi 1.0, page 36)
  72. // DESCRIPTOR_UNDEFINED 0x00
  73. // MS_GENERAL 0x01
  74. // MS MIDI IN and OUT Jack types (midi 1.0, page 36)
  75. // JACK_TYPE_UNDEFINED 0x00
  76. // EMBEDDED 0x01
  77. // EXTERNAL 0x02
  78. // Endpoint Control Selectors (midi 1.0, page 36)
  79. // EP_CONTROL_UNDEFINED 0x00
  80. // ASSOCIATION_CONTROL 0x01
  81. bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  82. {
  83. // only claim at interface level
  84. if (type != 1) return false;
  85. println("MIDIDevice claim this=", (uint32_t)this, HEX);
  86. println("len = ", len);
  87. const uint8_t *p = descriptors;
  88. const uint8_t *end = p + len;
  89. if (p[0] != 9 || p[1] != 4) return false; // interface descriptor
  90. //println(" bInterfaceClass=", p[5]);
  91. //println(" bInterfaceSubClass=", p[6]);
  92. if (p[5] != 1) return false; // bInterfaceClass: 1 = Audio class
  93. if (p[6] != 3) return false; // bInterfaceSubClass: 3 = MIDI
  94. p += 9;
  95. println(" Interface is MIDI");
  96. rx_ep = 0;
  97. tx_ep = 0;
  98. while (p < end) {
  99. len = *p;
  100. if (len < 4) return false; // all audio desc are at least 4 bytes
  101. if (p + len > end) return false; // reject if beyond end of data
  102. uint32_t type = p[1];
  103. //println("type: ", type);
  104. if (type == 4 || type == 11) break; // interface or IAD, not for us
  105. if (type == 0x24) { // 0x24 = Audio CS_INTERFACE, audio 1.0, page 99
  106. uint32_t subtype = p[2];
  107. //println("subtype: ", subtype);
  108. if (subtype == 1) {
  109. // Interface Header, midi 1.0, page 21
  110. println(" MIDI Header (ignored)");
  111. } else if (subtype == 2) {
  112. // MIDI IN Jack, midi 1.0, page 22
  113. println(" MIDI IN Jack (ignored)");
  114. } else if (subtype == 3) {
  115. // MIDI OUT Jack, midi 1.0, page 22
  116. println(" MIDI OUT Jack (ignored)");
  117. } else if (subtype == 4) {
  118. // Element Descriptor, midi 1.0, page 23-24
  119. println(" MIDI Element (ignored)");
  120. } else {
  121. return false; // unknown
  122. }
  123. } else if (type == 5) {
  124. // endpoint descriptor
  125. if (p[0] < 7) return false; // at least 7 bytes
  126. if (p[3] != 2) return false; // must be bulk type
  127. println(" MIDI Endpoint: ", p[2], HEX);
  128. switch (p[2] & 0xF0) {
  129. case 0x80:
  130. // IN endpoint
  131. if (rx_ep == 0) {
  132. rx_ep = p[2] & 0x0F;
  133. rx_size = p[4] | (p[5] << 8);
  134. println(" rx_size = ", rx_size);
  135. }
  136. break;
  137. case 0x00:
  138. // OUT endpoint
  139. if (tx_ep == 0) {
  140. tx_ep = p[2];
  141. tx_size = p[4] | (p[5] << 8);
  142. println(" tx_size = ", tx_size);
  143. }
  144. break;
  145. default:
  146. return false;
  147. }
  148. } else if (type == 37) {
  149. // MIDI endpoint info, midi 1.0: 6.2.2, page 26
  150. println(" MIDI Endpoint Jack Association (ignored)");
  151. } else {
  152. return false; // unknown
  153. }
  154. p += len;
  155. }
  156. // if an IN endpoint was found, create its pipe
  157. if (rx_ep && rx_size <= MAX_PACKET_SIZE) {
  158. rxpipe = new_Pipe(dev, 2, rx_ep, 1, rx_size);
  159. if (rxpipe) {
  160. rxpipe->callback_function = rx_callback;
  161. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  162. rx_packet_queued = true;
  163. }
  164. } else {
  165. rxpipe = NULL;
  166. }
  167. // if an OUT endpoint was found, create its pipe
  168. if (tx_ep && tx_size <= MAX_PACKET_SIZE) {
  169. txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
  170. if (txpipe) {
  171. txpipe->callback_function = tx_callback;
  172. tx1_count = 0;
  173. tx2_count = 0;
  174. }
  175. } else {
  176. txpipe = NULL;
  177. }
  178. rx_head = 0;
  179. rx_tail = 0;
  180. msg_channel = 0;
  181. msg_type = 0;
  182. msg_data1 = 0;
  183. msg_data2 = 0;
  184. msg_sysex_len = 0;
  185. // claim if either pipe created
  186. return (rxpipe || txpipe);
  187. }
  188. void MIDIDevice::rx_callback(const Transfer_t *transfer)
  189. {
  190. if (transfer->driver) {
  191. ((MIDIDevice *)(transfer->driver))->rx_data(transfer);
  192. }
  193. }
  194. void MIDIDevice::tx_callback(const Transfer_t *transfer)
  195. {
  196. if (transfer->driver) {
  197. ((MIDIDevice *)(transfer->driver))->tx_data(transfer);
  198. }
  199. }
  200. void MIDIDevice::rx_data(const Transfer_t *transfer)
  201. {
  202. println("MIDIDevice Receive");
  203. print(" MIDI Data: ");
  204. print_hexbytes(transfer->buffer, rx_size);
  205. uint32_t head = rx_head;
  206. uint32_t tail = rx_tail;
  207. uint32_t len = (transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF)) >> 2;
  208. for (uint32_t i=0; i < len; i++) {
  209. uint32_t msg = rx_buffer[i];
  210. if (msg) {
  211. if (++head >= RX_QUEUE_SIZE) head = 0;
  212. rx_queue[head] = msg;
  213. }
  214. }
  215. rx_head = head;
  216. rx_tail = tail;
  217. uint32_t avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  218. println("rx_size = ", rx_size);
  219. println("avail = ", avail);
  220. if (avail >= (uint32_t)(rx_size>>2)) {
  221. // enough space to accept another full packet
  222. println("queue another receive packet");
  223. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  224. rx_packet_queued = true;
  225. } else {
  226. // queue can't accept another packet's data, so leave
  227. // the data waiting on the device until we can accept it
  228. println("wait to receive more packets");
  229. rx_packet_queued = false;
  230. }
  231. }
  232. void MIDIDevice::tx_data(const Transfer_t *transfer)
  233. {
  234. println("MIDIDevice transmit complete");
  235. print(" MIDI Data: ");
  236. print_hexbytes(transfer->buffer, tx_size);
  237. if (transfer->buffer == tx_buffer1) {
  238. tx1_count = 0;
  239. } else if (transfer->buffer == tx_buffer2) {
  240. tx2_count = 0;
  241. }
  242. }
  243. void MIDIDevice::disconnect()
  244. {
  245. // should rx_queue be cleared?
  246. // as-is, the user can still read MIDI messages
  247. // which arrived before the device disconnected.
  248. rxpipe = NULL;
  249. txpipe = NULL;
  250. }
  251. void MIDIDevice::write_packed(uint32_t data)
  252. {
  253. if (!txpipe) return;
  254. uint32_t tx_max = tx_size / 4;
  255. while (1) {
  256. uint32_t tx1 = tx1_count;
  257. uint32_t tx2 = tx2_count;
  258. if (tx1 < tx_max && (tx2 == 0 || tx2 >= tx_max)) {
  259. // use tx_buffer1
  260. tx_buffer1[tx1++] = data;
  261. tx1_count = tx1;
  262. if (tx1 >= tx_max) {
  263. queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
  264. } else {
  265. // TODO: start a timer, rather than sending the buffer
  266. // before it's full, to make best use of bandwidth
  267. tx1_count = tx_max;
  268. queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
  269. }
  270. return;
  271. }
  272. if (tx2 < tx_max) {
  273. // use tx_buffer2
  274. tx_buffer2[tx2++] = data;
  275. tx2_count = tx2;
  276. if (tx2 >= tx_max) {
  277. queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
  278. } else {
  279. // TODO: start a timer, rather than sending the buffer
  280. // before it's full, to make best use of bandwidth
  281. tx2_count = tx_max;
  282. queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
  283. }
  284. return;
  285. }
  286. }
  287. }
  288. void MIDIDevice::send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
  289. {
  290. cable = (cable & 0x0F) << 4;
  291. while (length > 3) {
  292. write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  293. data += 3;
  294. length -= 3;
  295. }
  296. if (length == 3) {
  297. write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  298. } else if (length == 2) {
  299. write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  300. } else if (length == 1) {
  301. write_packed(0x05 | cable | (data[0] << 8));
  302. }
  303. }
  304. void MIDIDevice::send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
  305. {
  306. cable = (cable & 0x0F) << 4;
  307. if (length == 0) {
  308. write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
  309. return;
  310. } else if (length == 1) {
  311. write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
  312. return;
  313. } else {
  314. write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
  315. data += 2;
  316. length -= 2;
  317. }
  318. while (length >= 3) {
  319. write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  320. data += 3;
  321. length -= 3;
  322. }
  323. if (length == 2) {
  324. write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
  325. } else if (length == 1) {
  326. write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
  327. } else {
  328. write_packed(0x05 | cable | (0xF7 << 8));
  329. }
  330. }
  331. bool MIDIDevice::read(uint8_t channel)
  332. {
  333. uint32_t n, head, tail, avail, ch, type1, type2;
  334. head = rx_head;
  335. tail = rx_tail;
  336. if (head == tail) return false;
  337. if (++tail >= RX_QUEUE_SIZE) tail = 0;
  338. n = rx_queue[tail];
  339. rx_tail = tail;
  340. if (!rx_packet_queued && rxpipe) {
  341. avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  342. if (avail >= (uint32_t)(rx_size>>2)) {
  343. __disable_irq();
  344. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  345. __enable_irq();
  346. }
  347. }
  348. println("read: ", n, HEX);
  349. type1 = n & 15;
  350. type2 = (n >> 12) & 15;
  351. ch = ((n >> 8) & 15) + 1;
  352. msg_cable = (n >> 4) & 15;
  353. if (type1 >= 0x08 && type1 <= 0x0E) {
  354. if (channel && channel != ch) {
  355. // ignore other channels when user wants single channel read
  356. return false;
  357. }
  358. if (type1 == 0x08 && type2 == 0x08) {
  359. msg_type = 0x80; // 0x80 = Note off
  360. if (handleNoteOff) {
  361. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  362. }
  363. } else
  364. if (type1 == 0x09 && type2 == 0x09) {
  365. if ((n >> 24) > 0) {
  366. msg_type = 0x9; // 0x9 = Note on
  367. if (handleNoteOn) {
  368. (*handleNoteOn)(ch, (n >> 16), (n >> 24));
  369. }
  370. } else {
  371. msg_type = 0x8; // 0x8 = Note off
  372. if (handleNoteOff) {
  373. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  374. }
  375. }
  376. } else
  377. if (type1 == 0x0A && type2 == 0x0A) {
  378. msg_type = 0xA0; // 0xA0 = AfterTouchPoly
  379. if (handleVelocityChange) {
  380. (*handleVelocityChange)(ch, (n >> 16), (n >> 24));
  381. }
  382. } else
  383. if (type1 == 0x0B && type2 == 0x0B) {
  384. msg_type = 0xB0; // 0xB0 = Control Change
  385. if (handleControlChange) {
  386. (*handleControlChange)(ch, (n >> 16), (n >> 24));
  387. }
  388. } else
  389. if (type1 == 0x0C && type2 == 0x0C) {
  390. msg_type = 0xC0; // 0xC0 = Program Change
  391. if (handleProgramChange) {
  392. (*handleProgramChange)(ch, (n >> 16));
  393. }
  394. } else
  395. if (type1 == 0x0D && type2 == 0x0D) {
  396. msg_type = 0xD0; // 0xD0 = After Touch
  397. if (handleAfterTouch) {
  398. (*handleAfterTouch)(ch, (n >> 16));
  399. }
  400. } else
  401. if (type1 == 0x0E && type2 == 0x0E) {
  402. msg_type = 0xE0; // 0xE0 = Pitch Bend
  403. if (handlePitchChange) {
  404. (*handlePitchChange)(ch, ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  405. }
  406. } else {
  407. return false;
  408. }
  409. return_message:
  410. msg_channel = ch;
  411. msg_data1 = (n >> 16);
  412. msg_data2 = (n >> 24);
  413. return true;
  414. }
  415. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && type2 == 0x0F)) {
  416. // system common or system realtime message
  417. uint8_t type;
  418. system_common_or_realtime:
  419. type = n >> 8;
  420. switch (type) {
  421. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  422. if (handleTimeCodeQuarterFrame) {
  423. (*handleTimeCodeQuarterFrame)(n >> 16);
  424. }
  425. break;
  426. case 0xF2: // usbMIDI.SongPosition
  427. if (handleSongPosition) {
  428. (*handleSongPosition)(((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  429. }
  430. break;
  431. case 0xF3: // usbMIDI.SongSelect
  432. if (handleSongSelect) {
  433. (*handleSongSelect)(n >> 16);
  434. }
  435. break;
  436. case 0xF6: // usbMIDI.TuneRequest
  437. if (handleTuneRequest) {
  438. (*handleTuneRequest)();
  439. }
  440. break;
  441. case 0xF8: // usbMIDI.Clock
  442. if (handleClock) {
  443. (*handleClock)();
  444. } else if (handleRealTimeSystem) {
  445. (*handleRealTimeSystem)(0xF8);
  446. }
  447. break;
  448. case 0xFA: // usbMIDI.Start
  449. if (handleStart) {
  450. (*handleStart)();
  451. } else if (handleRealTimeSystem) {
  452. (*handleRealTimeSystem)(0xFA);
  453. }
  454. break;
  455. case 0xFB: // usbMIDI.Continue
  456. if (handleContinue) {
  457. (*handleContinue)();
  458. } else if (handleRealTimeSystem) {
  459. (*handleRealTimeSystem)(0xFB);
  460. }
  461. break;
  462. case 0xFC: // usbMIDI.Stop
  463. if (handleStop) {
  464. (*handleStop)();
  465. } else if (handleRealTimeSystem) {
  466. (*handleRealTimeSystem)(0xFC);
  467. }
  468. break;
  469. case 0xFE: // usbMIDI.ActiveSensing
  470. if (handleActiveSensing) {
  471. (*handleActiveSensing)();
  472. } else if (handleRealTimeSystem) {
  473. (*handleRealTimeSystem)(0xFE);
  474. }
  475. break;
  476. case 0xFF: // usbMIDI.SystemReset
  477. if (handleSystemReset) {
  478. (*handleSystemReset)();
  479. } else if (handleRealTimeSystem) {
  480. (*handleRealTimeSystem)(0xFF);
  481. }
  482. break;
  483. default:
  484. return false; // unknown message, ignore it
  485. }
  486. msg_type = type;
  487. goto return_message;
  488. }
  489. if (type1 == 0x04) {
  490. sysex_byte(n >> 8);
  491. sysex_byte(n >> 16);
  492. sysex_byte(n >> 24);
  493. return false;
  494. }
  495. if (type1 >= 0x05 && type1 <= 0x07) {
  496. sysex_byte(n >> 8);
  497. if (type1 >= 0x06) sysex_byte(n >> 16);
  498. if (type1 == 0x07) sysex_byte(n >> 24);
  499. uint16_t len = msg_sysex_len;
  500. msg_data1 = len;
  501. msg_data2 = len >> 8;
  502. msg_sysex_len = 0;
  503. msg_type = 0xF0; // 0xF0 = SystemExclusive
  504. if (handleSysExPartial) {
  505. (*handleSysExPartial)(msg_sysex, len, 1);
  506. } else if (handleSysExComplete) {
  507. (*handleSysExComplete)(msg_sysex, len);
  508. }
  509. return true;
  510. }
  511. if (type1 == 0x0F) {
  512. uint8_t b = n >> 8;
  513. if (b >= 0xF8) {
  514. goto system_common_or_realtime;
  515. }
  516. if (msg_sysex_len > 0) {
  517. // Is this really needed? Mac OS-X does this, but do any devices?
  518. sysex_byte(n >> 8);
  519. }
  520. }
  521. return false;
  522. }
  523. void MIDIDevice::sysex_byte(uint8_t b)
  524. {
  525. if (handleSysExPartial && msg_sysex_len >= SYSEX_MAX_LEN) {
  526. // when buffer is full, send another chunk to partial handler.
  527. (*handleSysExPartial)(msg_sysex, msg_sysex_len, 0);
  528. msg_sysex_len = 0;
  529. }
  530. if (msg_sysex_len < SYSEX_MAX_LEN) {
  531. msg_sysex[msg_sysex_len++] = b;
  532. }
  533. }