ca_midi.c 8.4 KB

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