ca_midi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright 10/16/2005 Tilman Kranz <tilde@tk-sls.de>
  3. * Creative Audio MIDI, for the CA0106 Driver
  4. * Version: 0.0.1
  5. *
  6. * Changelog:
  7. * Implementation is based on mpu401 and emu10k1x and
  8. * tested with ca0106.
  9. * mpu401: Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  10. * emu10k1x: Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. *
  27. */
  28. #include <linux/spinlock.h>
  29. #include <sound/core.h>
  30. #include <sound/rawmidi.h>
  31. #include "ca_midi.h"
  32. #define ca_midi_write_data(midi, data) midi->write(midi, data, 0)
  33. #define ca_midi_write_cmd(midi, data) midi->write(midi, data, 1)
  34. #define ca_midi_read_data(midi) midi->read(midi, 0)
  35. #define ca_midi_read_stat(midi) midi->read(midi, 1)
  36. #define ca_midi_input_avail(midi) (!(ca_midi_read_stat(midi) & midi->input_avail))
  37. #define ca_midi_output_ready(midi) (!(ca_midi_read_stat(midi) & midi->output_ready))
  38. static void ca_midi_clear_rx(struct snd_ca_midi *midi)
  39. {
  40. int timeout = 100000;
  41. for (; timeout > 0 && ca_midi_input_avail(midi); timeout--)
  42. ca_midi_read_data(midi);
  43. #ifdef CONFIG_SND_DEBUG
  44. if (timeout <= 0)
  45. snd_printk(KERN_ERR "ca_midi_clear_rx: timeout (status = 0x%x)\n",
  46. ca_midi_read_stat(midi));
  47. #endif
  48. }
  49. static void ca_midi_interrupt(struct snd_ca_midi *midi, unsigned int status)
  50. {
  51. unsigned char byte;
  52. if (midi->rmidi == NULL) {
  53. midi->interrupt_disable(midi,midi->tx_enable | midi->rx_enable);
  54. return;
  55. }
  56. spin_lock(&midi->input_lock);
  57. if ((status & midi->ipr_rx) && ca_midi_input_avail(midi)) {
  58. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  59. ca_midi_clear_rx(midi);
  60. } else {
  61. byte = ca_midi_read_data(midi);
  62. if(midi->substream_input)
  63. snd_rawmidi_receive(midi->substream_input, &byte, 1);
  64. }
  65. }
  66. spin_unlock(&midi->input_lock);
  67. spin_lock(&midi->output_lock);
  68. if ((status & midi->ipr_tx) && ca_midi_output_ready(midi)) {
  69. if (midi->substream_output &&
  70. snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
  71. ca_midi_write_data(midi, byte);
  72. } else {
  73. midi->interrupt_disable(midi,midi->tx_enable);
  74. }
  75. }
  76. spin_unlock(&midi->output_lock);
  77. }
  78. static void ca_midi_cmd(struct snd_ca_midi *midi, unsigned char cmd, int ack)
  79. {
  80. unsigned long flags;
  81. int timeout, ok;
  82. spin_lock_irqsave(&midi->input_lock, flags);
  83. ca_midi_write_data(midi, 0x00);
  84. /* ca_midi_clear_rx(midi); */
  85. ca_midi_write_cmd(midi, cmd);
  86. if (ack) {
  87. ok = 0;
  88. timeout = 10000;
  89. while (!ok && timeout-- > 0) {
  90. if (ca_midi_input_avail(midi)) {
  91. if (ca_midi_read_data(midi) == midi->ack)
  92. ok = 1;
  93. }
  94. }
  95. if (!ok && ca_midi_read_data(midi) == midi->ack)
  96. ok = 1;
  97. } else {
  98. ok = 1;
  99. }
  100. spin_unlock_irqrestore(&midi->input_lock, flags);
  101. if (!ok)
  102. snd_printk(KERN_ERR "ca_midi_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n",
  103. cmd,
  104. midi->get_dev_id_port(midi->dev_id),
  105. ca_midi_read_stat(midi),
  106. ca_midi_read_data(midi));
  107. }
  108. static int ca_midi_input_open(struct snd_rawmidi_substream *substream)
  109. {
  110. struct snd_ca_midi *midi = substream->rmidi->private_data;
  111. unsigned long flags;
  112. snd_assert(midi->dev_id, return -ENXIO);
  113. spin_lock_irqsave(&midi->open_lock, flags);
  114. midi->midi_mode |= CA_MIDI_MODE_INPUT;
  115. midi->substream_input = substream;
  116. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
  117. spin_unlock_irqrestore(&midi->open_lock, flags);
  118. ca_midi_cmd(midi, midi->reset, 1);
  119. ca_midi_cmd(midi, midi->enter_uart, 1);
  120. } else {
  121. spin_unlock_irqrestore(&midi->open_lock, flags);
  122. }
  123. return 0;
  124. }
  125. static int ca_midi_output_open(struct snd_rawmidi_substream *substream)
  126. {
  127. struct snd_ca_midi *midi = substream->rmidi->private_data;
  128. unsigned long flags;
  129. snd_assert(midi->dev_id, return -ENXIO);
  130. spin_lock_irqsave(&midi->open_lock, flags);
  131. midi->midi_mode |= CA_MIDI_MODE_OUTPUT;
  132. midi->substream_output = substream;
  133. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  134. spin_unlock_irqrestore(&midi->open_lock, flags);
  135. ca_midi_cmd(midi, midi->reset, 1);
  136. ca_midi_cmd(midi, midi->enter_uart, 1);
  137. } else {
  138. spin_unlock_irqrestore(&midi->open_lock, flags);
  139. }
  140. return 0;
  141. }
  142. static int ca_midi_input_close(struct snd_rawmidi_substream *substream)
  143. {
  144. struct snd_ca_midi *midi = substream->rmidi->private_data;
  145. unsigned long flags;
  146. snd_assert(midi->dev_id, return -ENXIO);
  147. spin_lock_irqsave(&midi->open_lock, flags);
  148. midi->interrupt_disable(midi,midi->rx_enable);
  149. midi->midi_mode &= ~CA_MIDI_MODE_INPUT;
  150. midi->substream_input = NULL;
  151. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
  152. spin_unlock_irqrestore(&midi->open_lock, flags);
  153. ca_midi_cmd(midi, midi->reset, 0);
  154. } else {
  155. spin_unlock_irqrestore(&midi->open_lock, flags);
  156. }
  157. return 0;
  158. }
  159. static int ca_midi_output_close(struct snd_rawmidi_substream *substream)
  160. {
  161. struct snd_ca_midi *midi = substream->rmidi->private_data;
  162. unsigned long flags;
  163. snd_assert(midi->dev_id, return -ENXIO);
  164. spin_lock_irqsave(&midi->open_lock, flags);
  165. midi->interrupt_disable(midi,midi->tx_enable);
  166. midi->midi_mode &= ~CA_MIDI_MODE_OUTPUT;
  167. midi->substream_output = NULL;
  168. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  169. spin_unlock_irqrestore(&midi->open_lock, flags);
  170. ca_midi_cmd(midi, midi->reset, 0);
  171. } else {
  172. spin_unlock_irqrestore(&midi->open_lock, flags);
  173. }
  174. return 0;
  175. }
  176. static void ca_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  177. {
  178. struct snd_ca_midi *midi = substream->rmidi->private_data;
  179. snd_assert(midi->dev_id, return);
  180. if (up) {
  181. midi->interrupt_enable(midi,midi->rx_enable);
  182. } else {
  183. midi->interrupt_disable(midi, midi->rx_enable);
  184. }
  185. }
  186. static void ca_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  187. {
  188. struct snd_ca_midi *midi = substream->rmidi->private_data;
  189. unsigned long flags;
  190. snd_assert(midi->dev_id, return);
  191. if (up) {
  192. int max = 4;
  193. unsigned char byte;
  194. spin_lock_irqsave(&midi->output_lock, flags);
  195. /* try to send some amount of bytes here before interrupts */
  196. while (max > 0) {
  197. if (ca_midi_output_ready(midi)) {
  198. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT) ||
  199. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  200. /* no more data */
  201. spin_unlock_irqrestore(&midi->output_lock, flags);
  202. return;
  203. }
  204. ca_midi_write_data(midi, byte);
  205. max--;
  206. } else {
  207. break;
  208. }
  209. }
  210. spin_unlock_irqrestore(&midi->output_lock, flags);
  211. midi->interrupt_enable(midi,midi->tx_enable);
  212. } else {
  213. midi->interrupt_disable(midi,midi->tx_enable);
  214. }
  215. }
  216. static struct snd_rawmidi_ops ca_midi_output =
  217. {
  218. .open = ca_midi_output_open,
  219. .close = ca_midi_output_close,
  220. .trigger = ca_midi_output_trigger,
  221. };
  222. static struct snd_rawmidi_ops ca_midi_input =
  223. {
  224. .open = ca_midi_input_open,
  225. .close = ca_midi_input_close,
  226. .trigger = ca_midi_input_trigger,
  227. };
  228. static void ca_midi_free(struct snd_ca_midi *midi)
  229. {
  230. midi->interrupt = NULL;
  231. midi->interrupt_enable = NULL;
  232. midi->interrupt_disable = NULL;
  233. midi->read = NULL;
  234. midi->write = NULL;
  235. midi->get_dev_id_card = NULL;
  236. midi->get_dev_id_port = NULL;
  237. midi->rmidi = NULL;
  238. }
  239. static void ca_rmidi_free(struct snd_rawmidi *rmidi)
  240. {
  241. ca_midi_free(rmidi->private_data);
  242. }
  243. int __devinit ca_midi_init(void *dev_id, struct snd_ca_midi *midi, int device, char *name)
  244. {
  245. struct snd_rawmidi *rmidi;
  246. int err;
  247. if ((err = snd_rawmidi_new(midi->get_dev_id_card(midi->dev_id), name, device, 1, 1, &rmidi)) < 0)
  248. return err;
  249. midi->dev_id = dev_id;
  250. midi->interrupt = ca_midi_interrupt;
  251. spin_lock_init(&midi->open_lock);
  252. spin_lock_init(&midi->input_lock);
  253. spin_lock_init(&midi->output_lock);
  254. strcpy(rmidi->name, name);
  255. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &ca_midi_output);
  256. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &ca_midi_input);
  257. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  258. SNDRV_RAWMIDI_INFO_INPUT |
  259. SNDRV_RAWMIDI_INFO_DUPLEX;
  260. rmidi->private_data = midi;
  261. rmidi->private_free = ca_rmidi_free;
  262. midi->rmidi = rmidi;
  263. return 0;
  264. }