voice.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Creative Labs, Inc.
  4. * Lee Revell <rlrevell@joe-job.com>
  5. * Routines for control of EMU10K1 chips - voice manager
  6. *
  7. * Rewrote voice allocator for multichannel support - rlrevell 12/2004
  8. *
  9. * BUGS:
  10. * --
  11. *
  12. * TODO:
  13. * --
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <linux/time.h>
  31. #include <linux/export.h>
  32. #include <sound/core.h>
  33. #include <sound/emu10k1.h>
  34. /* Previously the voice allocator started at 0 every time. The new voice
  35. * allocator uses a round robin scheme. The next free voice is tracked in
  36. * the card record and each allocation begins where the last left off. The
  37. * hardware requires stereo interleaved voices be aligned to an even/odd
  38. * boundary. For multichannel voice allocation we ensure than the block of
  39. * voices does not cross the 32 voice boundary. This simplifies the
  40. * multichannel support and ensures we can use a single write to the
  41. * (set|clear)_loop_stop registers. Otherwise (for example) the voices would
  42. * get out of sync when pausing/resuming a stream.
  43. * --rlrevell
  44. */
  45. static int voice_alloc(struct snd_emu10k1 *emu, int type, int number,
  46. struct snd_emu10k1_voice **rvoice)
  47. {
  48. struct snd_emu10k1_voice *voice;
  49. int i, j, k, first_voice, last_voice, skip;
  50. *rvoice = NULL;
  51. first_voice = last_voice = 0;
  52. for (i = emu->next_free_voice, j = 0; j < NUM_G ; i += number, j += number) {
  53. /*
  54. dev_dbg(emu->card->dev, "i %d j %d next free %d!\n",
  55. i, j, emu->next_free_voice);
  56. */
  57. i %= NUM_G;
  58. /* stereo voices must be even/odd */
  59. if ((number == 2) && (i % 2)) {
  60. i++;
  61. continue;
  62. }
  63. skip = 0;
  64. for (k = 0; k < number; k++) {
  65. voice = &emu->voices[(i+k) % NUM_G];
  66. if (voice->use) {
  67. skip = 1;
  68. break;
  69. }
  70. }
  71. if (!skip) {
  72. /* dev_dbg(emu->card->dev, "allocated voice %d\n", i); */
  73. first_voice = i;
  74. last_voice = (i + number) % NUM_G;
  75. emu->next_free_voice = last_voice;
  76. break;
  77. }
  78. }
  79. if (first_voice == last_voice)
  80. return -ENOMEM;
  81. for (i = 0; i < number; i++) {
  82. voice = &emu->voices[(first_voice + i) % NUM_G];
  83. /*
  84. dev_dbg(emu->card->dev, "voice alloc - %i, %i of %i\n",
  85. voice->number, idx-first_voice+1, number);
  86. */
  87. voice->use = 1;
  88. switch (type) {
  89. case EMU10K1_PCM:
  90. voice->pcm = 1;
  91. break;
  92. case EMU10K1_SYNTH:
  93. voice->synth = 1;
  94. break;
  95. case EMU10K1_MIDI:
  96. voice->midi = 1;
  97. break;
  98. case EMU10K1_EFX:
  99. voice->efx = 1;
  100. break;
  101. }
  102. }
  103. *rvoice = &emu->voices[first_voice];
  104. return 0;
  105. }
  106. int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int number,
  107. struct snd_emu10k1_voice **rvoice)
  108. {
  109. unsigned long flags;
  110. int result;
  111. if (snd_BUG_ON(!rvoice))
  112. return -EINVAL;
  113. if (snd_BUG_ON(!number))
  114. return -EINVAL;
  115. spin_lock_irqsave(&emu->voice_lock, flags);
  116. for (;;) {
  117. result = voice_alloc(emu, type, number, rvoice);
  118. if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
  119. break;
  120. /* free a voice from synth */
  121. if (emu->get_synth_voice) {
  122. result = emu->get_synth_voice(emu);
  123. if (result >= 0) {
  124. struct snd_emu10k1_voice *pvoice = &emu->voices[result];
  125. pvoice->interrupt = NULL;
  126. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  127. pvoice->epcm = NULL;
  128. }
  129. }
  130. if (result < 0)
  131. break;
  132. }
  133. spin_unlock_irqrestore(&emu->voice_lock, flags);
  134. return result;
  135. }
  136. EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
  137. int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
  138. struct snd_emu10k1_voice *pvoice)
  139. {
  140. unsigned long flags;
  141. if (snd_BUG_ON(!pvoice))
  142. return -EINVAL;
  143. spin_lock_irqsave(&emu->voice_lock, flags);
  144. pvoice->interrupt = NULL;
  145. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  146. pvoice->epcm = NULL;
  147. snd_emu10k1_voice_init(emu, pvoice->number);
  148. spin_unlock_irqrestore(&emu->voice_lock, flags);
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(snd_emu10k1_voice_free);