emu8000_patch.c 6.7 KB

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