emumpu401.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/time.h>
  22. #include <linux/init.h>
  23. #include <sound/core.h>
  24. #include <sound/emu10k1.h>
  25. #define EMU10K1_MIDI_MODE_INPUT (1<<0)
  26. #define EMU10K1_MIDI_MODE_OUTPUT (1<<1)
  27. static inline unsigned char mpu401_read(struct snd_emu10k1 *emu,
  28. struct snd_emu10k1_midi *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(struct snd_emu10k1 *emu,
  36. struct snd_emu10k1_midi *mpu, int data, int idx)
  37. {
  38. if (emu->audigy)
  39. snd_emu10k1_ptr_write(emu, mpu->port + idx, 0, data);
  40. else
  41. outb(data, emu->port + mpu->port + idx);
  42. }
  43. #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
  44. #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
  45. #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
  46. #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
  47. #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
  48. #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
  49. #define MPU401_RESET 0xff
  50. #define MPU401_ENTER_UART 0x3f
  51. #define MPU401_ACK 0xfe
  52. static void mpu401_clear_rx(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *mpu)
  53. {
  54. int timeout = 100000;
  55. for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
  56. mpu401_read_data(emu, mpu);
  57. #ifdef CONFIG_SND_DEBUG
  58. if (timeout <= 0)
  59. snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
  60. #endif
  61. }
  62. /*
  63. */
  64. static void do_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, unsigned int status)
  65. {
  66. unsigned char byte;
  67. if (midi->rmidi == NULL) {
  68. snd_emu10k1_intr_disable(emu, midi->tx_enable | midi->rx_enable);
  69. return;
  70. }
  71. spin_lock(&midi->input_lock);
  72. if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
  73. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
  74. mpu401_clear_rx(emu, midi);
  75. } else {
  76. byte = mpu401_read_data(emu, midi);
  77. if (midi->substream_input)
  78. snd_rawmidi_receive(midi->substream_input, &byte, 1);
  79. }
  80. }
  81. spin_unlock(&midi->input_lock);
  82. spin_lock(&midi->output_lock);
  83. if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
  84. if (midi->substream_output &&
  85. snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
  86. mpu401_write_data(emu, midi, byte);
  87. } else {
  88. snd_emu10k1_intr_disable(emu, midi->tx_enable);
  89. }
  90. }
  91. spin_unlock(&midi->output_lock);
  92. }
  93. static void snd_emu10k1_midi_interrupt(struct snd_emu10k1 *emu, unsigned int status)
  94. {
  95. do_emu10k1_midi_interrupt(emu, &emu->midi, status);
  96. }
  97. static void snd_emu10k1_midi_interrupt2(struct snd_emu10k1 *emu, unsigned int status)
  98. {
  99. do_emu10k1_midi_interrupt(emu, &emu->midi2, status);
  100. }
  101. static int snd_emu10k1_midi_cmd(struct snd_emu10k1 * emu, struct snd_emu10k1_midi *midi, unsigned char cmd, int ack)
  102. {
  103. unsigned long flags;
  104. int timeout, ok;
  105. spin_lock_irqsave(&midi->input_lock, flags);
  106. mpu401_write_data(emu, midi, 0x00);
  107. /* mpu401_clear_rx(emu, midi); */
  108. mpu401_write_cmd(emu, midi, cmd);
  109. if (ack) {
  110. ok = 0;
  111. timeout = 10000;
  112. while (!ok && timeout-- > 0) {
  113. if (mpu401_input_avail(emu, midi)) {
  114. if (mpu401_read_data(emu, midi) == MPU401_ACK)
  115. ok = 1;
  116. }
  117. }
  118. if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
  119. ok = 1;
  120. } else {
  121. ok = 1;
  122. }
  123. spin_unlock_irqrestore(&midi->input_lock, flags);
  124. if (!ok) {
  125. snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
  126. cmd, emu->port,
  127. mpu401_read_stat(emu, midi),
  128. mpu401_read_data(emu, midi));
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. static int snd_emu10k1_midi_input_open(struct snd_rawmidi_substream *substream)
  134. {
  135. struct snd_emu10k1 *emu;
  136. struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
  137. unsigned long flags;
  138. emu = midi->emu;
  139. if (snd_BUG_ON(!emu))
  140. return -ENXIO;
  141. spin_lock_irqsave(&midi->open_lock, flags);
  142. midi->midi_mode |= EMU10K1_MIDI_MODE_INPUT;
  143. midi->substream_input = substream;
  144. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
  145. spin_unlock_irqrestore(&midi->open_lock, flags);
  146. if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
  147. goto error_out;
  148. if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
  149. goto error_out;
  150. } else {
  151. spin_unlock_irqrestore(&midi->open_lock, flags);
  152. }
  153. return 0;
  154. error_out:
  155. return -EIO;
  156. }
  157. static int snd_emu10k1_midi_output_open(struct snd_rawmidi_substream *substream)
  158. {
  159. struct snd_emu10k1 *emu;
  160. struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
  161. unsigned long flags;
  162. emu = midi->emu;
  163. if (snd_BUG_ON(!emu))
  164. return -ENXIO;
  165. spin_lock_irqsave(&midi->open_lock, flags);
  166. midi->midi_mode |= EMU10K1_MIDI_MODE_OUTPUT;
  167. midi->substream_output = substream;
  168. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
  169. spin_unlock_irqrestore(&midi->open_lock, flags);
  170. if (snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 1))
  171. goto error_out;
  172. if (snd_emu10k1_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
  173. goto error_out;
  174. } else {
  175. spin_unlock_irqrestore(&midi->open_lock, flags);
  176. }
  177. return 0;
  178. error_out:
  179. return -EIO;
  180. }
  181. static int snd_emu10k1_midi_input_close(struct snd_rawmidi_substream *substream)
  182. {
  183. struct snd_emu10k1 *emu;
  184. struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
  185. unsigned long flags;
  186. int err = 0;
  187. emu = midi->emu;
  188. if (snd_BUG_ON(!emu))
  189. return -ENXIO;
  190. spin_lock_irqsave(&midi->open_lock, flags);
  191. snd_emu10k1_intr_disable(emu, midi->rx_enable);
  192. midi->midi_mode &= ~EMU10K1_MIDI_MODE_INPUT;
  193. midi->substream_input = NULL;
  194. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT)) {
  195. spin_unlock_irqrestore(&midi->open_lock, flags);
  196. err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
  197. } else {
  198. spin_unlock_irqrestore(&midi->open_lock, flags);
  199. }
  200. return err;
  201. }
  202. static int snd_emu10k1_midi_output_close(struct snd_rawmidi_substream *substream)
  203. {
  204. struct snd_emu10k1 *emu;
  205. struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
  206. unsigned long flags;
  207. int err = 0;
  208. emu = midi->emu;
  209. if (snd_BUG_ON(!emu))
  210. return -ENXIO;
  211. spin_lock_irqsave(&midi->open_lock, flags);
  212. snd_emu10k1_intr_disable(emu, midi->tx_enable);
  213. midi->midi_mode &= ~EMU10K1_MIDI_MODE_OUTPUT;
  214. midi->substream_output = NULL;
  215. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_INPUT)) {
  216. spin_unlock_irqrestore(&midi->open_lock, flags);
  217. err = snd_emu10k1_midi_cmd(emu, midi, MPU401_RESET, 0);
  218. } else {
  219. spin_unlock_irqrestore(&midi->open_lock, flags);
  220. }
  221. return err;
  222. }
  223. static void snd_emu10k1_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  224. {
  225. struct snd_emu10k1 *emu;
  226. struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
  227. emu = midi->emu;
  228. if (snd_BUG_ON(!emu))
  229. return;
  230. if (up)
  231. snd_emu10k1_intr_enable(emu, midi->rx_enable);
  232. else
  233. snd_emu10k1_intr_disable(emu, midi->rx_enable);
  234. }
  235. static void snd_emu10k1_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  236. {
  237. struct snd_emu10k1 *emu;
  238. struct snd_emu10k1_midi *midi = (struct snd_emu10k1_midi *)substream->rmidi->private_data;
  239. unsigned long flags;
  240. emu = midi->emu;
  241. if (snd_BUG_ON(!emu))
  242. return;
  243. if (up) {
  244. int max = 4;
  245. unsigned char byte;
  246. /* try to send some amount of bytes here before interrupts */
  247. spin_lock_irqsave(&midi->output_lock, flags);
  248. while (max > 0) {
  249. if (mpu401_output_ready(emu, midi)) {
  250. if (!(midi->midi_mode & EMU10K1_MIDI_MODE_OUTPUT) ||
  251. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  252. /* no more data */
  253. spin_unlock_irqrestore(&midi->output_lock, flags);
  254. return;
  255. }
  256. mpu401_write_data(emu, midi, byte);
  257. max--;
  258. } else {
  259. break;
  260. }
  261. }
  262. spin_unlock_irqrestore(&midi->output_lock, flags);
  263. snd_emu10k1_intr_enable(emu, midi->tx_enable);
  264. } else {
  265. snd_emu10k1_intr_disable(emu, midi->tx_enable);
  266. }
  267. }
  268. /*
  269. */
  270. static struct snd_rawmidi_ops snd_emu10k1_midi_output =
  271. {
  272. .open = snd_emu10k1_midi_output_open,
  273. .close = snd_emu10k1_midi_output_close,
  274. .trigger = snd_emu10k1_midi_output_trigger,
  275. };
  276. static struct snd_rawmidi_ops snd_emu10k1_midi_input =
  277. {
  278. .open = snd_emu10k1_midi_input_open,
  279. .close = snd_emu10k1_midi_input_close,
  280. .trigger = snd_emu10k1_midi_input_trigger,
  281. };
  282. static void snd_emu10k1_midi_free(struct snd_rawmidi *rmidi)
  283. {
  284. struct snd_emu10k1_midi *midi = rmidi->private_data;
  285. midi->interrupt = NULL;
  286. midi->rmidi = NULL;
  287. }
  288. static int __devinit emu10k1_midi_init(struct snd_emu10k1 *emu, struct snd_emu10k1_midi *midi, int device, char *name)
  289. {
  290. struct snd_rawmidi *rmidi;
  291. int err;
  292. if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
  293. return err;
  294. midi->emu = emu;
  295. spin_lock_init(&midi->open_lock);
  296. spin_lock_init(&midi->input_lock);
  297. spin_lock_init(&midi->output_lock);
  298. strcpy(rmidi->name, name);
  299. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1_midi_output);
  300. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1_midi_input);
  301. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  302. SNDRV_RAWMIDI_INFO_INPUT |
  303. SNDRV_RAWMIDI_INFO_DUPLEX;
  304. rmidi->private_data = midi;
  305. rmidi->private_free = snd_emu10k1_midi_free;
  306. midi->rmidi = rmidi;
  307. return 0;
  308. }
  309. int __devinit snd_emu10k1_midi(struct snd_emu10k1 *emu)
  310. {
  311. struct snd_emu10k1_midi *midi = &emu->midi;
  312. int err;
  313. if ((err = emu10k1_midi_init(emu, midi, 0, "EMU10K1 MPU-401 (UART)")) < 0)
  314. return err;
  315. midi->tx_enable = INTE_MIDITXENABLE;
  316. midi->rx_enable = INTE_MIDIRXENABLE;
  317. midi->port = MUDATA;
  318. midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
  319. midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
  320. midi->interrupt = snd_emu10k1_midi_interrupt;
  321. return 0;
  322. }
  323. int __devinit snd_emu10k1_audigy_midi(struct snd_emu10k1 *emu)
  324. {
  325. struct snd_emu10k1_midi *midi;
  326. int err;
  327. midi = &emu->midi;
  328. if ((err = emu10k1_midi_init(emu, midi, 0, "Audigy MPU-401 (UART)")) < 0)
  329. return err;
  330. midi->tx_enable = INTE_MIDITXENABLE;
  331. midi->rx_enable = INTE_MIDIRXENABLE;
  332. midi->port = A_MUDATA1;
  333. midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
  334. midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
  335. midi->interrupt = snd_emu10k1_midi_interrupt;
  336. midi = &emu->midi2;
  337. if ((err = emu10k1_midi_init(emu, midi, 1, "Audigy MPU-401 #2")) < 0)
  338. return err;
  339. midi->tx_enable = INTE_A_MIDITXENABLE2;
  340. midi->rx_enable = INTE_A_MIDIRXENABLE2;
  341. midi->port = A_MUDATA2;
  342. midi->ipr_tx = IPR_A_MIDITRANSBUFEMPTY2;
  343. midi->ipr_rx = IPR_A_MIDIRECVBUFEMPTY2;
  344. midi->interrupt = snd_emu10k1_midi_interrupt2;
  345. return 0;
  346. }