sonicvibes.c 51 KB

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