ca_midi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. pr_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. pr_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. if (snd_BUG_ON(!midi->dev_id))
  113. 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. if (snd_BUG_ON(!midi->dev_id))
  131. return -ENXIO;
  132. spin_lock_irqsave(&midi->open_lock, flags);
  133. midi->midi_mode |= CA_MIDI_MODE_OUTPUT;
  134. midi->substream_output = substream;
  135. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  136. spin_unlock_irqrestore(&midi->open_lock, flags);
  137. ca_midi_cmd(midi, midi->reset, 1);
  138. ca_midi_cmd(midi, midi->enter_uart, 1);
  139. } else {
  140. spin_unlock_irqrestore(&midi->open_lock, flags);
  141. }
  142. return 0;
  143. }
  144. static int ca_midi_input_close(struct snd_rawmidi_substream *substream)
  145. {
  146. struct snd_ca_midi *midi = substream->rmidi->private_data;
  147. unsigned long flags;
  148. if (snd_BUG_ON(!midi->dev_id))
  149. return -ENXIO;
  150. spin_lock_irqsave(&midi->open_lock, flags);
  151. midi->interrupt_disable(midi,midi->rx_enable);
  152. midi->midi_mode &= ~CA_MIDI_MODE_INPUT;
  153. midi->substream_input = NULL;
  154. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT)) {
  155. spin_unlock_irqrestore(&midi->open_lock, flags);
  156. ca_midi_cmd(midi, midi->reset, 0);
  157. } else {
  158. spin_unlock_irqrestore(&midi->open_lock, flags);
  159. }
  160. return 0;
  161. }
  162. static int ca_midi_output_close(struct snd_rawmidi_substream *substream)
  163. {
  164. struct snd_ca_midi *midi = substream->rmidi->private_data;
  165. unsigned long flags;
  166. if (snd_BUG_ON(!midi->dev_id))
  167. return -ENXIO;
  168. spin_lock_irqsave(&midi->open_lock, flags);
  169. midi->interrupt_disable(midi,midi->tx_enable);
  170. midi->midi_mode &= ~CA_MIDI_MODE_OUTPUT;
  171. midi->substream_output = NULL;
  172. if (!(midi->midi_mode & CA_MIDI_MODE_INPUT)) {
  173. spin_unlock_irqrestore(&midi->open_lock, flags);
  174. ca_midi_cmd(midi, midi->reset, 0);
  175. } else {
  176. spin_unlock_irqrestore(&midi->open_lock, flags);
  177. }
  178. return 0;
  179. }
  180. static void ca_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  181. {
  182. struct snd_ca_midi *midi = substream->rmidi->private_data;
  183. if (snd_BUG_ON(!midi->dev_id))
  184. return;
  185. if (up) {
  186. midi->interrupt_enable(midi,midi->rx_enable);
  187. } else {
  188. midi->interrupt_disable(midi, midi->rx_enable);
  189. }
  190. }
  191. static void ca_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  192. {
  193. struct snd_ca_midi *midi = substream->rmidi->private_data;
  194. unsigned long flags;
  195. if (snd_BUG_ON(!midi->dev_id))
  196. return;
  197. if (up) {
  198. int max = 4;
  199. unsigned char byte;
  200. spin_lock_irqsave(&midi->output_lock, flags);
  201. /* try to send some amount of bytes here before interrupts */
  202. while (max > 0) {
  203. if (ca_midi_output_ready(midi)) {
  204. if (!(midi->midi_mode & CA_MIDI_MODE_OUTPUT) ||
  205. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  206. /* no more data */
  207. spin_unlock_irqrestore(&midi->output_lock, flags);
  208. return;
  209. }
  210. ca_midi_write_data(midi, byte);
  211. max--;
  212. } else {
  213. break;
  214. }
  215. }
  216. spin_unlock_irqrestore(&midi->output_lock, flags);
  217. midi->interrupt_enable(midi,midi->tx_enable);
  218. } else {
  219. midi->interrupt_disable(midi,midi->tx_enable);
  220. }
  221. }
  222. static struct snd_rawmidi_ops ca_midi_output =
  223. {
  224. .open = ca_midi_output_open,
  225. .close = ca_midi_output_close,
  226. .trigger = ca_midi_output_trigger,
  227. };
  228. static struct snd_rawmidi_ops ca_midi_input =
  229. {
  230. .open = ca_midi_input_open,
  231. .close = ca_midi_input_close,
  232. .trigger = ca_midi_input_trigger,
  233. };
  234. static void ca_midi_free(struct snd_ca_midi *midi)
  235. {
  236. midi->interrupt = NULL;
  237. midi->interrupt_enable = NULL;
  238. midi->interrupt_disable = NULL;
  239. midi->read = NULL;
  240. midi->write = NULL;
  241. midi->get_dev_id_card = NULL;
  242. midi->get_dev_id_port = NULL;
  243. midi->rmidi = NULL;
  244. }
  245. static void ca_rmidi_free(struct snd_rawmidi *rmidi)
  246. {
  247. ca_midi_free(rmidi->private_data);
  248. }
  249. int ca_midi_init(void *dev_id, struct snd_ca_midi *midi, int device, char *name)
  250. {
  251. struct snd_rawmidi *rmidi;
  252. int err;
  253. if ((err = snd_rawmidi_new(midi->get_dev_id_card(midi->dev_id), name, device, 1, 1, &rmidi)) < 0)
  254. return err;
  255. midi->dev_id = dev_id;
  256. midi->interrupt = ca_midi_interrupt;
  257. spin_lock_init(&midi->open_lock);
  258. spin_lock_init(&midi->input_lock);
  259. spin_lock_init(&midi->output_lock);
  260. strcpy(rmidi->name, name);
  261. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &ca_midi_output);
  262. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &ca_midi_input);
  263. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  264. SNDRV_RAWMIDI_INFO_INPUT |
  265. SNDRV_RAWMIDI_INFO_DUPLEX;
  266. rmidi->private_data = midi;
  267. rmidi->private_free = ca_rmidi_free;
  268. midi->rmidi = rmidi;
  269. return 0;
  270. }