emu10k1_patch.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Patch transfer callback for Emu10k1
  3. *
  4. * Copyright (C) 2000 Takashi iwai <tiwai@suse.de>
  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. * All the code for loading in a patch. There is very little that is
  22. * chip specific here. Just the actual writing to the board.
  23. */
  24. #include "emu10k1_synth_local.h"
  25. /*
  26. */
  27. #define BLANK_LOOP_START 4
  28. #define BLANK_LOOP_END 8
  29. #define BLANK_LOOP_SIZE 12
  30. #define BLANK_HEAD_SIZE 32
  31. /*
  32. * allocate a sample block and copy data from userspace
  33. */
  34. int
  35. snd_emu10k1_sample_new(struct snd_emux *rec, struct snd_sf_sample *sp,
  36. struct snd_util_memhdr *hdr,
  37. const void __user *data, long count)
  38. {
  39. int offset;
  40. int truesize, size, loopsize, blocksize;
  41. int loopend, sampleend;
  42. unsigned int start_addr;
  43. struct snd_emu10k1 *emu;
  44. emu = rec->hw;
  45. snd_assert(sp != NULL, return -EINVAL);
  46. snd_assert(hdr != NULL, return -EINVAL);
  47. if (sp->v.size == 0) {
  48. snd_printd("emu: rom font for sample %d\n", sp->v.sample);
  49. return 0;
  50. }
  51. /* recalculate address offset */
  52. sp->v.end -= sp->v.start;
  53. sp->v.loopstart -= sp->v.start;
  54. sp->v.loopend -= sp->v.start;
  55. sp->v.start = 0;
  56. /* some samples have invalid data. the addresses are corrected in voice info */
  57. sampleend = sp->v.end;
  58. if (sampleend > sp->v.size)
  59. sampleend = sp->v.size;
  60. loopend = sp->v.loopend;
  61. if (loopend > sampleend)
  62. loopend = sampleend;
  63. /* be sure loop points start < end */
  64. if (sp->v.loopstart >= sp->v.loopend) {
  65. int tmp = sp->v.loopstart;
  66. sp->v.loopstart = sp->v.loopend;
  67. sp->v.loopend = tmp;
  68. }
  69. /* compute true data size to be loaded */
  70. truesize = sp->v.size + BLANK_HEAD_SIZE;
  71. loopsize = 0;
  72. #if 0 /* not supported */
  73. if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP))
  74. loopsize = sp->v.loopend - sp->v.loopstart;
  75. truesize += loopsize;
  76. #endif
  77. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK)
  78. truesize += BLANK_LOOP_SIZE;
  79. /* try to allocate a memory block */
  80. blocksize = truesize;
  81. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  82. blocksize *= 2;
  83. sp->block = snd_emu10k1_synth_alloc(emu, blocksize);
  84. if (sp->block == NULL) {
  85. snd_printd("emu10k1: synth malloc failed (size=%d)\n", blocksize);
  86. /* not ENOMEM (for compatibility with OSS) */
  87. return -ENOSPC;
  88. }
  89. /* set the total size */
  90. sp->v.truesize = blocksize;
  91. /* write blank samples at head */
  92. offset = 0;
  93. size = BLANK_HEAD_SIZE;
  94. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  95. size *= 2;
  96. snd_assert(offset + size <= blocksize, return -EINVAL);
  97. snd_emu10k1_synth_bzero(emu, sp->block, offset, size);
  98. offset += size;
  99. /* copy start->loopend */
  100. size = loopend;
  101. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  102. size *= 2;
  103. snd_assert(offset + size <= blocksize, return -EINVAL);
  104. if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size)) {
  105. snd_emu10k1_synth_free(emu, sp->block);
  106. sp->block = NULL;
  107. return -EFAULT;
  108. }
  109. offset += size;
  110. data += size;
  111. #if 0 /* not suppported yet */
  112. /* handle reverse (or bidirectional) loop */
  113. if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)) {
  114. /* copy loop in reverse */
  115. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS)) {
  116. int woffset;
  117. unsigned short *wblock = (unsigned short*)block;
  118. woffset = offset / 2;
  119. snd_assert(offset + loopsize*2 <= blocksize, return -EINVAL);
  120. for (i = 0; i < loopsize; i++)
  121. wblock[woffset + i] = wblock[woffset - i -1];
  122. offset += loopsize * 2;
  123. } else {
  124. snd_assert(offset + loopsize <= blocksize, return -EINVAL);
  125. for (i = 0; i < loopsize; i++)
  126. block[offset + i] = block[offset - i -1];
  127. offset += loopsize;
  128. }
  129. /* modify loop pointers */
  130. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) {
  131. sp->v.loopend += loopsize;
  132. } else {
  133. sp->v.loopstart += loopsize;
  134. sp->v.loopend += loopsize;
  135. }
  136. /* add sample pointer */
  137. sp->v.end += loopsize;
  138. }
  139. #endif
  140. /* loopend -> sample end */
  141. size = sp->v.size - loopend;
  142. snd_assert(size >= 0, return -EINVAL);
  143. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  144. size *= 2;
  145. if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size)) {
  146. snd_emu10k1_synth_free(emu, sp->block);
  147. sp->block = NULL;
  148. return -EFAULT;
  149. }
  150. offset += size;
  151. /* clear rest of samples (if any) */
  152. if (offset < blocksize)
  153. snd_emu10k1_synth_bzero(emu, sp->block, offset, blocksize - offset);
  154. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
  155. /* if no blank loop is attached in the sample, add it */
  156. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) {
  157. sp->v.loopstart = sp->v.end + BLANK_LOOP_START;
  158. sp->v.loopend = sp->v.end + BLANK_LOOP_END;
  159. }
  160. }
  161. #if 0 /* not supported yet */
  162. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_UNSIGNED) {
  163. /* unsigned -> signed */
  164. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS)) {
  165. unsigned short *wblock = (unsigned short*)block;
  166. for (i = 0; i < truesize; i++)
  167. wblock[i] ^= 0x8000;
  168. } else {
  169. for (i = 0; i < truesize; i++)
  170. block[i] ^= 0x80;
  171. }
  172. }
  173. #endif
  174. /* recalculate offset */
  175. start_addr = BLANK_HEAD_SIZE * 2;
  176. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  177. start_addr >>= 1;
  178. sp->v.start += start_addr;
  179. sp->v.end += start_addr;
  180. sp->v.loopstart += start_addr;
  181. sp->v.loopend += start_addr;
  182. return 0;
  183. }
  184. /*
  185. * free a sample block
  186. */
  187. int
  188. snd_emu10k1_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
  189. struct snd_util_memhdr *hdr)
  190. {
  191. struct snd_emu10k1 *emu;
  192. emu = rec->hw;
  193. snd_assert(sp != NULL, return -EINVAL);
  194. snd_assert(hdr != NULL, return -EINVAL);
  195. if (sp->block) {
  196. snd_emu10k1_synth_free(emu, sp->block);
  197. sp->block = NULL;
  198. }
  199. return 0;
  200. }