Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

573 lines
16KB

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