emumpu401.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Routines for control of EMU10K1 MPU-401 in UART mode
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/time.h>
  23. #include <linux/init.h>
  24. #include <sound/core.h>
  25. #include <sound/emu10k1.h>
  26. #define EMU10K1_MIDI_MODE_INPUT (1<<0)
  27. #define EMU10K1_MIDI_MODE_OUTPUT (1<<1)
  28. static inline unsigned char mpu401_read(emu10k1_t *emu, emu10k1_midi_t *mpu, int idx)
  29. {
  30. if (emu->audigy)
  31. return (unsigned char)snd_emu10k1_ptr_read(emu, mpu->port + idx, 0);
  32. else
  33. return inb(emu->port + mpu->port + idx);
  34. }
  35. static inline void mpu401_write(emu10k1_t *emu, emu10k1_midi_t *mpu, int data, int idx)
  36. {
  37. if (emu->audigy)
  38. snd_emu10k1_ptr_write(emu, mpu->port + idx, 0, data);
  39. else
  40. outb(data, emu->port + mpu->port + idx);
  41. }
  42. #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
  43. #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
  44. #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
  45. #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
  46. #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
  47. #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
  48. #define MPU401_RESET 0xff
  49. #define MPU401_ENTER_UART 0x3f
  50. #define MPU401_ACK 0xfe
  51. static void mpu401_clear_rx(emu10k1_t *emu, emu10k1_midi_t *mpu)
  52. {
  53. int timeout = 100000;
  54. for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
  55. mpu401_read_data(emu, mpu);
  56. #ifdef CONFIG_SND_DEBUG
  57. if (timeout <= 0)
  58. snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
  59. #endif
  60. }
  61. /*
  62. */
  63. static void do_emu10k1_midi_interrupt(emu10k1_t *emu, emu10k1_midi_t *midi, unsigned int status)
  64. {
  65. unsigned char byte;
  66. if (midi->rmidi == NULL) {
  67. snd_emu10k1_intr_disable(emu, midi->tx_enable | midi->rx_enable);
  68. return;
  69. }
  70. spin_lock(&midi->input_lock);
  71. if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
  72. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
  73. mpu401_clear_rx(emu, midi);
  74. } else {
  75. byte = mpu401_read_data(emu, midi);
  76. if (midi->substream_input)
  77. snd_rawmidi_receive(midi->substream_input, &byte, 1);
  78. }
  79. }
  80. spin_unlock(&midi->input_lock);
  81. spin_lock(&midi->output_lock);
  82. if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
  83. if (midi->substream_output &&
  84. snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
  85. mpu401_write_data(emu, midi, byte);
  86. } else {
  87. snd_emu10k1_intr_disable(emu, midi->tx_enable);
  88. }
  89. }
  90. spin_unlock(&midi->output_lock);
  91. }
  92. static void snd_emu10k1_midi_interrupt(emu10k1_t *emu, unsigned int status)
  93. {
  94. do_emu10k1_midi_interrupt(emu, &emu->midi, status);
  95. }
  96. static void snd_emu10k1_midi_interrupt2(emu10k1_t *emu, unsigned int status)
  97. {
  98. do_emu10k1_midi_interrupt(emu, &emu->midi2, status);
  99. }
  100. static void snd_emu10k1_midi_cmd(emu10k1_t * emu, emu10k1_midi_t *midi, unsigned char cmd, int ack)
  101. {
  102. unsigned long flags;
  103. int timeout, ok;
  104. spin_lock_irqsave(&midi->input_lock, flags);
  105. mpu401_write_data(emu, midi, 0x00);
  106. /* mpu401_clear_rx(emu, midi); */
  107. mpu401_write_cmd(emu, midi, cmd);
  108. if (ack) {
  109. ok = 0;
  110. timeout = 10000;
  111. while (!ok && timeout-- > 0) {
  112. if (mpu401_input_avail(emu, midi)) {
  113. if (mpu401_read_data(emu, midi) == MPU401_ACK)
  114. ok = 1;
  115. }
  116. }
  117. if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
  118. ok = 1;
  119. } else {
  120. ok = 1;
  121. }
  122. spin_unlock_irqrestore(&midi->input_lock, flags);
  123. if (!ok)
  124. snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
  125. cmd, emu->port,
  126. mpu401_read_stat(emu, midi),
  127. mpu401_read_data(emu, midi));
  128. }
  129. static int snd_emu10k1_midi_input_open(snd_rawmidi_substream_t * substream)
  130. {
  131. emu10k1_t *emu;
  132. emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
  133. unsigned long flags;
  134. emu = midi->emu;
  135. snd_assert(emu, return -ENXIO);
  136. spin_lock_irqsave(&midi->open_lock, flags);
  137. midi->midi_mode |= EMU10K1_MIDI_MODE_INPUT;
  138. midi->substream_input = substream;
  139. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
  140. spin_unlock_irqrestore(&midi->open_lock, flags);
  141. snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1);
  142. snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
  143. } else {
  144. spin_unlock_irqrestore(&midi->open_lock, flags);
  145. }
  146. return 0;
  147. }
  148. static int snd_emu10k1_midi_output_open(snd_rawmidi_substream_t * substream)
  149. {
  150. emu10k1_t *emu;
  151. emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
  152. unsigned long flags;
  153. emu = midi->emu;
  154. snd_assert(emu, return -ENXIO);
  155. spin_lock_irqsave(&midi->open_lock, flags);
  156. midi->midi_mode |= EMU10K1_MIDI_MODE_OUTPUT;
  157. midi->substream_output = substream;
  158. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
  159. spin_unlock_irqrestore(&midi->open_lock, flags);
  160. snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1);
  161. snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
  162. } else {
  163. spin_unlock_irqrestore(&midi->open_lock, flags);
  164. }
  165. return 0;
  166. }
  167. static int snd_emu10k1_midi_input_close(snd_rawmidi_substream_t * substream)
  168. {
  169. emu10k1_t *emu;
  170. emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
  171. unsigned long flags;
  172. emu = midi->emu;
  173. snd_assert(emu, return -ENXIO);
  174. spin_lock_irqsave(&midi->open_lock, flags);
  175. snd_emu10k1_intr_disable(emu, midi->rx_enable);
  176. midi->midi_mode &= ~EMU10K1_MIDI_MODE_INPUT;
  177. midi->substream_input = NULL;
  178. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
  179. spin_unlock_irqrestore(&midi->open_lock, flags);
  180. snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
  181. } else {
  182. spin_unlock_irqrestore(&midi->open_lock, flags);
  183. }
  184. return 0;
  185. }
  186. static int snd_emu10k1_midi_output_close(snd_rawmidi_substream_t * substream)
  187. {
  188. emu10k1_t *emu;
  189. emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
  190. unsigned long flags;
  191. emu = midi->emu;
  192. snd_assert(emu, return -ENXIO);
  193. spin_lock_irqsave(&midi->open_lock, flags);
  194. snd_emu10k1_intr_disable(emu, midi->tx_enable);
  195. midi->midi_mode &= ~EMU10K1_MIDI_MODE_OUTPUT;
  196. midi->substream_output = NULL;
  197. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
  198. spin_unlock_irqrestore(&midi->open_lock, flags);
  199. snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
  200. } else {
  201. spin_unlock_irqrestore(&midi->open_lock, flags);
  202. }
  203. return 0;
  204. }
  205. static void snd_emu10k1_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
  206. {
  207. emu10k1_t *emu;
  208. emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
  209. emu = midi->emu;
  210. snd_assert(emu, return);
  211. if (up)
  212. snd_emu10k1_intr_enable(emu, midi->rx_enable);
  213. else
  214. snd_emu10k1_intr_disable(emu, midi->rx_enable);
  215. }
  216. static void snd_emu10k1_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
  217. {
  218. emu10k1_t *emu;
  219. emu10k1_midi_t *midi = (emu10k1_midi_t *)substream->rmidi->private_data;
  220. unsigned long flags;
  221. emu = midi->emu;
  222. snd_assert(emu, return);
  223. if (up) {
  224. int max = 4;
  225. unsigned char byte;
  226. /* try to send some amount of bytes here before interrupts */
  227. spin_lock_irqsave(&midi->output_lock, flags);
  228. while (max > 0) {
  229. if (mpu401_output_ready(emu, midi)) {
  230. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT) ||
  231. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  232. /* no more data */
  233. spin_unlock_irqrestore(&midi->output_lock, flags);
  234. return;
  235. }
  236. mpu401_write_data(emu, midi, byte);
  237. max--;
  238. } else {
  239. break;
  240. }
  241. }
  242. spin_unlock_irqrestore(&midi->output_lock, flags);
  243. snd_emu10k1_intr_enable(emu, midi->tx_enable);
  244. } else {
  245. snd_emu10k1_intr_disable(emu, midi->tx_enable);
  246. }
  247. }
  248. /*
  249. */
  250. static snd_rawmidi_ops_t snd_emu10k1_midi_output =
  251. {
  252. .open = snd_emu10k1_midi_output_open,
  253. .close = snd_emu10k1_midi_output_close,
  254. .trigger = snd_emu10k1_midi_output_trigger,
  255. };
  256. static snd_rawmidi_ops_t snd_emu10k1_midi_input =
  257. {
  258. .open = snd_emu10k1_midi_input_open,
  259. .close = snd_emu10k1_midi_input_close,
  260. .trigger = snd_emu10k1_midi_input_trigger,
  261. };
  262. static void snd_emu10k1_midi_free(snd_rawmidi_t *rmidi)
  263. {
  264. emu10k1_midi_t *midi = (emu10k1_midi_t *)rmidi->private_data;
  265. midi->interrupt = NULL;
  266. midi->rmidi = NULL;
  267. }
  268. static int __devinit emu10k1_midi_init(emu10k1_t *emu, emu10k1_midi_t *midi, int device, char *name)
  269. {
  270. snd_rawmidi_t *rmidi;
  271. int err;
  272. if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
  273. return err;
  274. midi->emu = emu;
  275. spin_lock_init(&midi->open_lock);
  276. spin_lock_init(&midi->input_lock);
  277. spin_lock_init(&midi->output_lock);
  278. strcpy(rmidi->name, name);
  279. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1_midi_output);
  280. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1_midi_input);
  281. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  282. SNDRV_RAWMIDI_INFO_INPUT |
  283. SNDRV_RAWMIDI_INFO_DUPLEX;
  284. rmidi->private_data = midi;
  285. rmidi->private_free = snd_emu10k1_midi_free;
  286. midi->rmidi = rmidi;
  287. return 0;
  288. }
  289. int __devinit snd_emu10k1_midi(emu10k1_t *emu)
  290. {
  291. emu10k1_midi_t *midi = &emu->midi;
  292. int err;
  293. if ((err = emu10k1_midi_init(emu, midi, 0, "EMU10K1 MPU-401 (UART)")) < 0)
  294. return err;
  295. midi->tx_enable = INTE_MIDITXENABLE;
  296. midi->rx_enable = INTE_MIDIRXENABLE;
  297. midi->port = MUDATA;
  298. midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
  299. midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
  300. midi->interrupt = snd_emu10k1_midi_interrupt;
  301. return 0;
  302. }
  303. int __devinit snd_emu10k1_audigy_midi(emu10k1_t *emu)
  304. {
  305. emu10k1_midi_t *midi;
  306. int err;
  307. midi = &emu->midi;
  308. if ((err = emu10k1_midi_init(emu, midi, 0, "Audigy MPU-401 (UART)")) < 0)
  309. return err;
  310. midi->tx_enable = INTE_MIDITXENABLE;
  311. midi->rx_enable = INTE_MIDIRXENABLE;
  312. midi->port = A_MUDATA1;
  313. midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
  314. midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
  315. midi->interrupt = snd_emu10k1_midi_interrupt;
  316. midi = &emu->midi2;
  317. if ((err = emu10k1_midi_init(emu, midi, 1, "Audigy MPU-401 #2")) < 0)
  318. return err;
  319. midi->tx_enable = INTE_A_MIDITXENABLE2;
  320. midi->rx_enable = INTE_A_MIDIRXENABLE2;
  321. midi->port = A_MUDATA2;
  322. midi->ipr_tx = IPR_A_MIDITRANSBUFEMPTY2;
  323. midi->ipr_rx = IPR_A_MIDIRECVBUFEMPTY2;
  324. midi->interrupt = snd_emu10k1_midi_interrupt2;
  325. return 0;
  326. }