emumpu401.c 11 KB

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