sonicvibes.c 51 KB

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