emu10k1_patch.c 6.1 KB

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