emu8000_patch.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Patch routines for the emu8000 (AWE32/64)
  3. *
  4. * Copyright (C) 1999 Steve Ratcliffe
  5. * Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "emu8000_local.h"
  22. #include <linux/uaccess.h>
  23. #include <linux/moduleparam.h>
  24. static int emu8000_reset_addr;
  25. module_param(emu8000_reset_addr, int, 0444);
  26. MODULE_PARM_DESC(emu8000_reset_addr, "reset write address at each time (makes slowdown)");
  27. /*
  28. * Open up channels.
  29. */
  30. static int
  31. snd_emu8000_open_dma(struct snd_emu8000 *emu, int write)
  32. {
  33. int i;
  34. /* reserve all 30 voices for loading */
  35. for (i = 0; i < EMU8000_DRAM_VOICES; i++) {
  36. snd_emux_lock_voice(emu->emu, i);
  37. snd_emu8000_dma_chan(emu, i, write);
  38. }
  39. /* assign voice 31 and 32 to ROM */
  40. EMU8000_VTFT_WRITE(emu, 30, 0);
  41. EMU8000_PSST_WRITE(emu, 30, 0x1d8);
  42. EMU8000_CSL_WRITE(emu, 30, 0x1e0);
  43. EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
  44. EMU8000_VTFT_WRITE(emu, 31, 0);
  45. EMU8000_PSST_WRITE(emu, 31, 0x1d8);
  46. EMU8000_CSL_WRITE(emu, 31, 0x1e0);
  47. EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
  48. return 0;
  49. }
  50. /*
  51. * Close all dram channels.
  52. */
  53. static void
  54. snd_emu8000_close_dma(struct snd_emu8000 *emu)
  55. {
  56. int i;
  57. for (i = 0; i < EMU8000_DRAM_VOICES; i++) {
  58. snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE);
  59. snd_emux_unlock_voice(emu->emu, i);
  60. }
  61. }
  62. /*
  63. */
  64. #define BLANK_LOOP_START 4
  65. #define BLANK_LOOP_END 8
  66. #define BLANK_LOOP_SIZE 12
  67. #define BLANK_HEAD_SIZE 48
  68. /*
  69. * Read a word from userland, taking care of conversions from
  70. * 8bit samples etc.
  71. */
  72. static unsigned short
  73. read_word(const void __user *buf, int offset, int mode)
  74. {
  75. unsigned short c;
  76. if (mode & SNDRV_SFNT_SAMPLE_8BITS) {
  77. unsigned char cc;
  78. get_user(cc, (unsigned char __user *)buf + offset);
  79. c = cc << 8; /* convert 8bit -> 16bit */
  80. } else {
  81. #ifdef SNDRV_LITTLE_ENDIAN
  82. get_user(c, (unsigned short __user *)buf + offset);
  83. #else
  84. unsigned short cc;
  85. get_user(cc, (unsigned short __user *)buf + offset);
  86. c = swab16(cc);
  87. #endif
  88. }
  89. if (mode & SNDRV_SFNT_SAMPLE_UNSIGNED)
  90. c ^= 0x8000; /* unsigned -> signed */
  91. return c;
  92. }
  93. /*
  94. */
  95. static void
  96. snd_emu8000_write_wait(struct snd_emu8000 *emu)
  97. {
  98. while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
  99. schedule_timeout_interruptible(1);
  100. if (signal_pending(current))
  101. break;
  102. }
  103. }
  104. /*
  105. * write sample word data
  106. *
  107. * You should not have to keep resetting the address each time
  108. * as the chip is supposed to step on the next address automatically.
  109. * It mostly does, but during writes of some samples at random it
  110. * completely loses words (every one in 16 roughly but with no
  111. * obvious pattern).
  112. *
  113. * This is therefore much slower than need be, but is at least
  114. * working.
  115. */
  116. static inline void
  117. write_word(struct snd_emu8000 *emu, int *offset, unsigned short data)
  118. {
  119. if (emu8000_reset_addr) {
  120. if (emu8000_reset_addr > 1)
  121. snd_emu8000_write_wait(emu);
  122. EMU8000_SMALW_WRITE(emu, *offset);
  123. }
  124. EMU8000_SMLD_WRITE(emu, data);
  125. *offset += 1;
  126. }
  127. /*
  128. * Write the sample to EMU800 memory. This routine is invoked out of
  129. * the generic soundfont routines as a callback.
  130. */
  131. int
  132. snd_emu8000_sample_new(struct snd_emux *rec, struct snd_sf_sample *sp,
  133. struct snd_util_memhdr *hdr,
  134. const void __user *data, long count)
  135. {
  136. int i;
  137. int rc;
  138. int offset;
  139. int truesize;
  140. int dram_offset, dram_start;
  141. struct snd_emu8000 *emu;
  142. emu = rec->hw;
  143. if (snd_BUG_ON(!sp))
  144. return -EINVAL;
  145. if (sp->v.size == 0)
  146. return 0;
  147. /* be sure loop points start < end */
  148. if (sp->v.loopstart > sp->v.loopend) {
  149. int tmp = sp->v.loopstart;
  150. sp->v.loopstart = sp->v.loopend;
  151. sp->v.loopend = tmp;
  152. }
  153. /* compute true data size to be loaded */
  154. truesize = sp->v.size;
  155. if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP))
  156. truesize += sp->v.loopend - sp->v.loopstart;
  157. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK)
  158. truesize += BLANK_LOOP_SIZE;
  159. sp->block = snd_util_mem_alloc(hdr, truesize * 2);
  160. if (sp->block == NULL) {
  161. /*snd_printd("EMU8000: out of memory\n");*/
  162. /* not ENOMEM (for compatibility) */
  163. return -ENOSPC;
  164. }
  165. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS) {
  166. if (!access_ok(VERIFY_READ, data, sp->v.size))
  167. return -EFAULT;
  168. } else {
  169. if (!access_ok(VERIFY_READ, data, sp->v.size * 2))
  170. return -EFAULT;
  171. }
  172. /* recalculate address offset */
  173. sp->v.end -= sp->v.start;
  174. sp->v.loopstart -= sp->v.start;
  175. sp->v.loopend -= sp->v.start;
  176. sp->v.start = 0;
  177. /* dram position (in word) -- mem_offset is byte */
  178. dram_offset = EMU8000_DRAM_OFFSET + (sp->block->offset >> 1);
  179. dram_start = dram_offset;
  180. /* set the total size (store onto obsolete checksum value) */
  181. sp->v.truesize = truesize * 2; /* in bytes */
  182. snd_emux_terminate_all(emu->emu);
  183. if ((rc = snd_emu8000_open_dma(emu, EMU8000_RAM_WRITE)) != 0)
  184. return rc;
  185. /* Set the address to start writing at */
  186. snd_emu8000_write_wait(emu);
  187. EMU8000_SMALW_WRITE(emu, dram_offset);
  188. /*snd_emu8000_init_fm(emu);*/
  189. #if 0
  190. /* first block - write 48 samples for silence */
  191. if (! sp->block->offset) {
  192. for (i = 0; i < BLANK_HEAD_SIZE; i++) {
  193. write_word(emu, &dram_offset, 0);
  194. }
  195. }
  196. #endif
  197. offset = 0;
  198. for (i = 0; i < sp->v.size; i++) {
  199. unsigned short s;
  200. s = read_word(data, offset, sp->v.mode_flags);
  201. offset++;
  202. write_word(emu, &dram_offset, s);
  203. /* we may take too long time in this loop.
  204. * so give controls back to kernel if needed.
  205. */
  206. cond_resched();
  207. if (i == sp->v.loopend &&
  208. (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)))
  209. {
  210. int looplen = sp->v.loopend - sp->v.loopstart;
  211. int k;
  212. /* copy reverse loop */
  213. for (k = 1; k <= looplen; k++) {
  214. s = read_word(data, offset - k, sp->v.mode_flags);
  215. write_word(emu, &dram_offset, s);
  216. }
  217. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) {
  218. sp->v.loopend += looplen;
  219. } else {
  220. sp->v.loopstart += looplen;
  221. sp->v.loopend += looplen;
  222. }
  223. sp->v.end += looplen;
  224. }
  225. }
  226. /* if no blank loop is attached in the sample, add it */
  227. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
  228. for (i = 0; i < BLANK_LOOP_SIZE; i++) {
  229. write_word(emu, &dram_offset, 0);
  230. }
  231. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) {
  232. sp->v.loopstart = sp->v.end + BLANK_LOOP_START;
  233. sp->v.loopend = sp->v.end + BLANK_LOOP_END;
  234. }
  235. }
  236. /* add dram offset */
  237. sp->v.start += dram_start;
  238. sp->v.end += dram_start;
  239. sp->v.loopstart += dram_start;
  240. sp->v.loopend += dram_start;
  241. snd_emu8000_close_dma(emu);
  242. snd_emu8000_init_fm(emu);
  243. return 0;
  244. }
  245. /*
  246. * free a sample block
  247. */
  248. int
  249. snd_emu8000_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
  250. struct snd_util_memhdr *hdr)
  251. {
  252. if (sp->block) {
  253. snd_util_mem_free(hdr, sp->block);
  254. sp->block = NULL;
  255. }
  256. return 0;
  257. }
  258. /*
  259. * sample_reset callback - terminate voices
  260. */
  261. void
  262. snd_emu8000_sample_reset(struct snd_emux *rec)
  263. {
  264. snd_emux_terminate_all(rec);
  265. }