wavefront_midi.c 15 KB

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