emu10k1_patch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. if (snd_BUG_ON(!sp || !hdr))
  46. 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. if (offset + size > blocksize)
  97. return -EINVAL;
  98. snd_emu10k1_synth_bzero(emu, sp->block, offset, size);
  99. offset += size;
  100. /* copy start->loopend */
  101. size = loopend;
  102. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  103. size *= 2;
  104. if (offset + size > blocksize)
  105. return -EINVAL;
  106. if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size)) {
  107. snd_emu10k1_synth_free(emu, sp->block);
  108. sp->block = NULL;
  109. return -EFAULT;
  110. }
  111. offset += size;
  112. data += size;
  113. #if 0 /* not suppported yet */
  114. /* handle reverse (or bidirectional) loop */
  115. if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)) {
  116. /* copy loop in reverse */
  117. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS)) {
  118. int woffset;
  119. unsigned short *wblock = (unsigned short*)block;
  120. woffset = offset / 2;
  121. if (offset + loopsize * 2 > blocksize)
  122. return -EINVAL;
  123. for (i = 0; i < loopsize; i++)
  124. wblock[woffset + i] = wblock[woffset - i -1];
  125. offset += loopsize * 2;
  126. } else {
  127. if (offset + loopsize > blocksize)
  128. return -EINVAL;
  129. for (i = 0; i < loopsize; i++)
  130. block[offset + i] = block[offset - i -1];
  131. offset += loopsize;
  132. }
  133. /* modify loop pointers */
  134. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) {
  135. sp->v.loopend += loopsize;
  136. } else {
  137. sp->v.loopstart += loopsize;
  138. sp->v.loopend += loopsize;
  139. }
  140. /* add sample pointer */
  141. sp->v.end += loopsize;
  142. }
  143. #endif
  144. /* loopend -> sample end */
  145. size = sp->v.size - loopend;
  146. if (size < 0)
  147. return -EINVAL;
  148. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  149. size *= 2;
  150. if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size)) {
  151. snd_emu10k1_synth_free(emu, sp->block);
  152. sp->block = NULL;
  153. return -EFAULT;
  154. }
  155. offset += size;
  156. /* clear rest of samples (if any) */
  157. if (offset < blocksize)
  158. snd_emu10k1_synth_bzero(emu, sp->block, offset, blocksize - offset);
  159. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
  160. /* if no blank loop is attached in the sample, add it */
  161. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) {
  162. sp->v.loopstart = sp->v.end + BLANK_LOOP_START;
  163. sp->v.loopend = sp->v.end + BLANK_LOOP_END;
  164. }
  165. }
  166. #if 0 /* not supported yet */
  167. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_UNSIGNED) {
  168. /* unsigned -> signed */
  169. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS)) {
  170. unsigned short *wblock = (unsigned short*)block;
  171. for (i = 0; i < truesize; i++)
  172. wblock[i] ^= 0x8000;
  173. } else {
  174. for (i = 0; i < truesize; i++)
  175. block[i] ^= 0x80;
  176. }
  177. }
  178. #endif
  179. /* recalculate offset */
  180. start_addr = BLANK_HEAD_SIZE * 2;
  181. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  182. start_addr >>= 1;
  183. sp->v.start += start_addr;
  184. sp->v.end += start_addr;
  185. sp->v.loopstart += start_addr;
  186. sp->v.loopend += start_addr;
  187. return 0;
  188. }
  189. /*
  190. * free a sample block
  191. */
  192. int
  193. snd_emu10k1_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
  194. struct snd_util_memhdr *hdr)
  195. {
  196. struct snd_emu10k1 *emu;
  197. emu = rec->hw;
  198. if (snd_BUG_ON(!sp || !hdr))
  199. return -EINVAL;
  200. if (sp->block) {
  201. snd_emu10k1_synth_free(emu, sp->block);
  202. sp->block = NULL;
  203. }
  204. return 0;
  205. }