emu8000_patch.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. int tmp = sp->v.loopstart;
  151. sp->v.loopstart = sp->v.loopend;
  152. sp->v.loopend = tmp;
  153. }
  154. /* compute true data size to be loaded */
  155. truesize = sp->v.size;
  156. if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP))
  157. truesize += sp->v.loopend - sp->v.loopstart;
  158. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK)
  159. truesize += BLANK_LOOP_SIZE;
  160. sp->block = snd_util_mem_alloc(hdr, truesize * 2);
  161. if (sp->block == NULL) {
  162. /*snd_printd("EMU8000: out of memory\n");*/
  163. /* not ENOMEM (for compatibility) */
  164. return -ENOSPC;
  165. }
  166. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS) {
  167. if (!access_ok(VERIFY_READ, data, sp->v.size))
  168. return -EFAULT;
  169. } else {
  170. if (!access_ok(VERIFY_READ, data, sp->v.size * 2))
  171. return -EFAULT;
  172. }
  173. /* recalculate address offset */
  174. sp->v.end -= sp->v.start;
  175. sp->v.loopstart -= sp->v.start;
  176. sp->v.loopend -= sp->v.start;
  177. sp->v.start = 0;
  178. /* dram position (in word) -- mem_offset is byte */
  179. dram_offset = EMU8000_DRAM_OFFSET + (sp->block->offset >> 1);
  180. dram_start = dram_offset;
  181. /* set the total size (store onto obsolete checksum value) */
  182. sp->v.truesize = truesize * 2; /* in bytes */
  183. snd_emux_terminate_all(emu->emu);
  184. if ((rc = snd_emu8000_open_dma(emu, EMU8000_RAM_WRITE)) != 0)
  185. return rc;
  186. /* Set the address to start writing at */
  187. snd_emu8000_write_wait(emu);
  188. EMU8000_SMALW_WRITE(emu, dram_offset);
  189. /*snd_emu8000_init_fm(emu);*/
  190. #if 0
  191. /* first block - write 48 samples for silence */
  192. if (! sp->block->offset) {
  193. for (i = 0; i < BLANK_HEAD_SIZE; i++) {
  194. write_word(emu, &dram_offset, 0);
  195. }
  196. }
  197. #endif
  198. offset = 0;
  199. for (i = 0; i < sp->v.size; i++) {
  200. unsigned short s;
  201. s = read_word(data, offset, sp->v.mode_flags);
  202. offset++;
  203. write_word(emu, &dram_offset, s);
  204. /* we may take too long time in this loop.
  205. * so give controls back to kernel if needed.
  206. */
  207. cond_resched();
  208. if (i == sp->v.loopend &&
  209. (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)))
  210. {
  211. int looplen = sp->v.loopend - sp->v.loopstart;
  212. int k;
  213. /* copy reverse loop */
  214. for (k = 1; k <= looplen; k++) {
  215. s = read_word(data, offset - k, sp->v.mode_flags);
  216. write_word(emu, &dram_offset, s);
  217. }
  218. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) {
  219. sp->v.loopend += looplen;
  220. } else {
  221. sp->v.loopstart += looplen;
  222. sp->v.loopend += looplen;
  223. }
  224. sp->v.end += looplen;
  225. }
  226. }
  227. /* if no blank loop is attached in the sample, add it */
  228. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
  229. for (i = 0; i < BLANK_LOOP_SIZE; i++) {
  230. write_word(emu, &dram_offset, 0);
  231. }
  232. if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) {
  233. sp->v.loopstart = sp->v.end + BLANK_LOOP_START;
  234. sp->v.loopend = sp->v.end + BLANK_LOOP_END;
  235. }
  236. }
  237. /* add dram offset */
  238. sp->v.start += dram_start;
  239. sp->v.end += dram_start;
  240. sp->v.loopstart += dram_start;
  241. sp->v.loopend += dram_start;
  242. snd_emu8000_close_dma(emu);
  243. snd_emu8000_init_fm(emu);
  244. return 0;
  245. }
  246. /*
  247. * free a sample block
  248. */
  249. int
  250. snd_emu8000_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
  251. struct snd_util_memhdr *hdr)
  252. {
  253. if (sp->block) {
  254. snd_util_mem_free(hdr, sp->block);
  255. sp->block = NULL;
  256. }
  257. return 0;
  258. }
  259. /*
  260. * sample_reset callback - terminate voices
  261. */
  262. void
  263. snd_emu8000_sample_reset(struct snd_emux *rec)
  264. {
  265. snd_emux_terminate_all(rec);
  266. }