sonicvibes.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /*
  2. * Driver for S3 SonicVibes soundcard
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  4. *
  5. * BUGS:
  6. * It looks like 86c617 rev 3 doesn't supports DDMA buffers above 16MB?
  7. * Driver sometimes hangs... Nobody knows why at this moment...
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <sound/driver.h>
  25. #include <linux/delay.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/pci.h>
  29. #include <linux/slab.h>
  30. #include <linux/gameport.h>
  31. #include <linux/moduleparam.h>
  32. #include <sound/core.h>
  33. #include <sound/pcm.h>
  34. #include <sound/info.h>
  35. #include <sound/control.h>
  36. #include <sound/mpu401.h>
  37. #include <sound/opl3.h>
  38. #include <sound/initval.h>
  39. #include <asm/io.h>
  40. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  41. MODULE_DESCRIPTION("S3 SonicVibes PCI");
  42. MODULE_LICENSE("GPL");
  43. MODULE_SUPPORTED_DEVICE("{{S3,SonicVibes PCI}}");
  44. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  45. #define SUPPORT_JOYSTICK 1
  46. #endif
  47. #ifndef PCI_VENDOR_ID_S3
  48. #define PCI_VENDOR_ID_S3 0x5333
  49. #endif
  50. #ifndef PCI_DEVICE_ID_S3_SONICVIBES
  51. #define PCI_DEVICE_ID_S3_SONICVIBES 0xca00
  52. #endif
  53. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  54. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  55. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  56. static int reverb[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
  57. static int mge[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
  58. static unsigned int dmaio = 0x7a00; /* DDMA i/o address */
  59. module_param_array(index, int, NULL, 0444);
  60. MODULE_PARM_DESC(index, "Index value for S3 SonicVibes soundcard.");
  61. module_param_array(id, charp, NULL, 0444);
  62. MODULE_PARM_DESC(id, "ID string for S3 SonicVibes soundcard.");
  63. module_param_array(enable, bool, NULL, 0444);
  64. MODULE_PARM_DESC(enable, "Enable S3 SonicVibes soundcard.");
  65. module_param_array(reverb, bool, NULL, 0444);
  66. MODULE_PARM_DESC(reverb, "Enable reverb (SRAM is present) for S3 SonicVibes soundcard.");
  67. module_param_array(mge, bool, NULL, 0444);
  68. MODULE_PARM_DESC(mge, "MIC Gain Enable for S3 SonicVibes soundcard.");
  69. module_param(dmaio, uint, 0444);
  70. MODULE_PARM_DESC(dmaio, "DDMA i/o base address for S3 SonicVibes soundcard.");
  71. /*
  72. * Enhanced port direct registers
  73. */
  74. #define SV_REG(sonic, x) ((sonic)->enh_port + SV_REG_##x)
  75. #define SV_REG_CONTROL 0x00 /* R/W: CODEC/Mixer control register */
  76. #define SV_ENHANCED 0x01 /* audio mode select - enhanced mode */
  77. #define SV_TEST 0x02 /* test bit */
  78. #define SV_REVERB 0x04 /* reverb enable */
  79. #define SV_WAVETABLE 0x08 /* wavetable active / FM active if not set */
  80. #define SV_INTA 0x20 /* INTA driving - should be always 1 */
  81. #define SV_RESET 0x80 /* reset chip */
  82. #define SV_REG_IRQMASK 0x01 /* R/W: CODEC/Mixer interrupt mask register */
  83. #define SV_DMAA_MASK 0x01 /* mask DMA-A interrupt */
  84. #define SV_DMAC_MASK 0x04 /* mask DMA-C interrupt */
  85. #define SV_SPEC_MASK 0x08 /* special interrupt mask - should be always masked */
  86. #define SV_UD_MASK 0x40 /* Up/Down button interrupt mask */
  87. #define SV_MIDI_MASK 0x80 /* mask MIDI interrupt */
  88. #define SV_REG_STATUS 0x02 /* R/O: CODEC/Mixer status register */
  89. #define SV_DMAA_IRQ 0x01 /* DMA-A interrupt */
  90. #define SV_DMAC_IRQ 0x04 /* DMA-C interrupt */
  91. #define SV_SPEC_IRQ 0x08 /* special interrupt */
  92. #define SV_UD_IRQ 0x40 /* Up/Down interrupt */
  93. #define SV_MIDI_IRQ 0x80 /* MIDI interrupt */
  94. #define SV_REG_INDEX 0x04 /* R/W: CODEC/Mixer index address register */
  95. #define SV_MCE 0x40 /* mode change enable */
  96. #define SV_TRD 0x80 /* DMA transfer request disabled */
  97. #define SV_REG_DATA 0x05 /* R/W: CODEC/Mixer index data register */
  98. /*
  99. * Enhanced port indirect registers
  100. */
  101. #define SV_IREG_LEFT_ADC 0x00 /* Left ADC Input Control */
  102. #define SV_IREG_RIGHT_ADC 0x01 /* Right ADC Input Control */
  103. #define SV_IREG_LEFT_AUX1 0x02 /* Left AUX1 Input Control */
  104. #define SV_IREG_RIGHT_AUX1 0x03 /* Right AUX1 Input Control */
  105. #define SV_IREG_LEFT_CD 0x04 /* Left CD Input Control */
  106. #define SV_IREG_RIGHT_CD 0x05 /* Right CD Input Control */
  107. #define SV_IREG_LEFT_LINE 0x06 /* Left Line Input Control */
  108. #define SV_IREG_RIGHT_LINE 0x07 /* Right Line Input Control */
  109. #define SV_IREG_MIC 0x08 /* MIC Input Control */
  110. #define SV_IREG_GAME_PORT 0x09 /* Game Port Control */
  111. #define SV_IREG_LEFT_SYNTH 0x0a /* Left Synth Input Control */
  112. #define SV_IREG_RIGHT_SYNTH 0x0b /* Right Synth Input Control */
  113. #define SV_IREG_LEFT_AUX2 0x0c /* Left AUX2 Input Control */
  114. #define SV_IREG_RIGHT_AUX2 0x0d /* Right AUX2 Input Control */
  115. #define SV_IREG_LEFT_ANALOG 0x0e /* Left Analog Mixer Output Control */
  116. #define SV_IREG_RIGHT_ANALOG 0x0f /* Right Analog Mixer Output Control */
  117. #define SV_IREG_LEFT_PCM 0x10 /* Left PCM Input Control */
  118. #define SV_IREG_RIGHT_PCM 0x11 /* Right PCM Input Control */
  119. #define SV_IREG_DMA_DATA_FMT 0x12 /* DMA Data Format */
  120. #define SV_IREG_PC_ENABLE 0x13 /* Playback/Capture Enable Register */
  121. #define SV_IREG_UD_BUTTON 0x14 /* Up/Down Button Register */
  122. #define SV_IREG_REVISION 0x15 /* Revision */
  123. #define SV_IREG_ADC_OUTPUT_CTRL 0x16 /* ADC Output Control */
  124. #define SV_IREG_DMA_A_UPPER 0x18 /* DMA A Upper Base Count */
  125. #define SV_IREG_DMA_A_LOWER 0x19 /* DMA A Lower Base Count */
  126. #define SV_IREG_DMA_C_UPPER 0x1c /* DMA C Upper Base Count */
  127. #define SV_IREG_DMA_C_LOWER 0x1d /* DMA C Lower Base Count */
  128. #define SV_IREG_PCM_RATE_LOW 0x1e /* PCM Sampling Rate Low Byte */
  129. #define SV_IREG_PCM_RATE_HIGH 0x1f /* PCM Sampling Rate High Byte */
  130. #define SV_IREG_SYNTH_RATE_LOW 0x20 /* Synthesizer Sampling Rate Low Byte */
  131. #define SV_IREG_SYNTH_RATE_HIGH 0x21 /* Synthesizer Sampling Rate High Byte */
  132. #define SV_IREG_ADC_CLOCK 0x22 /* ADC Clock Source Selection */
  133. #define SV_IREG_ADC_ALT_RATE 0x23 /* ADC Alternative Sampling Rate Selection */
  134. #define SV_IREG_ADC_PLL_M 0x24 /* ADC PLL M Register */
  135. #define SV_IREG_ADC_PLL_N 0x25 /* ADC PLL N Register */
  136. #define SV_IREG_SYNTH_PLL_M 0x26 /* Synthesizer PLL M Register */
  137. #define SV_IREG_SYNTH_PLL_N 0x27 /* Synthesizer PLL N Register */
  138. #define SV_IREG_MPU401 0x2a /* MPU-401 UART Operation */
  139. #define SV_IREG_DRIVE_CTRL 0x2b /* Drive Control */
  140. #define SV_IREG_SRS_SPACE 0x2c /* SRS Space Control */
  141. #define SV_IREG_SRS_CENTER 0x2d /* SRS Center Control */
  142. #define SV_IREG_WAVE_SOURCE 0x2e /* Wavetable Sample Source Select */
  143. #define SV_IREG_ANALOG_POWER 0x30 /* Analog Power Down Control */
  144. #define SV_IREG_DIGITAL_POWER 0x31 /* Digital Power Down Control */
  145. #define SV_IREG_ADC_PLL SV_IREG_ADC_PLL_M
  146. #define SV_IREG_SYNTH_PLL SV_IREG_SYNTH_PLL_M
  147. /*
  148. * DMA registers
  149. */
  150. #define SV_DMA_ADDR0 0x00
  151. #define SV_DMA_ADDR1 0x01
  152. #define SV_DMA_ADDR2 0x02
  153. #define SV_DMA_ADDR3 0x03
  154. #define SV_DMA_COUNT0 0x04
  155. #define SV_DMA_COUNT1 0x05
  156. #define SV_DMA_COUNT2 0x06
  157. #define SV_DMA_MODE 0x0b
  158. #define SV_DMA_RESET 0x0d
  159. #define SV_DMA_MASK 0x0f
  160. /*
  161. * Record sources
  162. */
  163. #define SV_RECSRC_RESERVED (0x00<<5)
  164. #define SV_RECSRC_CD (0x01<<5)
  165. #define SV_RECSRC_DAC (0x02<<5)
  166. #define SV_RECSRC_AUX2 (0x03<<5)
  167. #define SV_RECSRC_LINE (0x04<<5)
  168. #define SV_RECSRC_AUX1 (0x05<<5)
  169. #define SV_RECSRC_MIC (0x06<<5)
  170. #define SV_RECSRC_OUT (0x07<<5)
  171. /*
  172. * constants
  173. */
  174. #define SV_FULLRATE 48000
  175. #define SV_REFFREQUENCY 24576000
  176. #define SV_ADCMULT 512
  177. #define SV_MODE_PLAY 1
  178. #define SV_MODE_CAPTURE 2
  179. /*
  180. */
  181. typedef struct _snd_sonicvibes sonicvibes_t;
  182. struct _snd_sonicvibes {
  183. unsigned long dma1size;
  184. unsigned long dma2size;
  185. int irq;
  186. unsigned long sb_port;
  187. unsigned long enh_port;
  188. unsigned long synth_port;
  189. unsigned long midi_port;
  190. unsigned long game_port;
  191. unsigned int dmaa_port;
  192. struct resource *res_dmaa;
  193. unsigned int dmac_port;
  194. struct resource *res_dmac;
  195. unsigned char enable;
  196. unsigned char irqmask;
  197. unsigned char revision;
  198. unsigned char format;
  199. unsigned char srs_space;
  200. unsigned char srs_center;
  201. unsigned char mpu_switch;
  202. unsigned char wave_source;
  203. unsigned int mode;
  204. struct pci_dev *pci;
  205. snd_card_t *card;
  206. snd_pcm_t *pcm;
  207. snd_pcm_substream_t *playback_substream;
  208. snd_pcm_substream_t *capture_substream;
  209. snd_rawmidi_t *rmidi;
  210. snd_hwdep_t *fmsynth; /* S3FM */
  211. spinlock_t reg_lock;
  212. unsigned int p_dma_size;
  213. unsigned int c_dma_size;
  214. snd_kcontrol_t *master_mute;
  215. snd_kcontrol_t *master_volume;
  216. #ifdef SUPPORT_JOYSTICK
  217. struct gameport *gameport;
  218. #endif
  219. };
  220. static struct pci_device_id snd_sonic_ids[] = {
  221. { 0x5333, 0xca00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  222. { 0, }
  223. };
  224. MODULE_DEVICE_TABLE(pci, snd_sonic_ids);
  225. static ratden_t sonicvibes_adc_clock = {
  226. .num_min = 4000 * 65536,
  227. .num_max = 48000UL * 65536,
  228. .num_step = 1,
  229. .den = 65536,
  230. };
  231. static snd_pcm_hw_constraint_ratdens_t snd_sonicvibes_hw_constraints_adc_clock = {
  232. .nrats = 1,
  233. .rats = &sonicvibes_adc_clock,
  234. };
  235. /*
  236. * common I/O routines
  237. */
  238. static inline void snd_sonicvibes_setdmaa(sonicvibes_t * sonic,
  239. unsigned int addr,
  240. unsigned int count)
  241. {
  242. count--;
  243. outl(addr, sonic->dmaa_port + SV_DMA_ADDR0);
  244. outl(count, sonic->dmaa_port + SV_DMA_COUNT0);
  245. outb(0x18, sonic->dmaa_port + SV_DMA_MODE);
  246. #if 0
  247. printk("program dmaa: addr = 0x%x, paddr = 0x%x\n", addr, inl(sonic->dmaa_port + SV_DMA_ADDR0));
  248. #endif
  249. }
  250. static inline void snd_sonicvibes_setdmac(sonicvibes_t * sonic,
  251. unsigned int addr,
  252. unsigned int count)
  253. {
  254. /* note: dmac is working in word mode!!! */
  255. count >>= 1;
  256. count--;
  257. outl(addr, sonic->dmac_port + SV_DMA_ADDR0);
  258. outl(count, sonic->dmac_port + SV_DMA_COUNT0);
  259. outb(0x14, sonic->dmac_port + SV_DMA_MODE);
  260. #if 0
  261. printk("program dmac: addr = 0x%x, paddr = 0x%x\n", addr, inl(sonic->dmac_port + SV_DMA_ADDR0));
  262. #endif
  263. }
  264. static inline unsigned int snd_sonicvibes_getdmaa(sonicvibes_t * sonic)
  265. {
  266. return (inl(sonic->dmaa_port + SV_DMA_COUNT0) & 0xffffff) + 1;
  267. }
  268. static inline unsigned int snd_sonicvibes_getdmac(sonicvibes_t * sonic)
  269. {
  270. /* note: dmac is working in word mode!!! */
  271. return ((inl(sonic->dmac_port + SV_DMA_COUNT0) & 0xffffff) + 1) << 1;
  272. }
  273. static void snd_sonicvibes_out1(sonicvibes_t * sonic,
  274. unsigned char reg,
  275. unsigned char value)
  276. {
  277. outb(reg, SV_REG(sonic, INDEX));
  278. udelay(10);
  279. outb(value, SV_REG(sonic, DATA));
  280. udelay(10);
  281. }
  282. static void snd_sonicvibes_out(sonicvibes_t * sonic,
  283. unsigned char reg,
  284. unsigned char value)
  285. {
  286. unsigned long flags;
  287. spin_lock_irqsave(&sonic->reg_lock, flags);
  288. outb(reg, SV_REG(sonic, INDEX));
  289. udelay(10);
  290. outb(value, SV_REG(sonic, DATA));
  291. udelay(10);
  292. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  293. }
  294. static unsigned char snd_sonicvibes_in1(sonicvibes_t * sonic, unsigned char reg)
  295. {
  296. unsigned char value;
  297. outb(reg, SV_REG(sonic, INDEX));
  298. udelay(10);
  299. value = inb(SV_REG(sonic, DATA));
  300. udelay(10);
  301. return value;
  302. }
  303. static unsigned char snd_sonicvibes_in(sonicvibes_t * sonic, unsigned char reg)
  304. {
  305. unsigned long flags;
  306. unsigned char value;
  307. spin_lock_irqsave(&sonic->reg_lock, flags);
  308. outb(reg, SV_REG(sonic, INDEX));
  309. udelay(10);
  310. value = inb(SV_REG(sonic, DATA));
  311. udelay(10);
  312. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  313. return value;
  314. }
  315. #if 0
  316. static void snd_sonicvibes_debug(sonicvibes_t * sonic)
  317. {
  318. printk("SV REGS: INDEX = 0x%02x ", inb(SV_REG(sonic, INDEX)));
  319. printk(" STATUS = 0x%02x\n", inb(SV_REG(sonic, STATUS)));
  320. printk(" 0x00: left input = 0x%02x ", snd_sonicvibes_in(sonic, 0x00));
  321. printk(" 0x20: synth rate low = 0x%02x\n", snd_sonicvibes_in(sonic, 0x20));
  322. printk(" 0x01: right input = 0x%02x ", snd_sonicvibes_in(sonic, 0x01));
  323. printk(" 0x21: synth rate high = 0x%02x\n", snd_sonicvibes_in(sonic, 0x21));
  324. printk(" 0x02: left AUX1 = 0x%02x ", snd_sonicvibes_in(sonic, 0x02));
  325. printk(" 0x22: ADC clock = 0x%02x\n", snd_sonicvibes_in(sonic, 0x22));
  326. printk(" 0x03: right AUX1 = 0x%02x ", snd_sonicvibes_in(sonic, 0x03));
  327. printk(" 0x23: ADC alt rate = 0x%02x\n", snd_sonicvibes_in(sonic, 0x23));
  328. printk(" 0x04: left CD = 0x%02x ", snd_sonicvibes_in(sonic, 0x04));
  329. printk(" 0x24: ADC pll M = 0x%02x\n", snd_sonicvibes_in(sonic, 0x24));
  330. printk(" 0x05: right CD = 0x%02x ", snd_sonicvibes_in(sonic, 0x05));
  331. printk(" 0x25: ADC pll N = 0x%02x\n", snd_sonicvibes_in(sonic, 0x25));
  332. printk(" 0x06: left line = 0x%02x ", snd_sonicvibes_in(sonic, 0x06));
  333. printk(" 0x26: Synth pll M = 0x%02x\n", snd_sonicvibes_in(sonic, 0x26));
  334. printk(" 0x07: right line = 0x%02x ", snd_sonicvibes_in(sonic, 0x07));
  335. printk(" 0x27: Synth pll N = 0x%02x\n", snd_sonicvibes_in(sonic, 0x27));
  336. printk(" 0x08: MIC = 0x%02x ", snd_sonicvibes_in(sonic, 0x08));
  337. printk(" 0x28: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x28));
  338. printk(" 0x09: Game port = 0x%02x ", snd_sonicvibes_in(sonic, 0x09));
  339. printk(" 0x29: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x29));
  340. printk(" 0x0a: left synth = 0x%02x ", snd_sonicvibes_in(sonic, 0x0a));
  341. printk(" 0x2a: MPU401 = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2a));
  342. printk(" 0x0b: right synth = 0x%02x ", snd_sonicvibes_in(sonic, 0x0b));
  343. printk(" 0x2b: drive ctrl = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2b));
  344. printk(" 0x0c: left AUX2 = 0x%02x ", snd_sonicvibes_in(sonic, 0x0c));
  345. printk(" 0x2c: SRS space = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2c));
  346. printk(" 0x0d: right AUX2 = 0x%02x ", snd_sonicvibes_in(sonic, 0x0d));
  347. printk(" 0x2d: SRS center = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2d));
  348. printk(" 0x0e: left analog = 0x%02x ", snd_sonicvibes_in(sonic, 0x0e));
  349. printk(" 0x2e: wave source = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2e));
  350. printk(" 0x0f: right analog = 0x%02x ", snd_sonicvibes_in(sonic, 0x0f));
  351. printk(" 0x2f: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2f));
  352. printk(" 0x10: left PCM = 0x%02x ", snd_sonicvibes_in(sonic, 0x10));
  353. printk(" 0x30: analog power = 0x%02x\n", snd_sonicvibes_in(sonic, 0x30));
  354. printk(" 0x11: right PCM = 0x%02x ", snd_sonicvibes_in(sonic, 0x11));
  355. printk(" 0x31: analog power = 0x%02x\n", snd_sonicvibes_in(sonic, 0x31));
  356. printk(" 0x12: DMA data format = 0x%02x ", snd_sonicvibes_in(sonic, 0x12));
  357. printk(" 0x32: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x32));
  358. printk(" 0x13: P/C enable = 0x%02x ", snd_sonicvibes_in(sonic, 0x13));
  359. printk(" 0x33: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x33));
  360. printk(" 0x14: U/D button = 0x%02x ", snd_sonicvibes_in(sonic, 0x14));
  361. printk(" 0x34: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x34));
  362. printk(" 0x15: revision = 0x%02x ", snd_sonicvibes_in(sonic, 0x15));
  363. printk(" 0x35: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x35));
  364. printk(" 0x16: ADC output ctrl = 0x%02x ", snd_sonicvibes_in(sonic, 0x16));
  365. printk(" 0x36: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x36));
  366. printk(" 0x17: --- = 0x%02x ", snd_sonicvibes_in(sonic, 0x17));
  367. printk(" 0x37: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x37));
  368. printk(" 0x18: DMA A upper cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x18));
  369. printk(" 0x38: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x38));
  370. printk(" 0x19: DMA A lower cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x19));
  371. printk(" 0x39: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x39));
  372. printk(" 0x1a: --- = 0x%02x ", snd_sonicvibes_in(sonic, 0x1a));
  373. printk(" 0x3a: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3a));
  374. printk(" 0x1b: --- = 0x%02x ", snd_sonicvibes_in(sonic, 0x1b));
  375. printk(" 0x3b: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3b));
  376. printk(" 0x1c: DMA C upper cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x1c));
  377. printk(" 0x3c: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3c));
  378. printk(" 0x1d: DMA C upper cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x1d));
  379. printk(" 0x3d: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3d));
  380. printk(" 0x1e: PCM rate low = 0x%02x ", snd_sonicvibes_in(sonic, 0x1e));
  381. printk(" 0x3e: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3e));
  382. printk(" 0x1f: PCM rate high = 0x%02x ", snd_sonicvibes_in(sonic, 0x1f));
  383. printk(" 0x3f: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3f));
  384. }
  385. #endif
  386. static void snd_sonicvibes_setfmt(sonicvibes_t * sonic,
  387. unsigned char mask,
  388. unsigned char value)
  389. {
  390. unsigned long flags;
  391. spin_lock_irqsave(&sonic->reg_lock, flags);
  392. outb(SV_MCE | SV_IREG_DMA_DATA_FMT, SV_REG(sonic, INDEX));
  393. if (mask) {
  394. sonic->format = inb(SV_REG(sonic, DATA));
  395. udelay(10);
  396. }
  397. sonic->format = (sonic->format & mask) | value;
  398. outb(sonic->format, SV_REG(sonic, DATA));
  399. udelay(10);
  400. outb(0, SV_REG(sonic, INDEX));
  401. udelay(10);
  402. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  403. }
  404. static void snd_sonicvibes_pll(unsigned int rate,
  405. unsigned int *res_r,
  406. unsigned int *res_m,
  407. unsigned int *res_n)
  408. {
  409. unsigned int r, m = 0, n = 0;
  410. unsigned int xm, xn, xr, xd, metric = ~0U;
  411. if (rate < 625000 / SV_ADCMULT)
  412. rate = 625000 / SV_ADCMULT;
  413. if (rate > 150000000 / SV_ADCMULT)
  414. rate = 150000000 / SV_ADCMULT;
  415. /* slight violation of specs, needed for continuous sampling rates */
  416. for (r = 0; rate < 75000000 / SV_ADCMULT; r += 0x20, rate <<= 1);
  417. for (xn = 3; xn < 33; xn++) /* 35 */
  418. for (xm = 3; xm < 257; xm++) {
  419. xr = ((SV_REFFREQUENCY / SV_ADCMULT) * xm) / xn;
  420. if (xr >= rate)
  421. xd = xr - rate;
  422. else
  423. xd = rate - xr;
  424. if (xd < metric) {
  425. metric = xd;
  426. m = xm - 2;
  427. n = xn - 2;
  428. }
  429. }
  430. *res_r = r;
  431. *res_m = m;
  432. *res_n = n;
  433. #if 0
  434. printk("metric = %i, xm = %i, xn = %i\n", metric, xm, xn);
  435. printk("pll: m = 0x%x, r = 0x%x, n = 0x%x\n", reg, m, r, n);
  436. #endif
  437. }
  438. static void snd_sonicvibes_setpll(sonicvibes_t * sonic,
  439. unsigned char reg,
  440. unsigned int rate)
  441. {
  442. unsigned long flags;
  443. unsigned int r, m, n;
  444. snd_sonicvibes_pll(rate, &r, &m, &n);
  445. if (sonic != NULL) {
  446. spin_lock_irqsave(&sonic->reg_lock, flags);
  447. snd_sonicvibes_out1(sonic, reg, m);
  448. snd_sonicvibes_out1(sonic, reg + 1, r | n);
  449. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  450. }
  451. }
  452. static void snd_sonicvibes_set_adc_rate(sonicvibes_t * sonic, unsigned int rate)
  453. {
  454. unsigned long flags;
  455. unsigned int div;
  456. unsigned char clock;
  457. div = 48000 / rate;
  458. if (div > 8)
  459. div = 8;
  460. if ((48000 / div) == rate) { /* use the alternate clock */
  461. clock = 0x10;
  462. } else { /* use the PLL source */
  463. clock = 0x00;
  464. snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, rate);
  465. }
  466. spin_lock_irqsave(&sonic->reg_lock, flags);
  467. snd_sonicvibes_out1(sonic, SV_IREG_ADC_ALT_RATE, (div - 1) << 4);
  468. snd_sonicvibes_out1(sonic, SV_IREG_ADC_CLOCK, clock);
  469. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  470. }
  471. static int snd_sonicvibes_hw_constraint_dac_rate(snd_pcm_hw_params_t *params,
  472. snd_pcm_hw_rule_t *rule)
  473. {
  474. unsigned int rate, div, r, m, n;
  475. if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min ==
  476. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max) {
  477. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min;
  478. div = 48000 / rate;
  479. if (div > 8)
  480. div = 8;
  481. if ((48000 / div) == rate) {
  482. params->rate_num = rate;
  483. params->rate_den = 1;
  484. } else {
  485. snd_sonicvibes_pll(rate, &r, &m, &n);
  486. snd_assert((SV_REFFREQUENCY % 16) == 0, return -EINVAL);
  487. snd_assert((SV_ADCMULT % 512) == 0, return -EINVAL);
  488. params->rate_num = (SV_REFFREQUENCY/16) * (n+2) * r;
  489. params->rate_den = (SV_ADCMULT/512) * (m+2);
  490. }
  491. }
  492. return 0;
  493. }
  494. static void snd_sonicvibes_set_dac_rate(sonicvibes_t * sonic, unsigned int rate)
  495. {
  496. unsigned int div;
  497. unsigned long flags;
  498. div = (rate * 65536 + SV_FULLRATE / 2) / SV_FULLRATE;
  499. if (div > 65535)
  500. div = 65535;
  501. spin_lock_irqsave(&sonic->reg_lock, flags);
  502. snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_HIGH, div >> 8);
  503. snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_LOW, div);
  504. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  505. }
  506. static int snd_sonicvibes_trigger(sonicvibes_t * sonic, int what, int cmd)
  507. {
  508. int result = 0;
  509. spin_lock(&sonic->reg_lock);
  510. if (cmd == SNDRV_PCM_TRIGGER_START) {
  511. if (!(sonic->enable & what)) {
  512. sonic->enable |= what;
  513. snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
  514. }
  515. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  516. if (sonic->enable & what) {
  517. sonic->enable &= ~what;
  518. snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
  519. }
  520. } else {
  521. result = -EINVAL;
  522. }
  523. spin_unlock(&sonic->reg_lock);
  524. return result;
  525. }
  526. static irqreturn_t snd_sonicvibes_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  527. {
  528. sonicvibes_t *sonic = dev_id;
  529. unsigned char status;
  530. status = inb(SV_REG(sonic, STATUS));
  531. if (!(status & (SV_DMAA_IRQ | SV_DMAC_IRQ | SV_MIDI_IRQ)))
  532. return IRQ_NONE;
  533. if (status == 0xff) { /* failure */
  534. outb(sonic->irqmask = ~0, SV_REG(sonic, IRQMASK));
  535. snd_printk("IRQ failure - interrupts disabled!!\n");
  536. return IRQ_HANDLED;
  537. }
  538. if (sonic->pcm) {
  539. if (status & SV_DMAA_IRQ)
  540. snd_pcm_period_elapsed(sonic->playback_substream);
  541. if (status & SV_DMAC_IRQ)
  542. snd_pcm_period_elapsed(sonic->capture_substream);
  543. }
  544. if (sonic->rmidi) {
  545. if (status & SV_MIDI_IRQ)
  546. snd_mpu401_uart_interrupt(irq, sonic->rmidi->private_data, regs);
  547. }
  548. if (status & SV_UD_IRQ) {
  549. unsigned char udreg;
  550. int vol, oleft, oright, mleft, mright;
  551. spin_lock(&sonic->reg_lock);
  552. udreg = snd_sonicvibes_in1(sonic, SV_IREG_UD_BUTTON);
  553. vol = udreg & 0x3f;
  554. if (!(udreg & 0x40))
  555. vol = -vol;
  556. oleft = mleft = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ANALOG);
  557. oright = mright = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ANALOG);
  558. oleft &= 0x1f;
  559. oright &= 0x1f;
  560. oleft += vol;
  561. if (oleft < 0)
  562. oleft = 0;
  563. if (oleft > 0x1f)
  564. oleft = 0x1f;
  565. oright += vol;
  566. if (oright < 0)
  567. oright = 0;
  568. if (oright > 0x1f)
  569. oright = 0x1f;
  570. if (udreg & 0x80) {
  571. mleft ^= 0x80;
  572. mright ^= 0x80;
  573. }
  574. oleft |= mleft & 0x80;
  575. oright |= mright & 0x80;
  576. snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ANALOG, oleft);
  577. snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ANALOG, oright);
  578. spin_unlock(&sonic->reg_lock);
  579. snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_mute->id);
  580. snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_volume->id);
  581. }
  582. return IRQ_HANDLED;
  583. }
  584. /*
  585. * PCM part
  586. */
  587. static int snd_sonicvibes_playback_trigger(snd_pcm_substream_t * substream,
  588. int cmd)
  589. {
  590. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  591. return snd_sonicvibes_trigger(sonic, 1, cmd);
  592. }
  593. static int snd_sonicvibes_capture_trigger(snd_pcm_substream_t * substream,
  594. int cmd)
  595. {
  596. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  597. return snd_sonicvibes_trigger(sonic, 2, cmd);
  598. }
  599. static int snd_sonicvibes_hw_params(snd_pcm_substream_t * substream,
  600. snd_pcm_hw_params_t * hw_params)
  601. {
  602. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  603. }
  604. static int snd_sonicvibes_hw_free(snd_pcm_substream_t * substream)
  605. {
  606. return snd_pcm_lib_free_pages(substream);
  607. }
  608. static int snd_sonicvibes_playback_prepare(snd_pcm_substream_t * substream)
  609. {
  610. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  611. snd_pcm_runtime_t *runtime = substream->runtime;
  612. unsigned char fmt = 0;
  613. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  614. unsigned int count = snd_pcm_lib_period_bytes(substream);
  615. sonic->p_dma_size = size;
  616. count--;
  617. if (runtime->channels > 1)
  618. fmt |= 1;
  619. if (snd_pcm_format_width(runtime->format) == 16)
  620. fmt |= 2;
  621. snd_sonicvibes_setfmt(sonic, ~3, fmt);
  622. snd_sonicvibes_set_dac_rate(sonic, runtime->rate);
  623. spin_lock_irq(&sonic->reg_lock);
  624. snd_sonicvibes_setdmaa(sonic, runtime->dma_addr, size);
  625. snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_UPPER, count >> 8);
  626. snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_LOWER, count);
  627. spin_unlock_irq(&sonic->reg_lock);
  628. return 0;
  629. }
  630. static int snd_sonicvibes_capture_prepare(snd_pcm_substream_t * substream)
  631. {
  632. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  633. snd_pcm_runtime_t *runtime = substream->runtime;
  634. unsigned char fmt = 0;
  635. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  636. unsigned int count = snd_pcm_lib_period_bytes(substream);
  637. sonic->c_dma_size = size;
  638. count >>= 1;
  639. count--;
  640. if (runtime->channels > 1)
  641. fmt |= 0x10;
  642. if (snd_pcm_format_width(runtime->format) == 16)
  643. fmt |= 0x20;
  644. snd_sonicvibes_setfmt(sonic, ~0x30, fmt);
  645. snd_sonicvibes_set_adc_rate(sonic, runtime->rate);
  646. spin_lock_irq(&sonic->reg_lock);
  647. snd_sonicvibes_setdmac(sonic, runtime->dma_addr, size);
  648. snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_UPPER, count >> 8);
  649. snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_LOWER, count);
  650. spin_unlock_irq(&sonic->reg_lock);
  651. return 0;
  652. }
  653. static snd_pcm_uframes_t snd_sonicvibes_playback_pointer(snd_pcm_substream_t * substream)
  654. {
  655. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  656. size_t ptr;
  657. if (!(sonic->enable & 1))
  658. return 0;
  659. ptr = sonic->p_dma_size - snd_sonicvibes_getdmaa(sonic);
  660. return bytes_to_frames(substream->runtime, ptr);
  661. }
  662. static snd_pcm_uframes_t snd_sonicvibes_capture_pointer(snd_pcm_substream_t * substream)
  663. {
  664. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  665. size_t ptr;
  666. if (!(sonic->enable & 2))
  667. return 0;
  668. ptr = sonic->c_dma_size - snd_sonicvibes_getdmac(sonic);
  669. return bytes_to_frames(substream->runtime, ptr);
  670. }
  671. static snd_pcm_hardware_t snd_sonicvibes_playback =
  672. {
  673. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  674. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  675. SNDRV_PCM_INFO_MMAP_VALID),
  676. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  677. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  678. .rate_min = 4000,
  679. .rate_max = 48000,
  680. .channels_min = 1,
  681. .channels_max = 2,
  682. .buffer_bytes_max = (128*1024),
  683. .period_bytes_min = 32,
  684. .period_bytes_max = (128*1024),
  685. .periods_min = 1,
  686. .periods_max = 1024,
  687. .fifo_size = 0,
  688. };
  689. static snd_pcm_hardware_t snd_sonicvibes_capture =
  690. {
  691. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  692. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  693. SNDRV_PCM_INFO_MMAP_VALID),
  694. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  695. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  696. .rate_min = 4000,
  697. .rate_max = 48000,
  698. .channels_min = 1,
  699. .channels_max = 2,
  700. .buffer_bytes_max = (128*1024),
  701. .period_bytes_min = 32,
  702. .period_bytes_max = (128*1024),
  703. .periods_min = 1,
  704. .periods_max = 1024,
  705. .fifo_size = 0,
  706. };
  707. static int snd_sonicvibes_playback_open(snd_pcm_substream_t * substream)
  708. {
  709. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  710. snd_pcm_runtime_t *runtime = substream->runtime;
  711. sonic->mode |= SV_MODE_PLAY;
  712. sonic->playback_substream = substream;
  713. runtime->hw = snd_sonicvibes_playback;
  714. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, snd_sonicvibes_hw_constraint_dac_rate, NULL, SNDRV_PCM_HW_PARAM_RATE, -1);
  715. return 0;
  716. }
  717. static int snd_sonicvibes_capture_open(snd_pcm_substream_t * substream)
  718. {
  719. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  720. snd_pcm_runtime_t *runtime = substream->runtime;
  721. sonic->mode |= SV_MODE_CAPTURE;
  722. sonic->capture_substream = substream;
  723. runtime->hw = snd_sonicvibes_capture;
  724. snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  725. &snd_sonicvibes_hw_constraints_adc_clock);
  726. return 0;
  727. }
  728. static int snd_sonicvibes_playback_close(snd_pcm_substream_t * substream)
  729. {
  730. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  731. sonic->playback_substream = NULL;
  732. sonic->mode &= ~SV_MODE_PLAY;
  733. return 0;
  734. }
  735. static int snd_sonicvibes_capture_close(snd_pcm_substream_t * substream)
  736. {
  737. sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
  738. sonic->capture_substream = NULL;
  739. sonic->mode &= ~SV_MODE_CAPTURE;
  740. return 0;
  741. }
  742. static snd_pcm_ops_t snd_sonicvibes_playback_ops = {
  743. .open = snd_sonicvibes_playback_open,
  744. .close = snd_sonicvibes_playback_close,
  745. .ioctl = snd_pcm_lib_ioctl,
  746. .hw_params = snd_sonicvibes_hw_params,
  747. .hw_free = snd_sonicvibes_hw_free,
  748. .prepare = snd_sonicvibes_playback_prepare,
  749. .trigger = snd_sonicvibes_playback_trigger,
  750. .pointer = snd_sonicvibes_playback_pointer,
  751. };
  752. static snd_pcm_ops_t snd_sonicvibes_capture_ops = {
  753. .open = snd_sonicvibes_capture_open,
  754. .close = snd_sonicvibes_capture_close,
  755. .ioctl = snd_pcm_lib_ioctl,
  756. .hw_params = snd_sonicvibes_hw_params,
  757. .hw_free = snd_sonicvibes_hw_free,
  758. .prepare = snd_sonicvibes_capture_prepare,
  759. .trigger = snd_sonicvibes_capture_trigger,
  760. .pointer = snd_sonicvibes_capture_pointer,
  761. };
  762. static void snd_sonicvibes_pcm_free(snd_pcm_t *pcm)
  763. {
  764. sonicvibes_t *sonic = pcm->private_data;
  765. sonic->pcm = NULL;
  766. snd_pcm_lib_preallocate_free_for_all(pcm);
  767. }
  768. static int __devinit snd_sonicvibes_pcm(sonicvibes_t * sonic, int device, snd_pcm_t ** rpcm)
  769. {
  770. snd_pcm_t *pcm;
  771. int err;
  772. if ((err = snd_pcm_new(sonic->card, "s3_86c617", device, 1, 1, &pcm)) < 0)
  773. return err;
  774. snd_assert(pcm != NULL, return -EINVAL);
  775. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sonicvibes_playback_ops);
  776. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sonicvibes_capture_ops);
  777. pcm->private_data = sonic;
  778. pcm->private_free = snd_sonicvibes_pcm_free;
  779. pcm->info_flags = 0;
  780. strcpy(pcm->name, "S3 SonicVibes");
  781. sonic->pcm = pcm;
  782. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  783. snd_dma_pci_data(sonic->pci), 64*1024, 128*1024);
  784. if (rpcm)
  785. *rpcm = pcm;
  786. return 0;
  787. }
  788. /*
  789. * Mixer part
  790. */
  791. #define SONICVIBES_MUX(xname, xindex) \
  792. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  793. .info = snd_sonicvibes_info_mux, \
  794. .get = snd_sonicvibes_get_mux, .put = snd_sonicvibes_put_mux }
  795. static int snd_sonicvibes_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  796. {
  797. static char *texts[7] = {
  798. "CD", "PCM", "Aux1", "Line", "Aux0", "Mic", "Mix"
  799. };
  800. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  801. uinfo->count = 2;
  802. uinfo->value.enumerated.items = 7;
  803. if (uinfo->value.enumerated.item >= 7)
  804. uinfo->value.enumerated.item = 6;
  805. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  806. return 0;
  807. }
  808. static int snd_sonicvibes_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  809. {
  810. sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
  811. spin_lock_irq(&sonic->reg_lock);
  812. ucontrol->value.enumerated.item[0] = ((snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
  813. ucontrol->value.enumerated.item[1] = ((snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
  814. spin_unlock_irq(&sonic->reg_lock);
  815. return 0;
  816. }
  817. static int snd_sonicvibes_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  818. {
  819. sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
  820. unsigned short left, right, oval1, oval2;
  821. int change;
  822. if (ucontrol->value.enumerated.item[0] >= 7 ||
  823. ucontrol->value.enumerated.item[1] >= 7)
  824. return -EINVAL;
  825. left = (ucontrol->value.enumerated.item[0] + 1) << 5;
  826. right = (ucontrol->value.enumerated.item[1] + 1) << 5;
  827. spin_lock_irq(&sonic->reg_lock);
  828. oval1 = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC);
  829. oval2 = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC);
  830. left = (oval1 & ~SV_RECSRC_OUT) | left;
  831. right = (oval2 & ~SV_RECSRC_OUT) | right;
  832. change = left != oval1 || right != oval2;
  833. snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ADC, left);
  834. snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ADC, right);
  835. spin_unlock_irq(&sonic->reg_lock);
  836. return change;
  837. }
  838. #define SONICVIBES_SINGLE(xname, xindex, reg, shift, mask, invert) \
  839. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  840. .info = snd_sonicvibes_info_single, \
  841. .get = snd_sonicvibes_get_single, .put = snd_sonicvibes_put_single, \
  842. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  843. static int snd_sonicvibes_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  844. {
  845. int mask = (kcontrol->private_value >> 16) & 0xff;
  846. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  847. uinfo->count = 1;
  848. uinfo->value.integer.min = 0;
  849. uinfo->value.integer.max = mask;
  850. return 0;
  851. }
  852. static int snd_sonicvibes_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  853. {
  854. sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
  855. int reg = kcontrol->private_value & 0xff;
  856. int shift = (kcontrol->private_value >> 8) & 0xff;
  857. int mask = (kcontrol->private_value >> 16) & 0xff;
  858. int invert = (kcontrol->private_value >> 24) & 0xff;
  859. spin_lock_irq(&sonic->reg_lock);
  860. ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, reg)>> shift) & mask;
  861. spin_unlock_irq(&sonic->reg_lock);
  862. if (invert)
  863. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  864. return 0;
  865. }
  866. static int snd_sonicvibes_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  867. {
  868. sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
  869. int reg = kcontrol->private_value & 0xff;
  870. int shift = (kcontrol->private_value >> 8) & 0xff;
  871. int mask = (kcontrol->private_value >> 16) & 0xff;
  872. int invert = (kcontrol->private_value >> 24) & 0xff;
  873. int change;
  874. unsigned short val, oval;
  875. val = (ucontrol->value.integer.value[0] & mask);
  876. if (invert)
  877. val = mask - val;
  878. val <<= shift;
  879. spin_lock_irq(&sonic->reg_lock);
  880. oval = snd_sonicvibes_in1(sonic, reg);
  881. val = (oval & ~(mask << shift)) | val;
  882. change = val != oval;
  883. snd_sonicvibes_out1(sonic, reg, val);
  884. spin_unlock_irq(&sonic->reg_lock);
  885. return change;
  886. }
  887. #define SONICVIBES_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  888. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  889. .info = snd_sonicvibes_info_double, \
  890. .get = snd_sonicvibes_get_double, .put = snd_sonicvibes_put_double, \
  891. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  892. static int snd_sonicvibes_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  893. {
  894. int mask = (kcontrol->private_value >> 24) & 0xff;
  895. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  896. uinfo->count = 2;
  897. uinfo->value.integer.min = 0;
  898. uinfo->value.integer.max = mask;
  899. return 0;
  900. }
  901. static int snd_sonicvibes_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  902. {
  903. sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
  904. int left_reg = kcontrol->private_value & 0xff;
  905. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  906. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  907. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  908. int mask = (kcontrol->private_value >> 24) & 0xff;
  909. int invert = (kcontrol->private_value >> 22) & 1;
  910. spin_lock_irq(&sonic->reg_lock);
  911. ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, left_reg) >> shift_left) & mask;
  912. ucontrol->value.integer.value[1] = (snd_sonicvibes_in1(sonic, right_reg) >> shift_right) & mask;
  913. spin_unlock_irq(&sonic->reg_lock);
  914. if (invert) {
  915. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  916. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  917. }
  918. return 0;
  919. }
  920. static int snd_sonicvibes_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  921. {
  922. sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
  923. int left_reg = kcontrol->private_value & 0xff;
  924. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  925. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  926. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  927. int mask = (kcontrol->private_value >> 24) & 0xff;
  928. int invert = (kcontrol->private_value >> 22) & 1;
  929. int change;
  930. unsigned short val1, val2, oval1, oval2;
  931. val1 = ucontrol->value.integer.value[0] & mask;
  932. val2 = ucontrol->value.integer.value[1] & mask;
  933. if (invert) {
  934. val1 = mask - val1;
  935. val2 = mask - val2;
  936. }
  937. val1 <<= shift_left;
  938. val2 <<= shift_right;
  939. spin_lock_irq(&sonic->reg_lock);
  940. oval1 = snd_sonicvibes_in1(sonic, left_reg);
  941. oval2 = snd_sonicvibes_in1(sonic, right_reg);
  942. val1 = (oval1 & ~(mask << shift_left)) | val1;
  943. val2 = (oval2 & ~(mask << shift_right)) | val2;
  944. change = val1 != oval1 || val2 != oval2;
  945. snd_sonicvibes_out1(sonic, left_reg, val1);
  946. snd_sonicvibes_out1(sonic, right_reg, val2);
  947. spin_unlock_irq(&sonic->reg_lock);
  948. return change;
  949. }
  950. static snd_kcontrol_new_t snd_sonicvibes_controls[] __devinitdata = {
  951. SONICVIBES_DOUBLE("Capture Volume", 0, SV_IREG_LEFT_ADC, SV_IREG_RIGHT_ADC, 0, 0, 15, 0),
  952. SONICVIBES_DOUBLE("Aux Playback Switch", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 7, 7, 1, 1),
  953. SONICVIBES_DOUBLE("Aux Playback Volume", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 0, 0, 31, 1),
  954. SONICVIBES_DOUBLE("CD Playback Switch", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 7, 7, 1, 1),
  955. SONICVIBES_DOUBLE("CD Playback Volume", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 0, 0, 31, 1),
  956. SONICVIBES_DOUBLE("Line Playback Switch", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 7, 7, 1, 1),
  957. SONICVIBES_DOUBLE("Line Playback Volume", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 0, 0, 31, 1),
  958. SONICVIBES_SINGLE("Mic Playback Switch", 0, SV_IREG_MIC, 7, 1, 1),
  959. SONICVIBES_SINGLE("Mic Playback Volume", 0, SV_IREG_MIC, 0, 15, 1),
  960. SONICVIBES_SINGLE("Mic Boost", 0, SV_IREG_LEFT_ADC, 4, 1, 0),
  961. SONICVIBES_DOUBLE("Synth Playback Switch", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 7, 7, 1, 1),
  962. SONICVIBES_DOUBLE("Synth Playback Volume", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 0, 0, 31, 1),
  963. SONICVIBES_DOUBLE("Aux Playback Switch", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 7, 7, 1, 1),
  964. SONICVIBES_DOUBLE("Aux Playback Volume", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 0, 0, 31, 1),
  965. SONICVIBES_DOUBLE("Master Playback Switch", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 7, 7, 1, 1),
  966. SONICVIBES_DOUBLE("Master Playback Volume", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 0, 0, 31, 1),
  967. SONICVIBES_DOUBLE("PCM Playback Switch", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 7, 7, 1, 1),
  968. SONICVIBES_DOUBLE("PCM Playback Volume", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 0, 0, 63, 1),
  969. SONICVIBES_SINGLE("Loopback Capture Switch", 0, SV_IREG_ADC_OUTPUT_CTRL, 0, 1, 0),
  970. SONICVIBES_SINGLE("Loopback Capture Volume", 0, SV_IREG_ADC_OUTPUT_CTRL, 2, 63, 1),
  971. SONICVIBES_MUX("Capture Source", 0)
  972. };
  973. static void snd_sonicvibes_master_free(snd_kcontrol_t *kcontrol)
  974. {
  975. sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
  976. sonic->master_mute = NULL;
  977. sonic->master_volume = NULL;
  978. }
  979. static int __devinit snd_sonicvibes_mixer(sonicvibes_t * sonic)
  980. {
  981. snd_card_t *card;
  982. snd_kcontrol_t *kctl;
  983. unsigned int idx;
  984. int err;
  985. snd_assert(sonic != NULL && sonic->card != NULL, return -EINVAL);
  986. card = sonic->card;
  987. strcpy(card->mixername, "S3 SonicVibes");
  988. for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_controls); idx++) {
  989. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_sonicvibes_controls[idx], sonic))) < 0)
  990. return err;
  991. switch (idx) {
  992. case 0:
  993. case 1: kctl->private_free = snd_sonicvibes_master_free; break;
  994. }
  995. }
  996. return 0;
  997. }
  998. /*
  999. */
  1000. static void snd_sonicvibes_proc_read(snd_info_entry_t *entry,
  1001. snd_info_buffer_t * buffer)
  1002. {
  1003. sonicvibes_t *sonic = entry->private_data;
  1004. unsigned char tmp;
  1005. tmp = sonic->srs_space & 0x0f;
  1006. snd_iprintf(buffer, "SRS 3D : %s\n",
  1007. sonic->srs_space & 0x80 ? "off" : "on");
  1008. snd_iprintf(buffer, "SRS Space : %s\n",
  1009. tmp == 0x00 ? "100%" :
  1010. tmp == 0x01 ? "75%" :
  1011. tmp == 0x02 ? "50%" :
  1012. tmp == 0x03 ? "25%" : "0%");
  1013. tmp = sonic->srs_center & 0x0f;
  1014. snd_iprintf(buffer, "SRS Center : %s\n",
  1015. tmp == 0x00 ? "100%" :
  1016. tmp == 0x01 ? "75%" :
  1017. tmp == 0x02 ? "50%" :
  1018. tmp == 0x03 ? "25%" : "0%");
  1019. tmp = sonic->wave_source & 0x03;
  1020. snd_iprintf(buffer, "WaveTable Source : %s\n",
  1021. tmp == 0x00 ? "on-board ROM" :
  1022. tmp == 0x01 ? "PCI bus" : "on-board ROM + PCI bus");
  1023. tmp = sonic->mpu_switch;
  1024. snd_iprintf(buffer, "Onboard synth : %s\n", tmp & 0x01 ? "on" : "off");
  1025. snd_iprintf(buffer, "Ext. Rx to synth : %s\n", tmp & 0x02 ? "on" : "off");
  1026. snd_iprintf(buffer, "MIDI to ext. Tx : %s\n", tmp & 0x04 ? "on" : "off");
  1027. }
  1028. static void __devinit snd_sonicvibes_proc_init(sonicvibes_t * sonic)
  1029. {
  1030. snd_info_entry_t *entry;
  1031. if (! snd_card_proc_new(sonic->card, "sonicvibes", &entry))
  1032. snd_info_set_text_ops(entry, sonic, 1024, snd_sonicvibes_proc_read);
  1033. }
  1034. /*
  1035. */
  1036. #ifdef SUPPORT_JOYSTICK
  1037. static snd_kcontrol_new_t snd_sonicvibes_game_control __devinitdata =
  1038. SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
  1039. static int __devinit snd_sonicvibes_create_gameport(sonicvibes_t *sonic)
  1040. {
  1041. struct gameport *gp;
  1042. sonic->gameport = gp = gameport_allocate_port();
  1043. if (!gp) {
  1044. printk(KERN_ERR "sonicvibes: cannot allocate memory for gameport\n");
  1045. return -ENOMEM;
  1046. }
  1047. gameport_set_name(gp, "SonicVibes Gameport");
  1048. gameport_set_phys(gp, "pci%s/gameport0", pci_name(sonic->pci));
  1049. gameport_set_dev_parent(gp, &sonic->pci->dev);
  1050. gp->io = sonic->game_port;
  1051. gameport_register_port(gp);
  1052. snd_ctl_add(sonic->card, snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
  1053. return 0;
  1054. }
  1055. static void snd_sonicvibes_free_gameport(sonicvibes_t *sonic)
  1056. {
  1057. if (sonic->gameport) {
  1058. gameport_unregister_port(sonic->gameport);
  1059. sonic->gameport = NULL;
  1060. }
  1061. }
  1062. #else
  1063. static inline int snd_sonicvibes_create_gameport(sonicvibes_t *sonic) { return -ENOSYS; }
  1064. static inline void snd_sonicvibes_free_gameport(sonicvibes_t *sonic) { }
  1065. #endif
  1066. static int snd_sonicvibes_free(sonicvibes_t *sonic)
  1067. {
  1068. snd_sonicvibes_free_gameport(sonic);
  1069. pci_write_config_dword(sonic->pci, 0x40, sonic->dmaa_port);
  1070. pci_write_config_dword(sonic->pci, 0x48, sonic->dmac_port);
  1071. if (sonic->irq >= 0)
  1072. free_irq(sonic->irq, (void *)sonic);
  1073. if (sonic->res_dmaa) {
  1074. release_resource(sonic->res_dmaa);
  1075. kfree_nocheck(sonic->res_dmaa);
  1076. }
  1077. if (sonic->res_dmac) {
  1078. release_resource(sonic->res_dmac);
  1079. kfree_nocheck(sonic->res_dmac);
  1080. }
  1081. pci_release_regions(sonic->pci);
  1082. pci_disable_device(sonic->pci);
  1083. kfree(sonic);
  1084. return 0;
  1085. }
  1086. static int snd_sonicvibes_dev_free(snd_device_t *device)
  1087. {
  1088. sonicvibes_t *sonic = device->device_data;
  1089. return snd_sonicvibes_free(sonic);
  1090. }
  1091. static int __devinit snd_sonicvibes_create(snd_card_t * card,
  1092. struct pci_dev *pci,
  1093. int reverb,
  1094. int mge,
  1095. sonicvibes_t ** rsonic)
  1096. {
  1097. sonicvibes_t *sonic;
  1098. unsigned int dmaa, dmac;
  1099. int err;
  1100. static snd_device_ops_t ops = {
  1101. .dev_free = snd_sonicvibes_dev_free,
  1102. };
  1103. *rsonic = NULL;
  1104. /* enable PCI device */
  1105. if ((err = pci_enable_device(pci)) < 0)
  1106. return err;
  1107. /* check, if we can restrict PCI DMA transfers to 24 bits */
  1108. if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
  1109. pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
  1110. snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
  1111. pci_disable_device(pci);
  1112. return -ENXIO;
  1113. }
  1114. sonic = kcalloc(1, sizeof(*sonic), GFP_KERNEL);
  1115. if (sonic == NULL) {
  1116. pci_disable_device(pci);
  1117. return -ENOMEM;
  1118. }
  1119. spin_lock_init(&sonic->reg_lock);
  1120. sonic->card = card;
  1121. sonic->pci = pci;
  1122. sonic->irq = -1;
  1123. if ((err = pci_request_regions(pci, "S3 SonicVibes")) < 0) {
  1124. kfree(sonic);
  1125. pci_disable_device(pci);
  1126. return err;
  1127. }
  1128. sonic->sb_port = pci_resource_start(pci, 0);
  1129. sonic->enh_port = pci_resource_start(pci, 1);
  1130. sonic->synth_port = pci_resource_start(pci, 2);
  1131. sonic->midi_port = pci_resource_start(pci, 3);
  1132. sonic->game_port = pci_resource_start(pci, 4);
  1133. if (request_irq(pci->irq, snd_sonicvibes_interrupt, SA_INTERRUPT|SA_SHIRQ, "S3 SonicVibes", (void *)sonic)) {
  1134. snd_printk("unable to grab IRQ %d\n", pci->irq);
  1135. snd_sonicvibes_free(sonic);
  1136. return -EBUSY;
  1137. }
  1138. sonic->irq = pci->irq;
  1139. pci_read_config_dword(pci, 0x40, &dmaa);
  1140. pci_read_config_dword(pci, 0x48, &dmac);
  1141. dmaio &= ~0x0f;
  1142. dmaa &= ~0x0f;
  1143. dmac &= ~0x0f;
  1144. if (!dmaa) {
  1145. dmaa = dmaio;
  1146. dmaio += 0x10;
  1147. snd_printk("BIOS did not allocate DDMA channel A i/o, allocated at 0x%x\n", dmaa);
  1148. }
  1149. if (!dmac) {
  1150. dmac = dmaio;
  1151. dmaio += 0x10;
  1152. snd_printk("BIOS did not allocate DDMA channel C i/o, allocated at 0x%x\n", dmac);
  1153. }
  1154. pci_write_config_dword(pci, 0x40, dmaa);
  1155. pci_write_config_dword(pci, 0x48, dmac);
  1156. if ((sonic->res_dmaa = request_region(dmaa, 0x10, "S3 SonicVibes DDMA-A")) == NULL) {
  1157. snd_sonicvibes_free(sonic);
  1158. snd_printk("unable to grab DDMA-A port at 0x%x-0x%x\n", dmaa, dmaa + 0x10 - 1);
  1159. return -EBUSY;
  1160. }
  1161. if ((sonic->res_dmac = request_region(dmac, 0x10, "S3 SonicVibes DDMA-C")) == NULL) {
  1162. snd_sonicvibes_free(sonic);
  1163. snd_printk("unable to grab DDMA-C port at 0x%x-0x%x\n", dmac, dmac + 0x10 - 1);
  1164. return -EBUSY;
  1165. }
  1166. pci_read_config_dword(pci, 0x40, &sonic->dmaa_port);
  1167. pci_read_config_dword(pci, 0x48, &sonic->dmac_port);
  1168. sonic->dmaa_port &= ~0x0f;
  1169. sonic->dmac_port &= ~0x0f;
  1170. pci_write_config_dword(pci, 0x40, sonic->dmaa_port | 9); /* enable + enhanced */
  1171. pci_write_config_dword(pci, 0x48, sonic->dmac_port | 9); /* enable */
  1172. /* ok.. initialize S3 SonicVibes chip */
  1173. outb(SV_RESET, SV_REG(sonic, CONTROL)); /* reset chip */
  1174. udelay(100);
  1175. outb(0, SV_REG(sonic, CONTROL)); /* release reset */
  1176. udelay(100);
  1177. outb(SV_ENHANCED | SV_INTA | (reverb ? SV_REVERB : 0), SV_REG(sonic, CONTROL));
  1178. inb(SV_REG(sonic, STATUS)); /* clear IRQs */
  1179. #if 1
  1180. snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0); /* drive current 16mA */
  1181. #else
  1182. snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0x40); /* drive current 8mA */
  1183. #endif
  1184. snd_sonicvibes_out(sonic, SV_IREG_PC_ENABLE, sonic->enable = 0); /* disable playback & capture */
  1185. outb(sonic->irqmask = ~(SV_DMAA_MASK | SV_DMAC_MASK | SV_UD_MASK), SV_REG(sonic, IRQMASK));
  1186. inb(SV_REG(sonic, STATUS)); /* clear IRQs */
  1187. snd_sonicvibes_out(sonic, SV_IREG_ADC_CLOCK, 0); /* use PLL as clock source */
  1188. snd_sonicvibes_out(sonic, SV_IREG_ANALOG_POWER, 0); /* power up analog parts */
  1189. snd_sonicvibes_out(sonic, SV_IREG_DIGITAL_POWER, 0); /* power up digital parts */
  1190. snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, 8000);
  1191. snd_sonicvibes_out(sonic, SV_IREG_SRS_SPACE, sonic->srs_space = 0x80); /* SRS space off */
  1192. snd_sonicvibes_out(sonic, SV_IREG_SRS_CENTER, sonic->srs_center = 0x00);/* SRS center off */
  1193. snd_sonicvibes_out(sonic, SV_IREG_MPU401, sonic->mpu_switch = 0x05); /* MPU-401 switch */
  1194. snd_sonicvibes_out(sonic, SV_IREG_WAVE_SOURCE, sonic->wave_source = 0x00); /* onboard ROM */
  1195. snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_LOW, (8000 * 65536 / SV_FULLRATE) & 0xff);
  1196. snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_HIGH, ((8000 * 65536 / SV_FULLRATE) >> 8) & 0xff);
  1197. snd_sonicvibes_out(sonic, SV_IREG_LEFT_ADC, mge ? 0xd0 : 0xc0);
  1198. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ADC, 0xc0);
  1199. snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX1, 0x9f);
  1200. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX1, 0x9f);
  1201. snd_sonicvibes_out(sonic, SV_IREG_LEFT_CD, 0x9f);
  1202. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_CD, 0x9f);
  1203. snd_sonicvibes_out(sonic, SV_IREG_LEFT_LINE, 0x9f);
  1204. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_LINE, 0x9f);
  1205. snd_sonicvibes_out(sonic, SV_IREG_MIC, 0x8f);
  1206. snd_sonicvibes_out(sonic, SV_IREG_LEFT_SYNTH, 0x9f);
  1207. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_SYNTH, 0x9f);
  1208. snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX2, 0x9f);
  1209. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX2, 0x9f);
  1210. snd_sonicvibes_out(sonic, SV_IREG_LEFT_ANALOG, 0x9f);
  1211. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ANALOG, 0x9f);
  1212. snd_sonicvibes_out(sonic, SV_IREG_LEFT_PCM, 0xbf);
  1213. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_PCM, 0xbf);
  1214. snd_sonicvibes_out(sonic, SV_IREG_ADC_OUTPUT_CTRL, 0xfc);
  1215. #if 0
  1216. snd_sonicvibes_debug(sonic);
  1217. #endif
  1218. sonic->revision = snd_sonicvibes_in(sonic, SV_IREG_REVISION);
  1219. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sonic, &ops)) < 0) {
  1220. snd_sonicvibes_free(sonic);
  1221. return err;
  1222. }
  1223. snd_sonicvibes_proc_init(sonic);
  1224. snd_card_set_dev(card, &pci->dev);
  1225. *rsonic = sonic;
  1226. return 0;
  1227. }
  1228. /*
  1229. * MIDI section
  1230. */
  1231. static snd_kcontrol_new_t snd_sonicvibes_midi_controls[] __devinitdata = {
  1232. SONICVIBES_SINGLE("SonicVibes Wave Source RAM", 0, SV_IREG_WAVE_SOURCE, 0, 1, 0),
  1233. SONICVIBES_SINGLE("SonicVibes Wave Source RAM+ROM", 0, SV_IREG_WAVE_SOURCE, 1, 1, 0),
  1234. SONICVIBES_SINGLE("SonicVibes Onboard Synth", 0, SV_IREG_MPU401, 0, 1, 0),
  1235. SONICVIBES_SINGLE("SonicVibes External Rx to Synth", 0, SV_IREG_MPU401, 1, 1, 0),
  1236. SONICVIBES_SINGLE("SonicVibes External Tx", 0, SV_IREG_MPU401, 2, 1, 0)
  1237. };
  1238. static int snd_sonicvibes_midi_input_open(mpu401_t * mpu)
  1239. {
  1240. sonicvibes_t *sonic = mpu->private_data;
  1241. outb(sonic->irqmask &= ~SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
  1242. return 0;
  1243. }
  1244. static void snd_sonicvibes_midi_input_close(mpu401_t * mpu)
  1245. {
  1246. sonicvibes_t *sonic = mpu->private_data;
  1247. outb(sonic->irqmask |= SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
  1248. }
  1249. static int __devinit snd_sonicvibes_midi(sonicvibes_t * sonic, snd_rawmidi_t * rmidi)
  1250. {
  1251. mpu401_t * mpu = rmidi->private_data;
  1252. snd_card_t *card = sonic->card;
  1253. snd_rawmidi_str_t *dir;
  1254. unsigned int idx;
  1255. int err;
  1256. mpu->private_data = sonic;
  1257. mpu->open_input = snd_sonicvibes_midi_input_open;
  1258. mpu->close_input = snd_sonicvibes_midi_input_close;
  1259. dir = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
  1260. for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_midi_controls); idx++)
  1261. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_sonicvibes_midi_controls[idx], sonic))) < 0)
  1262. return err;
  1263. return 0;
  1264. }
  1265. static int __devinit snd_sonic_probe(struct pci_dev *pci,
  1266. const struct pci_device_id *pci_id)
  1267. {
  1268. static int dev;
  1269. snd_card_t *card;
  1270. sonicvibes_t *sonic;
  1271. snd_rawmidi_t *midi_uart;
  1272. opl3_t *opl3;
  1273. int idx, err;
  1274. if (dev >= SNDRV_CARDS)
  1275. return -ENODEV;
  1276. if (!enable[dev]) {
  1277. dev++;
  1278. return -ENOENT;
  1279. }
  1280. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1281. if (card == NULL)
  1282. return -ENOMEM;
  1283. for (idx = 0; idx < 5; idx++) {
  1284. if (pci_resource_start(pci, idx) == 0 ||
  1285. !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
  1286. snd_card_free(card);
  1287. return -ENODEV;
  1288. }
  1289. }
  1290. if ((err = snd_sonicvibes_create(card, pci,
  1291. reverb[dev] ? 1 : 0,
  1292. mge[dev] ? 1 : 0,
  1293. &sonic)) < 0) {
  1294. snd_card_free(card);
  1295. return err;
  1296. }
  1297. strcpy(card->driver, "SonicVibes");
  1298. strcpy(card->shortname, "S3 SonicVibes");
  1299. sprintf(card->longname, "%s rev %i at 0x%lx, irq %i",
  1300. card->shortname,
  1301. sonic->revision,
  1302. pci_resource_start(pci, 1),
  1303. sonic->irq);
  1304. if ((err = snd_sonicvibes_pcm(sonic, 0, NULL)) < 0) {
  1305. snd_card_free(card);
  1306. return err;
  1307. }
  1308. if ((err = snd_sonicvibes_mixer(sonic)) < 0) {
  1309. snd_card_free(card);
  1310. return err;
  1311. }
  1312. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_SONICVIBES,
  1313. sonic->midi_port, 1,
  1314. sonic->irq, 0,
  1315. &midi_uart)) < 0) {
  1316. snd_card_free(card);
  1317. return err;
  1318. }
  1319. snd_sonicvibes_midi(sonic, midi_uart);
  1320. if ((err = snd_opl3_create(card, sonic->synth_port,
  1321. sonic->synth_port + 2,
  1322. OPL3_HW_OPL3_SV, 1, &opl3)) < 0) {
  1323. snd_card_free(card);
  1324. return err;
  1325. }
  1326. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  1327. snd_card_free(card);
  1328. return err;
  1329. }
  1330. snd_sonicvibes_create_gameport(sonic);
  1331. if ((err = snd_card_register(card)) < 0) {
  1332. snd_card_free(card);
  1333. return err;
  1334. }
  1335. pci_set_drvdata(pci, card);
  1336. dev++;
  1337. return 0;
  1338. }
  1339. static void __devexit snd_sonic_remove(struct pci_dev *pci)
  1340. {
  1341. snd_card_free(pci_get_drvdata(pci));
  1342. pci_set_drvdata(pci, NULL);
  1343. }
  1344. static struct pci_driver driver = {
  1345. .name = "S3 SonicVibes",
  1346. .id_table = snd_sonic_ids,
  1347. .probe = snd_sonic_probe,
  1348. .remove = __devexit_p(snd_sonic_remove),
  1349. };
  1350. static int __init alsa_card_sonicvibes_init(void)
  1351. {
  1352. return pci_register_driver(&driver);
  1353. }
  1354. static void __exit alsa_card_sonicvibes_exit(void)
  1355. {
  1356. pci_unregister_driver(&driver);
  1357. }
  1358. module_init(alsa_card_sonicvibes_init)
  1359. module_exit(alsa_card_sonicvibes_exit)