emu8000_patch.c 7.4 KB

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