emu8000.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * and (c) 1999 Steve Ratcliffe <steve@parabola.demon.co.uk>
  4. * Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * Routines for control of EMU8000 chip
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/wait.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/ioport.h>
  26. #include <linux/delay.h>
  27. #include <sound/core.h>
  28. #include <sound/emu8000.h>
  29. #include <sound/emu8000_reg.h>
  30. #include <asm/io.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/init.h>
  33. #include <sound/control.h>
  34. #include <sound/initval.h>
  35. /*
  36. * emu8000 register controls
  37. */
  38. /*
  39. * The following routines read and write registers on the emu8000. They
  40. * should always be called via the EMU8000*READ/WRITE macros and never
  41. * directly. The macros handle the port number and command word.
  42. */
  43. /* Write a word */
  44. void snd_emu8000_poke(struct snd_emu8000 *emu, unsigned int port, unsigned int reg, unsigned int val)
  45. {
  46. unsigned long flags;
  47. spin_lock_irqsave(&emu->reg_lock, flags);
  48. if (reg != emu->last_reg) {
  49. outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */
  50. emu->last_reg = reg;
  51. }
  52. outw((unsigned short)val, port); /* Send data */
  53. spin_unlock_irqrestore(&emu->reg_lock, flags);
  54. }
  55. /* Read a word */
  56. unsigned short snd_emu8000_peek(struct snd_emu8000 *emu, unsigned int port, unsigned int reg)
  57. {
  58. unsigned short res;
  59. unsigned long flags;
  60. spin_lock_irqsave(&emu->reg_lock, flags);
  61. if (reg != emu->last_reg) {
  62. outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */
  63. emu->last_reg = reg;
  64. }
  65. res = inw(port); /* Read data */
  66. spin_unlock_irqrestore(&emu->reg_lock, flags);
  67. return res;
  68. }
  69. /* Write a double word */
  70. void snd_emu8000_poke_dw(struct snd_emu8000 *emu, unsigned int port, unsigned int reg, unsigned int val)
  71. {
  72. unsigned long flags;
  73. spin_lock_irqsave(&emu->reg_lock, flags);
  74. if (reg != emu->last_reg) {
  75. outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */
  76. emu->last_reg = reg;
  77. }
  78. outw((unsigned short)val, port); /* Send low word of data */
  79. outw((unsigned short)(val>>16), port+2); /* Send high word of data */
  80. spin_unlock_irqrestore(&emu->reg_lock, flags);
  81. }
  82. /* Read a double word */
  83. unsigned int snd_emu8000_peek_dw(struct snd_emu8000 *emu, unsigned int port, unsigned int reg)
  84. {
  85. unsigned short low;
  86. unsigned int res;
  87. unsigned long flags;
  88. spin_lock_irqsave(&emu->reg_lock, flags);
  89. if (reg != emu->last_reg) {
  90. outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */
  91. emu->last_reg = reg;
  92. }
  93. low = inw(port); /* Read low word of data */
  94. res = low + (inw(port+2) << 16);
  95. spin_unlock_irqrestore(&emu->reg_lock, flags);
  96. return res;
  97. }
  98. /*
  99. * Set up / close a channel to be used for DMA.
  100. */
  101. /*exported*/ void
  102. snd_emu8000_dma_chan(struct snd_emu8000 *emu, int ch, int mode)
  103. {
  104. unsigned right_bit = (mode & EMU8000_RAM_RIGHT) ? 0x01000000 : 0;
  105. mode &= EMU8000_RAM_MODE_MASK;
  106. if (mode == EMU8000_RAM_CLOSE) {
  107. EMU8000_CCCA_WRITE(emu, ch, 0);
  108. EMU8000_DCYSUSV_WRITE(emu, ch, 0x807F);
  109. return;
  110. }
  111. EMU8000_DCYSUSV_WRITE(emu, ch, 0x80);
  112. EMU8000_VTFT_WRITE(emu, ch, 0);
  113. EMU8000_CVCF_WRITE(emu, ch, 0);
  114. EMU8000_PTRX_WRITE(emu, ch, 0x40000000);
  115. EMU8000_CPF_WRITE(emu, ch, 0x40000000);
  116. EMU8000_PSST_WRITE(emu, ch, 0);
  117. EMU8000_CSL_WRITE(emu, ch, 0);
  118. if (mode == EMU8000_RAM_WRITE) /* DMA write */
  119. EMU8000_CCCA_WRITE(emu, ch, 0x06000000 | right_bit);
  120. else /* DMA read */
  121. EMU8000_CCCA_WRITE(emu, ch, 0x04000000 | right_bit);
  122. }
  123. /*
  124. */
  125. static void __devinit
  126. snd_emu8000_read_wait(struct snd_emu8000 *emu)
  127. {
  128. while ((EMU8000_SMALR_READ(emu) & 0x80000000) != 0) {
  129. schedule_timeout_interruptible(1);
  130. if (signal_pending(current))
  131. break;
  132. }
  133. }
  134. /*
  135. */
  136. static void __devinit
  137. snd_emu8000_write_wait(struct snd_emu8000 *emu)
  138. {
  139. while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
  140. schedule_timeout_interruptible(1);
  141. if (signal_pending(current))
  142. break;
  143. }
  144. }
  145. /*
  146. * detect a card at the given port
  147. */
  148. static int __devinit
  149. snd_emu8000_detect(struct snd_emu8000 *emu)
  150. {
  151. /* Initialise */
  152. EMU8000_HWCF1_WRITE(emu, 0x0059);
  153. EMU8000_HWCF2_WRITE(emu, 0x0020);
  154. EMU8000_HWCF3_WRITE(emu, 0x0000);
  155. /* Check for a recognisable emu8000 */
  156. /*
  157. if ((EMU8000_U1_READ(emu) & 0x000f) != 0x000c)
  158. return -ENODEV;
  159. */
  160. if ((EMU8000_HWCF1_READ(emu) & 0x007e) != 0x0058)
  161. return -ENODEV;
  162. if ((EMU8000_HWCF2_READ(emu) & 0x0003) != 0x0003)
  163. return -ENODEV;
  164. snd_printdd("EMU8000 [0x%lx]: Synth chip found\n",
  165. emu->port1);
  166. return 0;
  167. }
  168. /*
  169. * intiailize audio channels
  170. */
  171. static void __devinit
  172. init_audio(struct snd_emu8000 *emu)
  173. {
  174. int ch;
  175. /* turn off envelope engines */
  176. for (ch = 0; ch < EMU8000_CHANNELS; ch++)
  177. EMU8000_DCYSUSV_WRITE(emu, ch, 0x80);
  178. /* reset all other parameters to zero */
  179. for (ch = 0; ch < EMU8000_CHANNELS; ch++) {
  180. EMU8000_ENVVOL_WRITE(emu, ch, 0);
  181. EMU8000_ENVVAL_WRITE(emu, ch, 0);
  182. EMU8000_DCYSUS_WRITE(emu, ch, 0);
  183. EMU8000_ATKHLDV_WRITE(emu, ch, 0);
  184. EMU8000_LFO1VAL_WRITE(emu, ch, 0);
  185. EMU8000_ATKHLD_WRITE(emu, ch, 0);
  186. EMU8000_LFO2VAL_WRITE(emu, ch, 0);
  187. EMU8000_IP_WRITE(emu, ch, 0);
  188. EMU8000_IFATN_WRITE(emu, ch, 0);
  189. EMU8000_PEFE_WRITE(emu, ch, 0);
  190. EMU8000_FMMOD_WRITE(emu, ch, 0);
  191. EMU8000_TREMFRQ_WRITE(emu, ch, 0);
  192. EMU8000_FM2FRQ2_WRITE(emu, ch, 0);
  193. EMU8000_PTRX_WRITE(emu, ch, 0);
  194. EMU8000_VTFT_WRITE(emu, ch, 0);
  195. EMU8000_PSST_WRITE(emu, ch, 0);
  196. EMU8000_CSL_WRITE(emu, ch, 0);
  197. EMU8000_CCCA_WRITE(emu, ch, 0);
  198. }
  199. for (ch = 0; ch < EMU8000_CHANNELS; ch++) {
  200. EMU8000_CPF_WRITE(emu, ch, 0);
  201. EMU8000_CVCF_WRITE(emu, ch, 0);
  202. }
  203. }
  204. /*
  205. * initialize DMA address
  206. */
  207. static void __devinit
  208. init_dma(struct snd_emu8000 *emu)
  209. {
  210. EMU8000_SMALR_WRITE(emu, 0);
  211. EMU8000_SMARR_WRITE(emu, 0);
  212. EMU8000_SMALW_WRITE(emu, 0);
  213. EMU8000_SMARW_WRITE(emu, 0);
  214. }
  215. /*
  216. * initialization arrays; from ADIP
  217. */
  218. static unsigned short init1[128] /*__devinitdata*/ = {
  219. 0x03ff, 0x0030, 0x07ff, 0x0130, 0x0bff, 0x0230, 0x0fff, 0x0330,
  220. 0x13ff, 0x0430, 0x17ff, 0x0530, 0x1bff, 0x0630, 0x1fff, 0x0730,
  221. 0x23ff, 0x0830, 0x27ff, 0x0930, 0x2bff, 0x0a30, 0x2fff, 0x0b30,
  222. 0x33ff, 0x0c30, 0x37ff, 0x0d30, 0x3bff, 0x0e30, 0x3fff, 0x0f30,
  223. 0x43ff, 0x0030, 0x47ff, 0x0130, 0x4bff, 0x0230, 0x4fff, 0x0330,
  224. 0x53ff, 0x0430, 0x57ff, 0x0530, 0x5bff, 0x0630, 0x5fff, 0x0730,
  225. 0x63ff, 0x0830, 0x67ff, 0x0930, 0x6bff, 0x0a30, 0x6fff, 0x0b30,
  226. 0x73ff, 0x0c30, 0x77ff, 0x0d30, 0x7bff, 0x0e30, 0x7fff, 0x0f30,
  227. 0x83ff, 0x0030, 0x87ff, 0x0130, 0x8bff, 0x0230, 0x8fff, 0x0330,
  228. 0x93ff, 0x0430, 0x97ff, 0x0530, 0x9bff, 0x0630, 0x9fff, 0x0730,
  229. 0xa3ff, 0x0830, 0xa7ff, 0x0930, 0xabff, 0x0a30, 0xafff, 0x0b30,
  230. 0xb3ff, 0x0c30, 0xb7ff, 0x0d30, 0xbbff, 0x0e30, 0xbfff, 0x0f30,
  231. 0xc3ff, 0x0030, 0xc7ff, 0x0130, 0xcbff, 0x0230, 0xcfff, 0x0330,
  232. 0xd3ff, 0x0430, 0xd7ff, 0x0530, 0xdbff, 0x0630, 0xdfff, 0x0730,
  233. 0xe3ff, 0x0830, 0xe7ff, 0x0930, 0xebff, 0x0a30, 0xefff, 0x0b30,
  234. 0xf3ff, 0x0c30, 0xf7ff, 0x0d30, 0xfbff, 0x0e30, 0xffff, 0x0f30,
  235. };
  236. static unsigned short init2[128] /*__devinitdata*/ = {
  237. 0x03ff, 0x8030, 0x07ff, 0x8130, 0x0bff, 0x8230, 0x0fff, 0x8330,
  238. 0x13ff, 0x8430, 0x17ff, 0x8530, 0x1bff, 0x8630, 0x1fff, 0x8730,
  239. 0x23ff, 0x8830, 0x27ff, 0x8930, 0x2bff, 0x8a30, 0x2fff, 0x8b30,
  240. 0x33ff, 0x8c30, 0x37ff, 0x8d30, 0x3bff, 0x8e30, 0x3fff, 0x8f30,
  241. 0x43ff, 0x8030, 0x47ff, 0x8130, 0x4bff, 0x8230, 0x4fff, 0x8330,
  242. 0x53ff, 0x8430, 0x57ff, 0x8530, 0x5bff, 0x8630, 0x5fff, 0x8730,
  243. 0x63ff, 0x8830, 0x67ff, 0x8930, 0x6bff, 0x8a30, 0x6fff, 0x8b30,
  244. 0x73ff, 0x8c30, 0x77ff, 0x8d30, 0x7bff, 0x8e30, 0x7fff, 0x8f30,
  245. 0x83ff, 0x8030, 0x87ff, 0x8130, 0x8bff, 0x8230, 0x8fff, 0x8330,
  246. 0x93ff, 0x8430, 0x97ff, 0x8530, 0x9bff, 0x8630, 0x9fff, 0x8730,
  247. 0xa3ff, 0x8830, 0xa7ff, 0x8930, 0xabff, 0x8a30, 0xafff, 0x8b30,
  248. 0xb3ff, 0x8c30, 0xb7ff, 0x8d30, 0xbbff, 0x8e30, 0xbfff, 0x8f30,
  249. 0xc3ff, 0x8030, 0xc7ff, 0x8130, 0xcbff, 0x8230, 0xcfff, 0x8330,
  250. 0xd3ff, 0x8430, 0xd7ff, 0x8530, 0xdbff, 0x8630, 0xdfff, 0x8730,
  251. 0xe3ff, 0x8830, 0xe7ff, 0x8930, 0xebff, 0x8a30, 0xefff, 0x8b30,
  252. 0xf3ff, 0x8c30, 0xf7ff, 0x8d30, 0xfbff, 0x8e30, 0xffff, 0x8f30,
  253. };
  254. static unsigned short init3[128] /*__devinitdata*/ = {
  255. 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
  256. 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x8F7C, 0x167E, 0xF254,
  257. 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x8BAA, 0x1B6D, 0xF234,
  258. 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x86E7, 0x229E, 0xF224,
  259. 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x87F6, 0x2C28, 0xF254,
  260. 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x8F02, 0x1341, 0xF264,
  261. 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x8FA9, 0x3EB5, 0xF294,
  262. 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0xC4C3, 0x3EBB, 0xC5C3,
  263. 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x8671, 0x14FD, 0x8287,
  264. 0x3EBC, 0xE610, 0x3EC8, 0x8C7B, 0x031A, 0x87E6, 0x3EC8, 0x86F7,
  265. 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x821F, 0x3ECA, 0x8386,
  266. 0x3EC1, 0x8C03, 0x3EC9, 0x831E, 0x3ECA, 0x8C4C, 0x3EBF, 0x8C55,
  267. 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x8EAD, 0x3EC8, 0xD308,
  268. 0x3EC2, 0x8F7E, 0x3ECB, 0x8219, 0x3ECB, 0xD26E, 0x3EC5, 0x831F,
  269. 0x3EC6, 0xC308, 0x3EC3, 0xB2FF, 0x3EC9, 0x8265, 0x3EC9, 0x8319,
  270. 0x1342, 0xD36E, 0x3EC7, 0xB3FF, 0x0000, 0x8365, 0x1420, 0x9570,
  271. };
  272. static unsigned short init4[128] /*__devinitdata*/ = {
  273. 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
  274. 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x0F7C, 0x167E, 0x7254,
  275. 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x0BAA, 0x1B6D, 0x7234,
  276. 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x06E7, 0x229E, 0x7224,
  277. 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x07F6, 0x2C28, 0x7254,
  278. 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x0F02, 0x1341, 0x7264,
  279. 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x0FA9, 0x3EB5, 0x7294,
  280. 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0x44C3, 0x3EBB, 0x45C3,
  281. 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x0671, 0x14FD, 0x0287,
  282. 0x3EBC, 0xE610, 0x3EC8, 0x0C7B, 0x031A, 0x07E6, 0x3EC8, 0x86F7,
  283. 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x021F, 0x3ECA, 0x0386,
  284. 0x3EC1, 0x0C03, 0x3EC9, 0x031E, 0x3ECA, 0x8C4C, 0x3EBF, 0x0C55,
  285. 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x0EAD, 0x3EC8, 0xD308,
  286. 0x3EC2, 0x8F7E, 0x3ECB, 0x0219, 0x3ECB, 0xD26E, 0x3EC5, 0x031F,
  287. 0x3EC6, 0xC308, 0x3EC3, 0x32FF, 0x3EC9, 0x0265, 0x3EC9, 0x8319,
  288. 0x1342, 0xD36E, 0x3EC7, 0x33FF, 0x0000, 0x8365, 0x1420, 0x9570,
  289. };
  290. /* send an initialization array
  291. * Taken from the oss driver, not obvious from the doc how this
  292. * is meant to work
  293. */
  294. static void __devinit
  295. send_array(struct snd_emu8000 *emu, unsigned short *data, int size)
  296. {
  297. int i;
  298. unsigned short *p;
  299. p = data;
  300. for (i = 0; i < size; i++, p++)
  301. EMU8000_INIT1_WRITE(emu, i, *p);
  302. for (i = 0; i < size; i++, p++)
  303. EMU8000_INIT2_WRITE(emu, i, *p);
  304. for (i = 0; i < size; i++, p++)
  305. EMU8000_INIT3_WRITE(emu, i, *p);
  306. for (i = 0; i < size; i++, p++)
  307. EMU8000_INIT4_WRITE(emu, i, *p);
  308. }
  309. /*
  310. * Send initialization arrays to start up, this just follows the
  311. * initialisation sequence in the adip.
  312. */
  313. static void __devinit
  314. init_arrays(struct snd_emu8000 *emu)
  315. {
  316. send_array(emu, init1, ARRAY_SIZE(init1)/4);
  317. msleep((1024 * 1000) / 44100); /* wait for 1024 clocks */
  318. send_array(emu, init2, ARRAY_SIZE(init2)/4);
  319. send_array(emu, init3, ARRAY_SIZE(init3)/4);
  320. EMU8000_HWCF4_WRITE(emu, 0);
  321. EMU8000_HWCF5_WRITE(emu, 0x83);
  322. EMU8000_HWCF6_WRITE(emu, 0x8000);
  323. send_array(emu, init4, ARRAY_SIZE(init4)/4);
  324. }
  325. #define UNIQUE_ID1 0xa5b9
  326. #define UNIQUE_ID2 0x9d53
  327. /*
  328. * Size the onboard memory.
  329. * This is written so as not to need arbitary delays after the write. It
  330. * seems that the only way to do this is to use the one channel and keep
  331. * reallocating between read and write.
  332. */
  333. static void __devinit
  334. size_dram(struct snd_emu8000 *emu)
  335. {
  336. int i, size;
  337. if (emu->dram_checked)
  338. return;
  339. size = 0;
  340. /* write out a magic number */
  341. snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_WRITE);
  342. snd_emu8000_dma_chan(emu, 1, EMU8000_RAM_READ);
  343. EMU8000_SMALW_WRITE(emu, EMU8000_DRAM_OFFSET);
  344. EMU8000_SMLD_WRITE(emu, UNIQUE_ID1);
  345. snd_emu8000_init_fm(emu); /* This must really be here and not 2 lines back even */
  346. while (size < EMU8000_MAX_DRAM) {
  347. size += 512 * 1024; /* increment 512kbytes */
  348. /* Write a unique data on the test address.
  349. * if the address is out of range, the data is written on
  350. * 0x200000(=EMU8000_DRAM_OFFSET). Then the id word is
  351. * changed by this data.
  352. */
  353. /*snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_WRITE);*/
  354. EMU8000_SMALW_WRITE(emu, EMU8000_DRAM_OFFSET + (size>>1));
  355. EMU8000_SMLD_WRITE(emu, UNIQUE_ID2);
  356. snd_emu8000_write_wait(emu);
  357. /*
  358. * read the data on the just written DRAM address
  359. * if not the same then we have reached the end of ram.
  360. */
  361. /*snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_READ);*/
  362. EMU8000_SMALR_WRITE(emu, EMU8000_DRAM_OFFSET + (size>>1));
  363. /*snd_emu8000_read_wait(emu);*/
  364. EMU8000_SMLD_READ(emu); /* discard stale data */
  365. if (EMU8000_SMLD_READ(emu) != UNIQUE_ID2)
  366. break; /* we must have wrapped around */
  367. snd_emu8000_read_wait(emu);
  368. /*
  369. * If it is the same it could be that the address just
  370. * wraps back to the beginning; so check to see if the
  371. * initial value has been overwritten.
  372. */
  373. EMU8000_SMALR_WRITE(emu, EMU8000_DRAM_OFFSET);
  374. EMU8000_SMLD_READ(emu); /* discard stale data */
  375. if (EMU8000_SMLD_READ(emu) != UNIQUE_ID1)
  376. break; /* we must have wrapped around */
  377. snd_emu8000_read_wait(emu);
  378. }
  379. /* wait until FULL bit in SMAxW register is false */
  380. for (i = 0; i < 10000; i++) {
  381. if ((EMU8000_SMALW_READ(emu) & 0x80000000) == 0)
  382. break;
  383. schedule_timeout_interruptible(1);
  384. if (signal_pending(current))
  385. break;
  386. }
  387. snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_CLOSE);
  388. snd_emu8000_dma_chan(emu, 1, EMU8000_RAM_CLOSE);
  389. snd_printdd("EMU8000 [0x%lx]: %d Kb on-board memory detected\n",
  390. emu->port1, size/1024);
  391. emu->mem_size = size;
  392. emu->dram_checked = 1;
  393. }
  394. /*
  395. * Initiailise the FM section. You have to do this to use sample RAM
  396. * and therefore lose 2 voices.
  397. */
  398. /*exported*/ void
  399. snd_emu8000_init_fm(struct snd_emu8000 *emu)
  400. {
  401. unsigned long flags;
  402. /* Initialize the last two channels for DRAM refresh and producing
  403. the reverb and chorus effects for Yamaha OPL-3 synthesizer */
  404. /* 31: FM left channel, 0xffffe0-0xffffe8 */
  405. EMU8000_DCYSUSV_WRITE(emu, 30, 0x80);
  406. EMU8000_PSST_WRITE(emu, 30, 0xFFFFFFE0); /* full left */
  407. EMU8000_CSL_WRITE(emu, 30, 0x00FFFFE8 | (emu->fm_chorus_depth << 24));
  408. EMU8000_PTRX_WRITE(emu, 30, (emu->fm_reverb_depth << 8));
  409. EMU8000_CPF_WRITE(emu, 30, 0);
  410. EMU8000_CCCA_WRITE(emu, 30, 0x00FFFFE3);
  411. /* 32: FM right channel, 0xfffff0-0xfffff8 */
  412. EMU8000_DCYSUSV_WRITE(emu, 31, 0x80);
  413. EMU8000_PSST_WRITE(emu, 31, 0x00FFFFF0); /* full right */
  414. EMU8000_CSL_WRITE(emu, 31, 0x00FFFFF8 | (emu->fm_chorus_depth << 24));
  415. EMU8000_PTRX_WRITE(emu, 31, (emu->fm_reverb_depth << 8));
  416. EMU8000_CPF_WRITE(emu, 31, 0x8000);
  417. EMU8000_CCCA_WRITE(emu, 31, 0x00FFFFF3);
  418. snd_emu8000_poke((emu), EMU8000_DATA0(emu), EMU8000_CMD(1, (30)), 0);
  419. spin_lock_irqsave(&emu->reg_lock, flags);
  420. while (!(inw(EMU8000_PTR(emu)) & 0x1000))
  421. ;
  422. while ((inw(EMU8000_PTR(emu)) & 0x1000))
  423. ;
  424. spin_unlock_irqrestore(&emu->reg_lock, flags);
  425. snd_emu8000_poke((emu), EMU8000_DATA0(emu), EMU8000_CMD(1, (30)), 0x4828);
  426. /* this is really odd part.. */
  427. outb(0x3C, EMU8000_PTR(emu));
  428. outb(0, EMU8000_DATA1(emu));
  429. /* skew volume & cutoff */
  430. EMU8000_VTFT_WRITE(emu, 30, 0x8000FFFF);
  431. EMU8000_VTFT_WRITE(emu, 31, 0x8000FFFF);
  432. }
  433. /*
  434. * The main initialization routine.
  435. */
  436. static void __devinit
  437. snd_emu8000_init_hw(struct snd_emu8000 *emu)
  438. {
  439. int i;
  440. emu->last_reg = 0xffff; /* reset the last register index */
  441. /* initialize hardware configuration */
  442. EMU8000_HWCF1_WRITE(emu, 0x0059);
  443. EMU8000_HWCF2_WRITE(emu, 0x0020);
  444. /* disable audio; this seems to reduce a clicking noise a bit.. */
  445. EMU8000_HWCF3_WRITE(emu, 0);
  446. /* initialize audio channels */
  447. init_audio(emu);
  448. /* initialize DMA */
  449. init_dma(emu);
  450. /* initialize init arrays */
  451. init_arrays(emu);
  452. /*
  453. * Initialize the FM section of the AWE32, this is needed
  454. * for DRAM refresh as well
  455. */
  456. snd_emu8000_init_fm(emu);
  457. /* terminate all voices */
  458. for (i = 0; i < EMU8000_DRAM_VOICES; i++)
  459. EMU8000_DCYSUSV_WRITE(emu, 0, 0x807F);
  460. /* check DRAM memory size */
  461. size_dram(emu);
  462. /* enable audio */
  463. EMU8000_HWCF3_WRITE(emu, 0x4);
  464. /* set equzlier, chorus and reverb modes */
  465. snd_emu8000_update_equalizer(emu);
  466. snd_emu8000_update_chorus_mode(emu);
  467. snd_emu8000_update_reverb_mode(emu);
  468. }
  469. /*----------------------------------------------------------------
  470. * Bass/Treble Equalizer
  471. *----------------------------------------------------------------*/
  472. static unsigned short bass_parm[12][3] = {
  473. {0xD26A, 0xD36A, 0x0000}, /* -12 dB */
  474. {0xD25B, 0xD35B, 0x0000}, /* -8 */
  475. {0xD24C, 0xD34C, 0x0000}, /* -6 */
  476. {0xD23D, 0xD33D, 0x0000}, /* -4 */
  477. {0xD21F, 0xD31F, 0x0000}, /* -2 */
  478. {0xC208, 0xC308, 0x0001}, /* 0 (HW default) */
  479. {0xC219, 0xC319, 0x0001}, /* +2 */
  480. {0xC22A, 0xC32A, 0x0001}, /* +4 */
  481. {0xC24C, 0xC34C, 0x0001}, /* +6 */
  482. {0xC26E, 0xC36E, 0x0001}, /* +8 */
  483. {0xC248, 0xC384, 0x0002}, /* +10 */
  484. {0xC26A, 0xC36A, 0x0002}, /* +12 dB */
  485. };
  486. static unsigned short treble_parm[12][9] = {
  487. {0x821E, 0xC26A, 0x031E, 0xC36A, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, /* -12 dB */
  488. {0x821E, 0xC25B, 0x031E, 0xC35B, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  489. {0x821E, 0xC24C, 0x031E, 0xC34C, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  490. {0x821E, 0xC23D, 0x031E, 0xC33D, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  491. {0x821E, 0xC21F, 0x031E, 0xC31F, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  492. {0x821E, 0xD208, 0x031E, 0xD308, 0x021E, 0xD208, 0x831E, 0xD308, 0x0002},
  493. {0x821E, 0xD208, 0x031E, 0xD308, 0x021D, 0xD219, 0x831D, 0xD319, 0x0002},
  494. {0x821E, 0xD208, 0x031E, 0xD308, 0x021C, 0xD22A, 0x831C, 0xD32A, 0x0002},
  495. {0x821E, 0xD208, 0x031E, 0xD308, 0x021A, 0xD24C, 0x831A, 0xD34C, 0x0002},
  496. {0x821E, 0xD208, 0x031E, 0xD308, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +8 (HW default) */
  497. {0x821D, 0xD219, 0x031D, 0xD319, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002},
  498. {0x821C, 0xD22A, 0x031C, 0xD32A, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002} /* +12 dB */
  499. };
  500. /*
  501. * set Emu8000 digital equalizer; from 0 to 11 [-12dB - 12dB]
  502. */
  503. /*exported*/ void
  504. snd_emu8000_update_equalizer(struct snd_emu8000 *emu)
  505. {
  506. unsigned short w;
  507. int bass = emu->bass_level;
  508. int treble = emu->treble_level;
  509. if (bass < 0 || bass > 11 || treble < 0 || treble > 11)
  510. return;
  511. EMU8000_INIT4_WRITE(emu, 0x01, bass_parm[bass][0]);
  512. EMU8000_INIT4_WRITE(emu, 0x11, bass_parm[bass][1]);
  513. EMU8000_INIT3_WRITE(emu, 0x11, treble_parm[treble][0]);
  514. EMU8000_INIT3_WRITE(emu, 0x13, treble_parm[treble][1]);
  515. EMU8000_INIT3_WRITE(emu, 0x1b, treble_parm[treble][2]);
  516. EMU8000_INIT4_WRITE(emu, 0x07, treble_parm[treble][3]);
  517. EMU8000_INIT4_WRITE(emu, 0x0b, treble_parm[treble][4]);
  518. EMU8000_INIT4_WRITE(emu, 0x0d, treble_parm[treble][5]);
  519. EMU8000_INIT4_WRITE(emu, 0x17, treble_parm[treble][6]);
  520. EMU8000_INIT4_WRITE(emu, 0x19, treble_parm[treble][7]);
  521. w = bass_parm[bass][2] + treble_parm[treble][8];
  522. EMU8000_INIT4_WRITE(emu, 0x15, (unsigned short)(w + 0x0262));
  523. EMU8000_INIT4_WRITE(emu, 0x1d, (unsigned short)(w + 0x8362));
  524. }
  525. /*----------------------------------------------------------------
  526. * Chorus mode control
  527. *----------------------------------------------------------------*/
  528. /*
  529. * chorus mode parameters
  530. */
  531. #define SNDRV_EMU8000_CHORUS_1 0
  532. #define SNDRV_EMU8000_CHORUS_2 1
  533. #define SNDRV_EMU8000_CHORUS_3 2
  534. #define SNDRV_EMU8000_CHORUS_4 3
  535. #define SNDRV_EMU8000_CHORUS_FEEDBACK 4
  536. #define SNDRV_EMU8000_CHORUS_FLANGER 5
  537. #define SNDRV_EMU8000_CHORUS_SHORTDELAY 6
  538. #define SNDRV_EMU8000_CHORUS_SHORTDELAY2 7
  539. #define SNDRV_EMU8000_CHORUS_PREDEFINED 8
  540. /* user can define chorus modes up to 32 */
  541. #define SNDRV_EMU8000_CHORUS_NUMBERS 32
  542. struct soundfont_chorus_fx {
  543. unsigned short feedback; /* feedback level (0xE600-0xE6FF) */
  544. unsigned short delay_offset; /* delay (0-0x0DA3) [1/44100 sec] */
  545. unsigned short lfo_depth; /* LFO depth (0xBC00-0xBCFF) */
  546. unsigned int delay; /* right delay (0-0xFFFFFFFF) [1/256/44100 sec] */
  547. unsigned int lfo_freq; /* LFO freq LFO freq (0-0xFFFFFFFF) */
  548. };
  549. /* 5 parameters for each chorus mode; 3 x 16bit, 2 x 32bit */
  550. static char chorus_defined[SNDRV_EMU8000_CHORUS_NUMBERS];
  551. static struct soundfont_chorus_fx chorus_parm[SNDRV_EMU8000_CHORUS_NUMBERS] = {
  552. {0xE600, 0x03F6, 0xBC2C ,0x00000000, 0x0000006D}, /* chorus 1 */
  553. {0xE608, 0x031A, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 2 */
  554. {0xE610, 0x031A, 0xBC84, 0x00000000, 0x00000083}, /* chorus 3 */
  555. {0xE620, 0x0269, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 4 */
  556. {0xE680, 0x04D3, 0xBCA6, 0x00000000, 0x0000005B}, /* feedback */
  557. {0xE6E0, 0x044E, 0xBC37, 0x00000000, 0x00000026}, /* flanger */
  558. {0xE600, 0x0B06, 0xBC00, 0x0006E000, 0x00000083}, /* short delay */
  559. {0xE6C0, 0x0B06, 0xBC00, 0x0006E000, 0x00000083}, /* short delay + feedback */
  560. };
  561. /*exported*/ int
  562. snd_emu8000_load_chorus_fx(struct snd_emu8000 *emu, int mode, const void __user *buf, long len)
  563. {
  564. struct soundfont_chorus_fx rec;
  565. if (mode < SNDRV_EMU8000_CHORUS_PREDEFINED || mode >= SNDRV_EMU8000_CHORUS_NUMBERS) {
  566. snd_printk(KERN_WARNING "invalid chorus mode %d for uploading\n", mode);
  567. return -EINVAL;
  568. }
  569. if (len < (long)sizeof(rec) || copy_from_user(&rec, buf, sizeof(rec)))
  570. return -EFAULT;
  571. chorus_parm[mode] = rec;
  572. chorus_defined[mode] = 1;
  573. return 0;
  574. }
  575. /*exported*/ void
  576. snd_emu8000_update_chorus_mode(struct snd_emu8000 *emu)
  577. {
  578. int effect = emu->chorus_mode;
  579. if (effect < 0 || effect >= SNDRV_EMU8000_CHORUS_NUMBERS ||
  580. (effect >= SNDRV_EMU8000_CHORUS_PREDEFINED && !chorus_defined[effect]))
  581. return;
  582. EMU8000_INIT3_WRITE(emu, 0x09, chorus_parm[effect].feedback);
  583. EMU8000_INIT3_WRITE(emu, 0x0c, chorus_parm[effect].delay_offset);
  584. EMU8000_INIT4_WRITE(emu, 0x03, chorus_parm[effect].lfo_depth);
  585. EMU8000_HWCF4_WRITE(emu, chorus_parm[effect].delay);
  586. EMU8000_HWCF5_WRITE(emu, chorus_parm[effect].lfo_freq);
  587. EMU8000_HWCF6_WRITE(emu, 0x8000);
  588. EMU8000_HWCF7_WRITE(emu, 0x0000);
  589. }
  590. /*----------------------------------------------------------------
  591. * Reverb mode control
  592. *----------------------------------------------------------------*/
  593. /*
  594. * reverb mode parameters
  595. */
  596. #define SNDRV_EMU8000_REVERB_ROOM1 0
  597. #define SNDRV_EMU8000_REVERB_ROOM2 1
  598. #define SNDRV_EMU8000_REVERB_ROOM3 2
  599. #define SNDRV_EMU8000_REVERB_HALL1 3
  600. #define SNDRV_EMU8000_REVERB_HALL2 4
  601. #define SNDRV_EMU8000_REVERB_PLATE 5
  602. #define SNDRV_EMU8000_REVERB_DELAY 6
  603. #define SNDRV_EMU8000_REVERB_PANNINGDELAY 7
  604. #define SNDRV_EMU8000_REVERB_PREDEFINED 8
  605. /* user can define reverb modes up to 32 */
  606. #define SNDRV_EMU8000_REVERB_NUMBERS 32
  607. struct soundfont_reverb_fx {
  608. unsigned short parms[28];
  609. };
  610. /* reverb mode settings; write the following 28 data of 16 bit length
  611. * on the corresponding ports in the reverb_cmds array
  612. */
  613. static char reverb_defined[SNDRV_EMU8000_CHORUS_NUMBERS];
  614. static struct soundfont_reverb_fx reverb_parm[SNDRV_EMU8000_REVERB_NUMBERS] = {
  615. {{ /* room 1 */
  616. 0xB488, 0xA450, 0x9550, 0x84B5, 0x383A, 0x3EB5, 0x72F4,
  617. 0x72A4, 0x7254, 0x7204, 0x7204, 0x7204, 0x4416, 0x4516,
  618. 0xA490, 0xA590, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  619. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  620. }},
  621. {{ /* room 2 */
  622. 0xB488, 0xA458, 0x9558, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  623. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
  624. 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  625. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  626. }},
  627. {{ /* room 3 */
  628. 0xB488, 0xA460, 0x9560, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  629. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4416, 0x4516,
  630. 0xA490, 0xA590, 0x842C, 0x852C, 0x842C, 0x852C, 0x842B,
  631. 0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A,
  632. }},
  633. {{ /* hall 1 */
  634. 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  635. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
  636. 0xA440, 0xA540, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A,
  637. 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529,
  638. }},
  639. {{ /* hall 2 */
  640. 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7254,
  641. 0x7234, 0x7224, 0x7254, 0x7264, 0x7294, 0x44C3, 0x45C3,
  642. 0xA404, 0xA504, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  643. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  644. }},
  645. {{ /* plate */
  646. 0xB4FF, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7234,
  647. 0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x4448, 0x4548,
  648. 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  649. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  650. }},
  651. {{ /* delay */
  652. 0xB4FF, 0xA470, 0x9500, 0x84B5, 0x333A, 0x39B5, 0x7204,
  653. 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
  654. 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
  655. 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
  656. }},
  657. {{ /* panning delay */
  658. 0xB4FF, 0xA490, 0x9590, 0x8474, 0x333A, 0x39B5, 0x7204,
  659. 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
  660. 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
  661. 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
  662. }},
  663. };
  664. enum { DATA1, DATA2 };
  665. #define AWE_INIT1(c) EMU8000_CMD(2,c), DATA1
  666. #define AWE_INIT2(c) EMU8000_CMD(2,c), DATA2
  667. #define AWE_INIT3(c) EMU8000_CMD(3,c), DATA1
  668. #define AWE_INIT4(c) EMU8000_CMD(3,c), DATA2
  669. static struct reverb_cmd_pair {
  670. unsigned short cmd, port;
  671. } reverb_cmds[28] = {
  672. {AWE_INIT1(0x03)}, {AWE_INIT1(0x05)}, {AWE_INIT4(0x1F)}, {AWE_INIT1(0x07)},
  673. {AWE_INIT2(0x14)}, {AWE_INIT2(0x16)}, {AWE_INIT1(0x0F)}, {AWE_INIT1(0x17)},
  674. {AWE_INIT1(0x1F)}, {AWE_INIT2(0x07)}, {AWE_INIT2(0x0F)}, {AWE_INIT2(0x17)},
  675. {AWE_INIT2(0x1D)}, {AWE_INIT2(0x1F)}, {AWE_INIT3(0x01)}, {AWE_INIT3(0x03)},
  676. {AWE_INIT1(0x09)}, {AWE_INIT1(0x0B)}, {AWE_INIT1(0x11)}, {AWE_INIT1(0x13)},
  677. {AWE_INIT1(0x19)}, {AWE_INIT1(0x1B)}, {AWE_INIT2(0x01)}, {AWE_INIT2(0x03)},
  678. {AWE_INIT2(0x09)}, {AWE_INIT2(0x0B)}, {AWE_INIT2(0x11)}, {AWE_INIT2(0x13)},
  679. };
  680. /*exported*/ int
  681. snd_emu8000_load_reverb_fx(struct snd_emu8000 *emu, int mode, const void __user *buf, long len)
  682. {
  683. struct soundfont_reverb_fx rec;
  684. if (mode < SNDRV_EMU8000_REVERB_PREDEFINED || mode >= SNDRV_EMU8000_REVERB_NUMBERS) {
  685. snd_printk(KERN_WARNING "invalid reverb mode %d for uploading\n", mode);
  686. return -EINVAL;
  687. }
  688. if (len < (long)sizeof(rec) || copy_from_user(&rec, buf, sizeof(rec)))
  689. return -EFAULT;
  690. reverb_parm[mode] = rec;
  691. reverb_defined[mode] = 1;
  692. return 0;
  693. }
  694. /*exported*/ void
  695. snd_emu8000_update_reverb_mode(struct snd_emu8000 *emu)
  696. {
  697. int effect = emu->reverb_mode;
  698. int i;
  699. if (effect < 0 || effect >= SNDRV_EMU8000_REVERB_NUMBERS ||
  700. (effect >= SNDRV_EMU8000_REVERB_PREDEFINED && !reverb_defined[effect]))
  701. return;
  702. for (i = 0; i < 28; i++) {
  703. int port;
  704. if (reverb_cmds[i].port == DATA1)
  705. port = EMU8000_DATA1(emu);
  706. else
  707. port = EMU8000_DATA2(emu);
  708. snd_emu8000_poke(emu, port, reverb_cmds[i].cmd, reverb_parm[effect].parms[i]);
  709. }
  710. }
  711. /*----------------------------------------------------------------
  712. * mixer interface
  713. *----------------------------------------------------------------*/
  714. /*
  715. * bass/treble
  716. */
  717. static int mixer_bass_treble_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  718. {
  719. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  720. uinfo->count = 1;
  721. uinfo->value.integer.min = 0;
  722. uinfo->value.integer.max = 11;
  723. return 0;
  724. }
  725. static int mixer_bass_treble_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  726. {
  727. struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol);
  728. ucontrol->value.integer.value[0] = kcontrol->private_value ? emu->treble_level : emu->bass_level;
  729. return 0;
  730. }
  731. static int mixer_bass_treble_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  732. {
  733. struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol);
  734. unsigned long flags;
  735. int change;
  736. unsigned short val1;
  737. val1 = ucontrol->value.integer.value[0] % 12;
  738. spin_lock_irqsave(&emu->control_lock, flags);
  739. if (kcontrol->private_value) {
  740. change = val1 != emu->treble_level;
  741. emu->treble_level = val1;
  742. } else {
  743. change = val1 != emu->bass_level;
  744. emu->bass_level = val1;
  745. }
  746. spin_unlock_irqrestore(&emu->control_lock, flags);
  747. snd_emu8000_update_equalizer(emu);
  748. return change;
  749. }
  750. static struct snd_kcontrol_new mixer_bass_control =
  751. {
  752. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  753. .name = "Synth Tone Control - Bass",
  754. .info = mixer_bass_treble_info,
  755. .get = mixer_bass_treble_get,
  756. .put = mixer_bass_treble_put,
  757. .private_value = 0,
  758. };
  759. static struct snd_kcontrol_new mixer_treble_control =
  760. {
  761. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  762. .name = "Synth Tone Control - Treble",
  763. .info = mixer_bass_treble_info,
  764. .get = mixer_bass_treble_get,
  765. .put = mixer_bass_treble_put,
  766. .private_value = 1,
  767. };
  768. /*
  769. * chorus/reverb mode
  770. */
  771. static int mixer_chorus_reverb_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  772. {
  773. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  774. uinfo->count = 1;
  775. uinfo->value.integer.min = 0;
  776. uinfo->value.integer.max = kcontrol->private_value ? (SNDRV_EMU8000_CHORUS_NUMBERS-1) : (SNDRV_EMU8000_REVERB_NUMBERS-1);
  777. return 0;
  778. }
  779. static int mixer_chorus_reverb_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  780. {
  781. struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol);
  782. ucontrol->value.integer.value[0] = kcontrol->private_value ? emu->chorus_mode : emu->reverb_mode;
  783. return 0;
  784. }
  785. static int mixer_chorus_reverb_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  786. {
  787. struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol);
  788. unsigned long flags;
  789. int change;
  790. unsigned short val1;
  791. spin_lock_irqsave(&emu->control_lock, flags);
  792. if (kcontrol->private_value) {
  793. val1 = ucontrol->value.integer.value[0] % SNDRV_EMU8000_CHORUS_NUMBERS;
  794. change = val1 != emu->chorus_mode;
  795. emu->chorus_mode = val1;
  796. } else {
  797. val1 = ucontrol->value.integer.value[0] % SNDRV_EMU8000_REVERB_NUMBERS;
  798. change = val1 != emu->reverb_mode;
  799. emu->reverb_mode = val1;
  800. }
  801. spin_unlock_irqrestore(&emu->control_lock, flags);
  802. if (change) {
  803. if (kcontrol->private_value)
  804. snd_emu8000_update_chorus_mode(emu);
  805. else
  806. snd_emu8000_update_reverb_mode(emu);
  807. }
  808. return change;
  809. }
  810. static struct snd_kcontrol_new mixer_chorus_mode_control =
  811. {
  812. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  813. .name = "Chorus Mode",
  814. .info = mixer_chorus_reverb_info,
  815. .get = mixer_chorus_reverb_get,
  816. .put = mixer_chorus_reverb_put,
  817. .private_value = 1,
  818. };
  819. static struct snd_kcontrol_new mixer_reverb_mode_control =
  820. {
  821. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  822. .name = "Reverb Mode",
  823. .info = mixer_chorus_reverb_info,
  824. .get = mixer_chorus_reverb_get,
  825. .put = mixer_chorus_reverb_put,
  826. .private_value = 0,
  827. };
  828. /*
  829. * FM OPL3 chorus/reverb depth
  830. */
  831. static int mixer_fm_depth_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  832. {
  833. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  834. uinfo->count = 1;
  835. uinfo->value.integer.min = 0;
  836. uinfo->value.integer.max = 255;
  837. return 0;
  838. }
  839. static int mixer_fm_depth_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  840. {
  841. struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol);
  842. ucontrol->value.integer.value[0] = kcontrol->private_value ? emu->fm_chorus_depth : emu->fm_reverb_depth;
  843. return 0;
  844. }
  845. static int mixer_fm_depth_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  846. {
  847. struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol);
  848. unsigned long flags;
  849. int change;
  850. unsigned short val1;
  851. val1 = ucontrol->value.integer.value[0] % 256;
  852. spin_lock_irqsave(&emu->control_lock, flags);
  853. if (kcontrol->private_value) {
  854. change = val1 != emu->fm_chorus_depth;
  855. emu->fm_chorus_depth = val1;
  856. } else {
  857. change = val1 != emu->fm_reverb_depth;
  858. emu->fm_reverb_depth = val1;
  859. }
  860. spin_unlock_irqrestore(&emu->control_lock, flags);
  861. if (change)
  862. snd_emu8000_init_fm(emu);
  863. return change;
  864. }
  865. static struct snd_kcontrol_new mixer_fm_chorus_depth_control =
  866. {
  867. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  868. .name = "FM Chorus Depth",
  869. .info = mixer_fm_depth_info,
  870. .get = mixer_fm_depth_get,
  871. .put = mixer_fm_depth_put,
  872. .private_value = 1,
  873. };
  874. static struct snd_kcontrol_new mixer_fm_reverb_depth_control =
  875. {
  876. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  877. .name = "FM Reverb Depth",
  878. .info = mixer_fm_depth_info,
  879. .get = mixer_fm_depth_get,
  880. .put = mixer_fm_depth_put,
  881. .private_value = 0,
  882. };
  883. static struct snd_kcontrol_new *mixer_defs[EMU8000_NUM_CONTROLS] = {
  884. &mixer_bass_control,
  885. &mixer_treble_control,
  886. &mixer_chorus_mode_control,
  887. &mixer_reverb_mode_control,
  888. &mixer_fm_chorus_depth_control,
  889. &mixer_fm_reverb_depth_control,
  890. };
  891. /*
  892. * create and attach mixer elements for WaveTable treble/bass controls
  893. */
  894. static int __devinit
  895. snd_emu8000_create_mixer(struct snd_card *card, struct snd_emu8000 *emu)
  896. {
  897. int i, err = 0;
  898. if (snd_BUG_ON(!emu || !card))
  899. return -EINVAL;
  900. spin_lock_init(&emu->control_lock);
  901. memset(emu->controls, 0, sizeof(emu->controls));
  902. for (i = 0; i < EMU8000_NUM_CONTROLS; i++) {
  903. if ((err = snd_ctl_add(card, emu->controls[i] = snd_ctl_new1(mixer_defs[i], emu))) < 0)
  904. goto __error;
  905. }
  906. return 0;
  907. __error:
  908. for (i = 0; i < EMU8000_NUM_CONTROLS; i++) {
  909. down_write(&card->controls_rwsem);
  910. if (emu->controls[i])
  911. snd_ctl_remove(card, emu->controls[i]);
  912. up_write(&card->controls_rwsem);
  913. }
  914. return err;
  915. }
  916. /*
  917. * free resources
  918. */
  919. static int snd_emu8000_free(struct snd_emu8000 *hw)
  920. {
  921. release_and_free_resource(hw->res_port1);
  922. release_and_free_resource(hw->res_port2);
  923. release_and_free_resource(hw->res_port3);
  924. kfree(hw);
  925. return 0;
  926. }
  927. /*
  928. */
  929. static int snd_emu8000_dev_free(struct snd_device *device)
  930. {
  931. struct snd_emu8000 *hw = device->device_data;
  932. return snd_emu8000_free(hw);
  933. }
  934. /*
  935. * initialize and register emu8000 synth device.
  936. */
  937. int __devinit
  938. snd_emu8000_new(struct snd_card *card, int index, long port, int seq_ports,
  939. struct snd_seq_device **awe_ret)
  940. {
  941. struct snd_seq_device *awe;
  942. struct snd_emu8000 *hw;
  943. int err;
  944. static struct snd_device_ops ops = {
  945. .dev_free = snd_emu8000_dev_free,
  946. };
  947. if (awe_ret)
  948. *awe_ret = NULL;
  949. if (seq_ports <= 0)
  950. return 0;
  951. hw = kzalloc(sizeof(*hw), GFP_KERNEL);
  952. if (hw == NULL)
  953. return -ENOMEM;
  954. spin_lock_init(&hw->reg_lock);
  955. hw->index = index;
  956. hw->port1 = port;
  957. hw->port2 = port + 0x400;
  958. hw->port3 = port + 0x800;
  959. if (!(hw->res_port1 = request_region(hw->port1, 4, "Emu8000-1")) ||
  960. !(hw->res_port2 = request_region(hw->port2, 4, "Emu8000-2")) ||
  961. !(hw->res_port3 = request_region(hw->port3, 4, "Emu8000-3"))) {
  962. snd_printk(KERN_ERR "sbawe: can't grab ports 0x%lx, 0x%lx, 0x%lx\n", hw->port1, hw->port2, hw->port3);
  963. snd_emu8000_free(hw);
  964. return -EBUSY;
  965. }
  966. hw->mem_size = 0;
  967. hw->card = card;
  968. hw->seq_ports = seq_ports;
  969. hw->bass_level = 5;
  970. hw->treble_level = 9;
  971. hw->chorus_mode = 2;
  972. hw->reverb_mode = 4;
  973. hw->fm_chorus_depth = 0;
  974. hw->fm_reverb_depth = 0;
  975. if (snd_emu8000_detect(hw) < 0) {
  976. snd_emu8000_free(hw);
  977. return -ENODEV;
  978. }
  979. snd_emu8000_init_hw(hw);
  980. if ((err = snd_emu8000_create_mixer(card, hw)) < 0) {
  981. snd_emu8000_free(hw);
  982. return err;
  983. }
  984. if ((err = snd_device_new(card, SNDRV_DEV_CODEC, hw, &ops)) < 0) {
  985. snd_emu8000_free(hw);
  986. return err;
  987. }
  988. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  989. if (snd_seq_device_new(card, index, SNDRV_SEQ_DEV_ID_EMU8000,
  990. sizeof(struct snd_emu8000*), &awe) >= 0) {
  991. strcpy(awe->name, "EMU-8000");
  992. *(struct snd_emu8000 **)SNDRV_SEQ_DEVICE_ARGPTR(awe) = hw;
  993. }
  994. #else
  995. awe = NULL;
  996. #endif
  997. if (awe_ret)
  998. *awe_ret = awe;
  999. return 0;
  1000. }
  1001. /*
  1002. * exported stuff
  1003. */
  1004. EXPORT_SYMBOL(snd_emu8000_poke);
  1005. EXPORT_SYMBOL(snd_emu8000_peek);
  1006. EXPORT_SYMBOL(snd_emu8000_poke_dw);
  1007. EXPORT_SYMBOL(snd_emu8000_peek_dw);
  1008. EXPORT_SYMBOL(snd_emu8000_dma_chan);
  1009. EXPORT_SYMBOL(snd_emu8000_init_fm);
  1010. EXPORT_SYMBOL(snd_emu8000_load_chorus_fx);
  1011. EXPORT_SYMBOL(snd_emu8000_load_reverb_fx);
  1012. EXPORT_SYMBOL(snd_emu8000_update_chorus_mode);
  1013. EXPORT_SYMBOL(snd_emu8000_update_reverb_mode);
  1014. EXPORT_SYMBOL(snd_emu8000_update_equalizer);