emu10k1_patch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. dev_dbg(emu->card->dev,
  49. "emu: rom font for sample %d\n", sp->v.sample);
  50. return 0;
  51. }
  52. /* recalculate address offset */
  53. sp->v.end -= sp->v.start;
  54. sp->v.loopstart -= sp->v.start;
  55. sp->v.loopend -= sp->v.start;
  56. sp->v.start = 0;
  57. /* some samples have invalid data. the addresses are corrected in voice info */
  58. sampleend = sp->v.end;
  59. if (sampleend > sp->v.size)
  60. sampleend = sp->v.size;
  61. loopend = sp->v.loopend;
  62. if (loopend > sampleend)
  63. loopend = sampleend;
  64. /* be sure loop points start < end */
  65. if (sp->v.loopstart >= sp->v.loopend)
  66. swap(sp->v.loopstart, sp->v.loopend);
  67. /* compute true data size to be loaded */
  68. truesize = sp->v.size + BLANK_HEAD_SIZE;
  69. loopsize = 0;
  70. #if 0 /* not supported */
  71. if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP))
  72. loopsize = sp->v.loopend - sp->v.loopstart;
  73. truesize += loopsize;
  74. #endif
  75. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK)
  76. truesize += BLANK_LOOP_SIZE;
  77. /* try to allocate a memory block */
  78. blocksize = truesize;
  79. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  80. blocksize *= 2;
  81. sp->block = snd_emu10k1_synth_alloc(emu, blocksize);
  82. if (sp->block == NULL) {
  83. dev_dbg(emu->card->dev,
  84. "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. if (offset + size > blocksize)
  96. 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. if (offset + size > blocksize)
  104. return -EINVAL;
  105. if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size)) {
  106. snd_emu10k1_synth_free(emu, sp->block);
  107. sp->block = NULL;
  108. return -EFAULT;
  109. }
  110. offset += size;
  111. data += size;
  112. #if 0 /* not supported yet */
  113. /* handle reverse (or bidirectional) loop */
  114. if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)) {
  115. /* copy loop in reverse */
  116. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS)) {
  117. int woffset;
  118. unsigned short *wblock = (unsigned short*)block;
  119. woffset = offset / 2;
  120. if (offset + loopsize * 2 > blocksize)
  121. return -EINVAL;
  122. for (i = 0; i < loopsize; i++)
  123. wblock[woffset + i] = wblock[woffset - i -1];
  124. offset += loopsize * 2;
  125. } else {
  126. if (offset + loopsize > blocksize)
  127. return -EINVAL;
  128. for (i = 0; i < loopsize; i++)
  129. block[offset + i] = block[offset - i -1];
  130. offset += loopsize;
  131. }
  132. /* modify loop pointers */
  133. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) {
  134. sp->v.loopend += loopsize;
  135. } else {
  136. sp->v.loopstart += loopsize;
  137. sp->v.loopend += loopsize;
  138. }
  139. /* add sample pointer */
  140. sp->v.end += loopsize;
  141. }
  142. #endif
  143. /* loopend -> sample end */
  144. size = sp->v.size - loopend;
  145. if (size < 0)
  146. return -EINVAL;
  147. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  148. size *= 2;
  149. if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size)) {
  150. snd_emu10k1_synth_free(emu, sp->block);
  151. sp->block = NULL;
  152. return -EFAULT;
  153. }
  154. offset += size;
  155. /* clear rest of samples (if any) */
  156. if (offset < blocksize)
  157. snd_emu10k1_synth_bzero(emu, sp->block, offset, blocksize - offset);
  158. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
  159. /* if no blank loop is attached in the sample, add it */
  160. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) {
  161. sp->v.loopstart = sp->v.end + BLANK_LOOP_START;
  162. sp->v.loopend = sp->v.end + BLANK_LOOP_END;
  163. }
  164. }
  165. #if 0 /* not supported yet */
  166. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_UNSIGNED) {
  167. /* unsigned -> signed */
  168. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS)) {
  169. unsigned short *wblock = (unsigned short*)block;
  170. for (i = 0; i < truesize; i++)
  171. wblock[i] ^= 0x8000;
  172. } else {
  173. for (i = 0; i < truesize; i++)
  174. block[i] ^= 0x80;
  175. }
  176. }
  177. #endif
  178. /* recalculate offset */
  179. start_addr = BLANK_HEAD_SIZE * 2;
  180. if (! (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS))
  181. start_addr >>= 1;
  182. sp->v.start += start_addr;
  183. sp->v.end += start_addr;
  184. sp->v.loopstart += start_addr;
  185. sp->v.loopend += start_addr;
  186. return 0;
  187. }
  188. /*
  189. * free a sample block
  190. */
  191. int
  192. snd_emu10k1_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
  193. struct snd_util_memhdr *hdr)
  194. {
  195. struct snd_emu10k1 *emu;
  196. emu = rec->hw;
  197. if (snd_BUG_ON(!sp || !hdr))
  198. return -EINVAL;
  199. if (sp->block) {
  200. snd_emu10k1_synth_free(emu, sp->block);
  201. sp->block = NULL;
  202. }
  203. return 0;
  204. }