wavefront_midi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) by Paul Barton-Davis 1998-1999
  4. */
  5. /* The low level driver for the WaveFront ICS2115 MIDI interface(s)
  6. *
  7. * Note that there is also an MPU-401 emulation (actually, a UART-401
  8. * emulation) on the CS4232 on the Tropez and Tropez Plus. This code
  9. * has nothing to do with that interface at all.
  10. *
  11. * The interface is essentially just a UART-401, but is has the
  12. * interesting property of supporting what Turtle Beach called
  13. * "Virtual MIDI" mode. In this mode, there are effectively *two*
  14. * MIDI buses accessible via the interface, one that is routed
  15. * solely to/from the external WaveFront synthesizer and the other
  16. * corresponding to the pin/socket connector used to link external
  17. * MIDI devices to the board.
  18. *
  19. * This driver fully supports this mode, allowing two distinct MIDI
  20. * busses to be used completely independently, giving 32 channels of
  21. * MIDI routing, 16 to the WaveFront synth and 16 to the external MIDI
  22. * bus. The devices are named /dev/snd/midiCnD0 and /dev/snd/midiCnD1,
  23. * where `n' is the card number. Note that the device numbers may be
  24. * something other than 0 and 1 if the CS4232 UART/MPU-401 interface
  25. * is enabled.
  26. *
  27. * Switching between the two is accomplished externally by the driver
  28. * using the two otherwise unused MIDI bytes. See the code for more details.
  29. *
  30. * NOTE: VIRTUAL MIDI MODE IS ON BY DEFAULT (see lowlevel/isa/wavefront.c)
  31. *
  32. * The main reason to turn off Virtual MIDI mode is when you want to
  33. * tightly couple the WaveFront synth with an external MIDI
  34. * device. You won't be able to distinguish the source of any MIDI
  35. * data except via SysEx ID, but thats probably OK, since for the most
  36. * part, the WaveFront won't be sending any MIDI data at all.
  37. *
  38. * The main reason to turn on Virtual MIDI Mode is to provide two
  39. * completely independent 16-channel MIDI buses, one to the
  40. * WaveFront and one to any external MIDI devices. Given the 32
  41. * voice nature of the WaveFront, its pretty easy to find a use
  42. * for all 16 channels driving just that synth.
  43. *
  44. */
  45. #include <linux/io.h>
  46. #include <linux/init.h>
  47. #include <linux/time.h>
  48. #include <linux/wait.h>
  49. #include <sound/core.h>
  50. #include <sound/snd_wavefront.h>
  51. static inline int
  52. wf_mpu_status (snd_wavefront_midi_t *midi)
  53. {
  54. return inb (midi->mpu_status_port);
  55. }
  56. static inline int
  57. input_avail (snd_wavefront_midi_t *midi)
  58. {
  59. return !(wf_mpu_status(midi) & INPUT_AVAIL);
  60. }
  61. static inline int
  62. output_ready (snd_wavefront_midi_t *midi)
  63. {
  64. return !(wf_mpu_status(midi) & OUTPUT_READY);
  65. }
  66. static inline int
  67. read_data (snd_wavefront_midi_t *midi)
  68. {
  69. return inb (midi->mpu_data_port);
  70. }
  71. static inline void
  72. write_data (snd_wavefront_midi_t *midi, unsigned char byte)
  73. {
  74. outb (byte, midi->mpu_data_port);
  75. }
  76. static snd_wavefront_midi_t *
  77. get_wavefront_midi (struct snd_rawmidi_substream *substream)
  78. {
  79. struct snd_card *card;
  80. snd_wavefront_card_t *acard;
  81. if (substream == NULL || substream->rmidi == NULL)
  82. return NULL;
  83. card = substream->rmidi->card;
  84. if (card == NULL)
  85. return NULL;
  86. if (card->private_data == NULL)
  87. return NULL;
  88. acard = card->private_data;
  89. return &acard->wavefront.midi;
  90. }
  91. static void snd_wavefront_midi_output_write(snd_wavefront_card_t *card)
  92. {
  93. snd_wavefront_midi_t *midi = &card->wavefront.midi;
  94. snd_wavefront_mpu_id mpu;
  95. unsigned long flags;
  96. unsigned char midi_byte;
  97. int max = 256, mask = 1;
  98. int timeout;
  99. /* Its not OK to try to change the status of "virtuality" of
  100. the MIDI interface while we're outputting stuff. See
  101. snd_wavefront_midi_{enable,disable}_virtual () for the
  102. other half of this.
  103. The first loop attempts to flush any data from the
  104. current output device, and then the second
  105. emits the switch byte (if necessary), and starts
  106. outputting data for the output device currently in use.
  107. */
  108. if (midi->substream_output[midi->output_mpu] == NULL) {
  109. goto __second;
  110. }
  111. while (max > 0) {
  112. /* XXX fix me - no hard timing loops allowed! */
  113. for (timeout = 30000; timeout > 0; timeout--) {
  114. if (output_ready (midi))
  115. break;
  116. }
  117. spin_lock_irqsave (&midi->virtual, flags);
  118. if ((midi->mode[midi->output_mpu] & MPU401_MODE_OUTPUT) == 0) {
  119. spin_unlock_irqrestore (&midi->virtual, flags);
  120. goto __second;
  121. }
  122. if (output_ready (midi)) {
  123. if (snd_rawmidi_transmit(midi->substream_output[midi->output_mpu], &midi_byte, 1) == 1) {
  124. if (!midi->isvirtual ||
  125. (midi_byte != WF_INTERNAL_SWITCH &&
  126. midi_byte != WF_EXTERNAL_SWITCH))
  127. write_data(midi, midi_byte);
  128. max--;
  129. } else {
  130. if (midi->istimer) {
  131. if (--midi->istimer <= 0)
  132. del_timer(&midi->timer);
  133. }
  134. midi->mode[midi->output_mpu] &= ~MPU401_MODE_OUTPUT_TRIGGER;
  135. spin_unlock_irqrestore (&midi->virtual, flags);
  136. goto __second;
  137. }
  138. } else {
  139. spin_unlock_irqrestore (&midi->virtual, flags);
  140. return;
  141. }
  142. spin_unlock_irqrestore (&midi->virtual, flags);
  143. }
  144. __second:
  145. if (midi->substream_output[!midi->output_mpu] == NULL) {
  146. return;
  147. }
  148. while (max > 0) {
  149. /* XXX fix me - no hard timing loops allowed! */
  150. for (timeout = 30000; timeout > 0; timeout--) {
  151. if (output_ready (midi))
  152. break;
  153. }
  154. spin_lock_irqsave (&midi->virtual, flags);
  155. if (!midi->isvirtual)
  156. mask = 0;
  157. mpu = midi->output_mpu ^ mask;
  158. mask = 0; /* don't invert the value from now */
  159. if ((midi->mode[mpu] & MPU401_MODE_OUTPUT) == 0) {
  160. spin_unlock_irqrestore (&midi->virtual, flags);
  161. return;
  162. }
  163. if (snd_rawmidi_transmit_empty(midi->substream_output[mpu]))
  164. goto __timer;
  165. if (output_ready (midi)) {
  166. if (mpu != midi->output_mpu) {
  167. write_data(midi, mpu == internal_mpu ?
  168. WF_INTERNAL_SWITCH :
  169. WF_EXTERNAL_SWITCH);
  170. midi->output_mpu = mpu;
  171. } else if (snd_rawmidi_transmit(midi->substream_output[mpu], &midi_byte, 1) == 1) {
  172. if (!midi->isvirtual ||
  173. (midi_byte != WF_INTERNAL_SWITCH &&
  174. midi_byte != WF_EXTERNAL_SWITCH))
  175. write_data(midi, midi_byte);
  176. max--;
  177. } else {
  178. __timer:
  179. if (midi->istimer) {
  180. if (--midi->istimer <= 0)
  181. del_timer(&midi->timer);
  182. }
  183. midi->mode[mpu] &= ~MPU401_MODE_OUTPUT_TRIGGER;
  184. spin_unlock_irqrestore (&midi->virtual, flags);
  185. return;
  186. }
  187. } else {
  188. spin_unlock_irqrestore (&midi->virtual, flags);
  189. return;
  190. }
  191. spin_unlock_irqrestore (&midi->virtual, flags);
  192. }
  193. }
  194. static int snd_wavefront_midi_input_open(struct snd_rawmidi_substream *substream)
  195. {
  196. unsigned long flags;
  197. snd_wavefront_midi_t *midi;
  198. snd_wavefront_mpu_id mpu;
  199. if (snd_BUG_ON(!substream || !substream->rmidi))
  200. return -ENXIO;
  201. if (snd_BUG_ON(!substream->rmidi->private_data))
  202. return -ENXIO;
  203. mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
  204. midi = get_wavefront_midi(substream);
  205. if (!midi)
  206. return -EIO;
  207. spin_lock_irqsave (&midi->open, flags);
  208. midi->mode[mpu] |= MPU401_MODE_INPUT;
  209. midi->substream_input[mpu] = substream;
  210. spin_unlock_irqrestore (&midi->open, flags);
  211. return 0;
  212. }
  213. static int snd_wavefront_midi_output_open(struct snd_rawmidi_substream *substream)
  214. {
  215. unsigned long flags;
  216. snd_wavefront_midi_t *midi;
  217. snd_wavefront_mpu_id mpu;
  218. if (snd_BUG_ON(!substream || !substream->rmidi))
  219. return -ENXIO;
  220. if (snd_BUG_ON(!substream->rmidi->private_data))
  221. return -ENXIO;
  222. mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
  223. midi = get_wavefront_midi(substream);
  224. if (!midi)
  225. return -EIO;
  226. spin_lock_irqsave (&midi->open, flags);
  227. midi->mode[mpu] |= MPU401_MODE_OUTPUT;
  228. midi->substream_output[mpu] = substream;
  229. spin_unlock_irqrestore (&midi->open, flags);
  230. return 0;
  231. }
  232. static int snd_wavefront_midi_input_close(struct snd_rawmidi_substream *substream)
  233. {
  234. unsigned long flags;
  235. snd_wavefront_midi_t *midi;
  236. snd_wavefront_mpu_id mpu;
  237. if (snd_BUG_ON(!substream || !substream->rmidi))
  238. return -ENXIO;
  239. if (snd_BUG_ON(!substream->rmidi->private_data))
  240. return -ENXIO;
  241. mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
  242. midi = get_wavefront_midi(substream);
  243. if (!midi)
  244. return -EIO;
  245. spin_lock_irqsave (&midi->open, flags);
  246. midi->mode[mpu] &= ~MPU401_MODE_INPUT;
  247. spin_unlock_irqrestore (&midi->open, flags);
  248. return 0;
  249. }
  250. static int snd_wavefront_midi_output_close(struct snd_rawmidi_substream *substream)
  251. {
  252. unsigned long flags;
  253. snd_wavefront_midi_t *midi;
  254. snd_wavefront_mpu_id mpu;
  255. if (snd_BUG_ON(!substream || !substream->rmidi))
  256. return -ENXIO;
  257. if (snd_BUG_ON(!substream->rmidi->private_data))
  258. return -ENXIO;
  259. mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
  260. midi = get_wavefront_midi(substream);
  261. if (!midi)
  262. return -EIO;
  263. spin_lock_irqsave (&midi->open, flags);
  264. midi->mode[mpu] &= ~MPU401_MODE_OUTPUT;
  265. spin_unlock_irqrestore (&midi->open, flags);
  266. return 0;
  267. }
  268. static void snd_wavefront_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  269. {
  270. unsigned long flags;
  271. snd_wavefront_midi_t *midi;
  272. snd_wavefront_mpu_id mpu;
  273. if (substream == NULL || substream->rmidi == NULL)
  274. return;
  275. if (substream->rmidi->private_data == NULL)
  276. return;
  277. mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
  278. midi = get_wavefront_midi(substream);
  279. if (!midi)
  280. return;
  281. spin_lock_irqsave (&midi->virtual, flags);
  282. if (up) {
  283. midi->mode[mpu] |= MPU401_MODE_INPUT_TRIGGER;
  284. } else {
  285. midi->mode[mpu] &= ~MPU401_MODE_INPUT_TRIGGER;
  286. }
  287. spin_unlock_irqrestore (&midi->virtual, flags);
  288. }
  289. static void snd_wavefront_midi_output_timer(struct timer_list *t)
  290. {
  291. snd_wavefront_midi_t *midi = from_timer(midi, t, timer);
  292. snd_wavefront_card_t *card = midi->timer_card;
  293. unsigned long flags;
  294. spin_lock_irqsave (&midi->virtual, flags);
  295. mod_timer(&midi->timer, 1 + jiffies);
  296. spin_unlock_irqrestore (&midi->virtual, flags);
  297. snd_wavefront_midi_output_write(card);
  298. }
  299. static void snd_wavefront_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  300. {
  301. unsigned long flags;
  302. snd_wavefront_midi_t *midi;
  303. snd_wavefront_mpu_id mpu;
  304. if (substream == NULL || substream->rmidi == NULL)
  305. return;
  306. if (substream->rmidi->private_data == NULL)
  307. return;
  308. mpu = *((snd_wavefront_mpu_id *) substream->rmidi->private_data);
  309. midi = get_wavefront_midi(substream);
  310. if (!midi)
  311. return;
  312. spin_lock_irqsave (&midi->virtual, flags);
  313. if (up) {
  314. if ((midi->mode[mpu] & MPU401_MODE_OUTPUT_TRIGGER) == 0) {
  315. if (!midi->istimer) {
  316. timer_setup(&midi->timer,
  317. snd_wavefront_midi_output_timer,
  318. 0);
  319. mod_timer(&midi->timer, 1 + jiffies);
  320. }
  321. midi->istimer++;
  322. midi->mode[mpu] |= MPU401_MODE_OUTPUT_TRIGGER;
  323. }
  324. } else {
  325. midi->mode[mpu] &= ~MPU401_MODE_OUTPUT_TRIGGER;
  326. }
  327. spin_unlock_irqrestore (&midi->virtual, flags);
  328. if (up)
  329. snd_wavefront_midi_output_write((snd_wavefront_card_t *)substream->rmidi->card->private_data);
  330. }
  331. void
  332. snd_wavefront_midi_interrupt (snd_wavefront_card_t *card)
  333. {
  334. unsigned long flags;
  335. snd_wavefront_midi_t *midi;
  336. static struct snd_rawmidi_substream *substream = NULL;
  337. static int mpu = external_mpu;
  338. int max = 128;
  339. unsigned char byte;
  340. midi = &card->wavefront.midi;
  341. if (!input_avail (midi)) { /* not for us */
  342. snd_wavefront_midi_output_write(card);
  343. return;
  344. }
  345. spin_lock_irqsave (&midi->virtual, flags);
  346. while (--max) {
  347. if (input_avail (midi)) {
  348. byte = read_data (midi);
  349. if (midi->isvirtual) {
  350. if (byte == WF_EXTERNAL_SWITCH) {
  351. substream = midi->substream_input[external_mpu];
  352. mpu = external_mpu;
  353. } else if (byte == WF_INTERNAL_SWITCH) {
  354. substream = midi->substream_output[internal_mpu];
  355. mpu = internal_mpu;
  356. } /* else just leave it as it is */
  357. } else {
  358. substream = midi->substream_input[internal_mpu];
  359. mpu = internal_mpu;
  360. }
  361. if (substream == NULL) {
  362. continue;
  363. }
  364. if (midi->mode[mpu] & MPU401_MODE_INPUT_TRIGGER) {
  365. snd_rawmidi_receive(substream, &byte, 1);
  366. }
  367. } else {
  368. break;
  369. }
  370. }
  371. spin_unlock_irqrestore (&midi->virtual, flags);
  372. snd_wavefront_midi_output_write(card);
  373. }
  374. void
  375. snd_wavefront_midi_enable_virtual (snd_wavefront_card_t *card)
  376. {
  377. unsigned long flags;
  378. spin_lock_irqsave (&card->wavefront.midi.virtual, flags);
  379. card->wavefront.midi.isvirtual = 1;
  380. card->wavefront.midi.output_mpu = internal_mpu;
  381. card->wavefront.midi.input_mpu = internal_mpu;
  382. spin_unlock_irqrestore (&card->wavefront.midi.virtual, flags);
  383. }
  384. void
  385. snd_wavefront_midi_disable_virtual (snd_wavefront_card_t *card)
  386. {
  387. unsigned long flags;
  388. spin_lock_irqsave (&card->wavefront.midi.virtual, flags);
  389. // snd_wavefront_midi_input_close (card->ics2115_external_rmidi);
  390. // snd_wavefront_midi_output_close (card->ics2115_external_rmidi);
  391. card->wavefront.midi.isvirtual = 0;
  392. spin_unlock_irqrestore (&card->wavefront.midi.virtual, flags);
  393. }
  394. int
  395. snd_wavefront_midi_start (snd_wavefront_card_t *card)
  396. {
  397. int ok, i;
  398. unsigned char rbuf[4], wbuf[4];
  399. snd_wavefront_t *dev;
  400. snd_wavefront_midi_t *midi;
  401. dev = &card->wavefront;
  402. midi = &dev->midi;
  403. /* The ICS2115 MPU-401 interface doesn't do anything
  404. until its set into UART mode.
  405. */
  406. /* XXX fix me - no hard timing loops allowed! */
  407. for (i = 0; i < 30000 && !output_ready (midi); i++);
  408. if (!output_ready (midi)) {
  409. snd_printk ("MIDI interface not ready for command\n");
  410. return -1;
  411. }
  412. /* Any interrupts received from now on
  413. are owned by the MIDI side of things.
  414. */
  415. dev->interrupts_are_midi = 1;
  416. outb (UART_MODE_ON, midi->mpu_command_port);
  417. for (ok = 0, i = 50000; i > 0 && !ok; i--) {
  418. if (input_avail (midi)) {
  419. if (read_data (midi) == MPU_ACK) {
  420. ok = 1;
  421. break;
  422. }
  423. }
  424. }
  425. if (!ok) {
  426. snd_printk ("cannot set UART mode for MIDI interface");
  427. dev->interrupts_are_midi = 0;
  428. return -1;
  429. }
  430. /* Route external MIDI to WaveFront synth (by default) */
  431. if (snd_wavefront_cmd (dev, WFC_MISYNTH_ON, rbuf, wbuf)) {
  432. snd_printk ("can't enable MIDI-IN-2-synth routing.\n");
  433. /* XXX error ? */
  434. }
  435. /* Turn on Virtual MIDI, but first *always* turn it off,
  436. since otherwise consecutive reloads of the driver will
  437. never cause the hardware to generate the initial "internal" or
  438. "external" source bytes in the MIDI data stream. This
  439. is pretty important, since the internal hardware generally will
  440. be used to generate none or very little MIDI output, and
  441. thus the only source of MIDI data is actually external. Without
  442. the switch bytes, the driver will think it all comes from
  443. the internal interface. Duh.
  444. */
  445. if (snd_wavefront_cmd (dev, WFC_VMIDI_OFF, rbuf, wbuf)) {
  446. snd_printk ("virtual MIDI mode not disabled\n");
  447. return 0; /* We're OK, but missing the external MIDI dev */
  448. }
  449. snd_wavefront_midi_enable_virtual (card);
  450. if (snd_wavefront_cmd (dev, WFC_VMIDI_ON, rbuf, wbuf)) {
  451. snd_printk ("cannot enable virtual MIDI mode.\n");
  452. snd_wavefront_midi_disable_virtual (card);
  453. }
  454. return 0;
  455. }
  456. const struct snd_rawmidi_ops snd_wavefront_midi_output =
  457. {
  458. .open = snd_wavefront_midi_output_open,
  459. .close = snd_wavefront_midi_output_close,
  460. .trigger = snd_wavefront_midi_output_trigger,
  461. };
  462. const struct snd_rawmidi_ops snd_wavefront_midi_input =
  463. {
  464. .open = snd_wavefront_midi_input_open,
  465. .close = snd_wavefront_midi_input_close,
  466. .trigger = snd_wavefront_midi_input_trigger,
  467. };