ca_midi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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@suse.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/driver.h>
  30. #include <sound/core.h>
  31. #include <sound/rawmidi.h>
  32. #include "ca_midi.h"
  33. #define ca_midi_write_data(midi, data) midi->write(midi, data, 0)
  34. #define ca_midi_write_cmd(midi, data) midi->write(midi, data, 1)
  35. #define ca_midi_read_data(midi) midi->read(midi, 0)
  36. #define ca_midi_read_stat(midi) midi->read(midi, 1)
  37. #define ca_midi_input_avail(midi) (!(ca_midi_read_stat(midi) & midi->input_avail))
  38. #define ca_midi_output_ready(midi) (!(ca_midi_read_stat(midi) & midi->output_ready))
  39. static void ca_midi_clear_rx(struct snd_ca_midi *midi)
  40. {
  41. int timeout = 100000;
  42. for (; timeout > 0 && ca_midi_input_avail(midi); timeout--)
  43. ca_midi_read_data(midi);
  44. #ifdef CONFIG_SND_DEBUG
  45. if (timeout <= 0)
  46. snd_printk(KERN_ERR "ca_midi_clear_rx: timeout (status = 0x%x)\n",
  47. ca_midi_read_stat(midi));
  48. #endif
  49. }
  50. static void ca_midi_interrupt(struct snd_ca_midi *midi, unsigned int status)
  51. {
  52. unsigned char byte;
  53. if (midi->rmidi == NULL) {
  54. midi->interrupt_disable(midi,midi->tx_enable | midi->rx_enable);
  55. return;
  56. }
  57. spin_lock(&midi->input_lock);
  58. if ((status & midi->ipr_rx) && ca_midi_input_avail(midi)) {
  59. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  60. ca_midi_clear_rx(midi);
  61. } else {
  62. byte = ca_midi_read_data(midi);
  63. if(midi->substream_input)
  64. snd_rawmidi_receive(midi->substream_input, &byte, 1);
  65. }
  66. }
  67. spin_unlock(&midi->input_lock);
  68. spin_lock(&midi->output_lock);
  69. if ((status & midi->ipr_tx) && ca_midi_output_ready(midi)) {
  70. if (midi->substream_output &&
  71. snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
  72. ca_midi_write_data(midi, byte);
  73. } else {
  74. midi->interrupt_disable(midi,midi->tx_enable);
  75. }
  76. }
  77. spin_unlock(&midi->output_lock);
  78. }
  79. static void ca_midi_cmd(struct snd_ca_midi *midi, unsigned char cmd, int ack)
  80. {
  81. unsigned long flags;
  82. int timeout, ok;
  83. spin_lock_irqsave(&midi->input_lock, flags);
  84. ca_midi_write_data(midi, 0x00);
  85. /* ca_midi_clear_rx(midi); */
  86. ca_midi_write_cmd(midi, cmd);
  87. if (ack) {
  88. ok = 0;
  89. timeout = 10000;
  90. while (!ok && timeout-- > 0) {
  91. if (ca_midi_input_avail(midi)) {
  92. if (ca_midi_read_data(midi) == midi->ack)
  93. ok = 1;
  94. }
  95. }
  96. if (!ok && ca_midi_read_data(midi) == midi->ack)
  97. ok = 1;
  98. } else {
  99. ok = 1;
  100. }
  101. spin_unlock_irqrestore(&midi->input_lock, flags);
  102. if (!ok)
  103. snd_printk(KERN_ERR "ca_midi_cmd: 0x%x failed at 0x%x (status = 0x%x, data = 0x%x)!!!\n",
  104. cmd,
  105. midi->get_dev_id_port(midi->dev_id),
  106. ca_midi_read_stat(midi),
  107. ca_midi_read_data(midi));
  108. }
  109. static int ca_midi_input_open(struct snd_rawmidi_substream *substream)
  110. {
  111. struct snd_ca_midi *midi = substream->rmidi->private_data;
  112. unsigned long flags;
  113. snd_assert(midi->dev_id, return -ENXIO);
  114. spin_lock_irqsave(&midi->open_lock, flags);
  115. midi->midi_mode |= CA_MIDI_MODE_INPUT;
  116. midi->substream_input = substream;
  117. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
  118. spin_unlock_irqrestore(&midi->open_lock, flags);
  119. ca_midi_cmd(midi, midi->reset, 1);
  120. ca_midi_cmd(midi, midi->enter_uart, 1);
  121. } else {
  122. spin_unlock_irqrestore(&midi->open_lock, flags);
  123. }
  124. return 0;
  125. }
  126. static int ca_midi_output_open(struct snd_rawmidi_substream *substream)
  127. {
  128. struct snd_ca_midi *midi = substream->rmidi->private_data;
  129. unsigned long flags;
  130. snd_assert(midi->dev_id, return -ENXIO);
  131. spin_lock_irqsave(&midi->open_lock, flags);
  132. midi->midi_mode |= CA_MIDI_MODE_OUTPUT;
  133. midi->substream_output = substream;
  134. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  135. spin_unlock_irqrestore(&midi->open_lock, flags);
  136. ca_midi_cmd(midi, midi->reset, 1);
  137. ca_midi_cmd(midi, midi->enter_uart, 1);
  138. } else {
  139. spin_unlock_irqrestore(&midi->open_lock, flags);
  140. }
  141. return 0;
  142. }
  143. static int ca_midi_input_close(struct snd_rawmidi_substream *substream)
  144. {
  145. struct snd_ca_midi *midi = substream->rmidi->private_data;
  146. unsigned long flags;
  147. snd_assert(midi->dev_id, return -ENXIO);
  148. spin_lock_irqsave(&midi->open_lock, flags);
  149. midi->interrupt_disable(midi,midi->rx_enable);
  150. midi->midi_mode &= ~CA_MIDI_MODE_INPUT;
  151. midi->substream_input = NULL;
  152. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
  153. spin_unlock_irqrestore(&midi->open_lock, flags);
  154. ca_midi_cmd(midi, midi->reset, 0);
  155. } else {
  156. spin_unlock_irqrestore(&midi->open_lock, flags);
  157. }
  158. return 0;
  159. }
  160. static int ca_midi_output_close(struct snd_rawmidi_substream *substream)
  161. {
  162. struct snd_ca_midi *midi = substream->rmidi->private_data;
  163. unsigned long flags;
  164. snd_assert(midi->dev_id, return -ENXIO);
  165. spin_lock_irqsave(&midi->open_lock, flags);
  166. midi->interrupt_disable(midi,midi->tx_enable);
  167. midi->midi_mode &= ~CA_MIDI_MODE_OUTPUT;
  168. midi->substream_output = NULL;
  169. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  170. spin_unlock_irqrestore(&midi->open_lock, flags);
  171. ca_midi_cmd(midi, midi->reset, 0);
  172. } else {
  173. spin_unlock_irqrestore(&midi->open_lock, flags);
  174. }
  175. return 0;
  176. }
  177. static void ca_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  178. {
  179. struct snd_ca_midi *midi = substream->rmidi->private_data;
  180. snd_assert(midi->dev_id, return);
  181. if (up) {
  182. midi->interrupt_enable(midi,midi->rx_enable);
  183. } else {
  184. midi->interrupt_disable(midi, midi->rx_enable);
  185. }
  186. }
  187. static void ca_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  188. {
  189. struct snd_ca_midi *midi = substream->rmidi->private_data;
  190. unsigned long flags;
  191. snd_assert(midi->dev_id, return);
  192. if (up) {
  193. int max = 4;
  194. unsigned char byte;
  195. spin_lock_irqsave(&midi->output_lock, flags);
  196. /* try to send some amount of bytes here before interrupts */
  197. while (max > 0) {
  198. if (ca_midi_output_ready(midi)) {
  199. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT) ||
  200. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  201. /* no more data */
  202. spin_unlock_irqrestore(&midi->output_lock, flags);
  203. return;
  204. }
  205. ca_midi_write_data(midi, byte);
  206. max--;
  207. } else {
  208. break;
  209. }
  210. }
  211. spin_unlock_irqrestore(&midi->output_lock, flags);
  212. midi->interrupt_enable(midi,midi->tx_enable);
  213. } else {
  214. midi->interrupt_disable(midi,midi->tx_enable);
  215. }
  216. }
  217. static struct snd_rawmidi_ops ca_midi_output =
  218. {
  219. .open = ca_midi_output_open,
  220. .close = ca_midi_output_close,
  221. .trigger = ca_midi_output_trigger,
  222. };
  223. static struct snd_rawmidi_ops ca_midi_input =
  224. {
  225. .open = ca_midi_input_open,
  226. .close = ca_midi_input_close,
  227. .trigger = ca_midi_input_trigger,
  228. };
  229. static void ca_midi_free(struct snd_ca_midi *midi)
  230. {
  231. midi->interrupt = NULL;
  232. midi->interrupt_enable = NULL;
  233. midi->interrupt_disable = NULL;
  234. midi->read = NULL;
  235. midi->write = NULL;
  236. midi->get_dev_id_card = NULL;
  237. midi->get_dev_id_port = NULL;
  238. midi->rmidi = NULL;
  239. }
  240. static void ca_rmidi_free(struct snd_rawmidi *rmidi)
  241. {
  242. ca_midi_free(rmidi->private_data);
  243. }
  244. int __devinit ca_midi_init(void *dev_id, struct snd_ca_midi *midi, int device, char *name)
  245. {
  246. struct snd_rawmidi *rmidi;
  247. int err;
  248. if ((err = snd_rawmidi_new(midi->get_dev_id_card(midi->dev_id), name, device, 1, 1, &rmidi)) < 0)
  249. return err;
  250. midi->dev_id = dev_id;
  251. midi->interrupt = ca_midi_interrupt;
  252. spin_lock_init(&midi->open_lock);
  253. spin_lock_init(&midi->input_lock);
  254. spin_lock_init(&midi->output_lock);
  255. strcpy(rmidi->name, name);
  256. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &ca_midi_output);
  257. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &ca_midi_input);
  258. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  259. SNDRV_RAWMIDI_INFO_INPUT |
  260. SNDRV_RAWMIDI_INFO_DUPLEX;
  261. rmidi->private_data = midi;
  262. rmidi->private_free = ca_rmidi_free;
  263. midi->rmidi = rmidi;
  264. return 0;
  265. }