sonicvibes.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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/moduleparam.h>
  31. #include <linux/dma-mapping.h>
  32. #include <sound/core.h>
  33. #include <sound/pcm.h>
  34. #include <sound/info.h>
  35. #include <sound/control.h>
  36. #include <sound/mpu401.h>
  37. #include <sound/opl3.h>
  38. #include <sound/initval.h>
  39. #include <asm/io.h>
  40. MODULE_AUTHOR("Jaroslav Kysela <perex@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 int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  50. static int reverb[SNDRV_CARDS];
  51. static int 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 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. printk(KERN_DEBUG "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. printk(KERN_DEBUG "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. printk(KERN_DEBUG
  314. "SV REGS: INDEX = 0x%02x ", inb(SV_REG(sonic, INDEX)));
  315. printk(" STATUS = 0x%02x\n", inb(SV_REG(sonic, STATUS)));
  316. printk(KERN_DEBUG
  317. " 0x00: left input = 0x%02x ", snd_sonicvibes_in(sonic, 0x00));
  318. printk(" 0x20: synth rate low = 0x%02x\n", snd_sonicvibes_in(sonic, 0x20));
  319. printk(KERN_DEBUG
  320. " 0x01: right input = 0x%02x ", snd_sonicvibes_in(sonic, 0x01));
  321. printk(" 0x21: synth rate high = 0x%02x\n", snd_sonicvibes_in(sonic, 0x21));
  322. printk(KERN_DEBUG
  323. " 0x02: left AUX1 = 0x%02x ", snd_sonicvibes_in(sonic, 0x02));
  324. printk(" 0x22: ADC clock = 0x%02x\n", snd_sonicvibes_in(sonic, 0x22));
  325. printk(KERN_DEBUG
  326. " 0x03: right AUX1 = 0x%02x ", snd_sonicvibes_in(sonic, 0x03));
  327. printk(" 0x23: ADC alt rate = 0x%02x\n", snd_sonicvibes_in(sonic, 0x23));
  328. printk(KERN_DEBUG
  329. " 0x04: left CD = 0x%02x ", snd_sonicvibes_in(sonic, 0x04));
  330. printk(" 0x24: ADC pll M = 0x%02x\n", snd_sonicvibes_in(sonic, 0x24));
  331. printk(KERN_DEBUG
  332. " 0x05: right CD = 0x%02x ", snd_sonicvibes_in(sonic, 0x05));
  333. printk(" 0x25: ADC pll N = 0x%02x\n", snd_sonicvibes_in(sonic, 0x25));
  334. printk(KERN_DEBUG
  335. " 0x06: left line = 0x%02x ", snd_sonicvibes_in(sonic, 0x06));
  336. printk(" 0x26: Synth pll M = 0x%02x\n", snd_sonicvibes_in(sonic, 0x26));
  337. printk(KERN_DEBUG
  338. " 0x07: right line = 0x%02x ", snd_sonicvibes_in(sonic, 0x07));
  339. printk(" 0x27: Synth pll N = 0x%02x\n", snd_sonicvibes_in(sonic, 0x27));
  340. printk(KERN_DEBUG
  341. " 0x08: MIC = 0x%02x ", snd_sonicvibes_in(sonic, 0x08));
  342. printk(" 0x28: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x28));
  343. printk(KERN_DEBUG
  344. " 0x09: Game port = 0x%02x ", snd_sonicvibes_in(sonic, 0x09));
  345. printk(" 0x29: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x29));
  346. printk(KERN_DEBUG
  347. " 0x0a: left synth = 0x%02x ", snd_sonicvibes_in(sonic, 0x0a));
  348. printk(" 0x2a: MPU401 = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2a));
  349. printk(KERN_DEBUG
  350. " 0x0b: right synth = 0x%02x ", snd_sonicvibes_in(sonic, 0x0b));
  351. printk(" 0x2b: drive ctrl = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2b));
  352. printk(KERN_DEBUG
  353. " 0x0c: left AUX2 = 0x%02x ", snd_sonicvibes_in(sonic, 0x0c));
  354. printk(" 0x2c: SRS space = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2c));
  355. printk(KERN_DEBUG
  356. " 0x0d: right AUX2 = 0x%02x ", snd_sonicvibes_in(sonic, 0x0d));
  357. printk(" 0x2d: SRS center = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2d));
  358. printk(KERN_DEBUG
  359. " 0x0e: left analog = 0x%02x ", snd_sonicvibes_in(sonic, 0x0e));
  360. printk(" 0x2e: wave source = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2e));
  361. printk(KERN_DEBUG
  362. " 0x0f: right analog = 0x%02x ", snd_sonicvibes_in(sonic, 0x0f));
  363. printk(" 0x2f: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2f));
  364. printk(KERN_DEBUG
  365. " 0x10: left PCM = 0x%02x ", snd_sonicvibes_in(sonic, 0x10));
  366. printk(" 0x30: analog power = 0x%02x\n", snd_sonicvibes_in(sonic, 0x30));
  367. printk(KERN_DEBUG
  368. " 0x11: right PCM = 0x%02x ", snd_sonicvibes_in(sonic, 0x11));
  369. printk(" 0x31: analog power = 0x%02x\n", snd_sonicvibes_in(sonic, 0x31));
  370. printk(KERN_DEBUG
  371. " 0x12: DMA data format = 0x%02x ", snd_sonicvibes_in(sonic, 0x12));
  372. printk(" 0x32: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x32));
  373. printk(KERN_DEBUG
  374. " 0x13: P/C enable = 0x%02x ", snd_sonicvibes_in(sonic, 0x13));
  375. printk(" 0x33: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x33));
  376. printk(KERN_DEBUG
  377. " 0x14: U/D button = 0x%02x ", snd_sonicvibes_in(sonic, 0x14));
  378. printk(" 0x34: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x34));
  379. printk(KERN_DEBUG
  380. " 0x15: revision = 0x%02x ", snd_sonicvibes_in(sonic, 0x15));
  381. printk(" 0x35: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x35));
  382. printk(KERN_DEBUG
  383. " 0x16: ADC output ctrl = 0x%02x ", snd_sonicvibes_in(sonic, 0x16));
  384. printk(" 0x36: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x36));
  385. printk(KERN_DEBUG
  386. " 0x17: --- = 0x%02x ", snd_sonicvibes_in(sonic, 0x17));
  387. printk(" 0x37: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x37));
  388. printk(KERN_DEBUG
  389. " 0x18: DMA A upper cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x18));
  390. printk(" 0x38: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x38));
  391. printk(KERN_DEBUG
  392. " 0x19: DMA A lower cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x19));
  393. printk(" 0x39: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x39));
  394. printk(KERN_DEBUG
  395. " 0x1a: --- = 0x%02x ", snd_sonicvibes_in(sonic, 0x1a));
  396. printk(" 0x3a: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3a));
  397. printk(KERN_DEBUG
  398. " 0x1b: --- = 0x%02x ", snd_sonicvibes_in(sonic, 0x1b));
  399. printk(" 0x3b: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3b));
  400. printk(KERN_DEBUG
  401. " 0x1c: DMA C upper cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x1c));
  402. printk(" 0x3c: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3c));
  403. printk(KERN_DEBUG
  404. " 0x1d: DMA C upper cnt = 0x%02x ", snd_sonicvibes_in(sonic, 0x1d));
  405. printk(" 0x3d: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3d));
  406. printk(KERN_DEBUG
  407. " 0x1e: PCM rate low = 0x%02x ", snd_sonicvibes_in(sonic, 0x1e));
  408. printk(" 0x3e: --- = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3e));
  409. printk(KERN_DEBUG
  410. " 0x1f: PCM rate high = 0x%02x ", snd_sonicvibes_in(sonic, 0x1f));
  411. printk(" 0x3f: --- = 0x%02x\n", 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. printk(KERN_DEBUG "metric = %i, xm = %i, xn = %i\n", metric, xm, xn);
  463. printk(KERN_DEBUG "pll: m = 0x%x, r = 0x%x, n = 0x%x\n", reg, m, r, n);
  464. #endif
  465. }
  466. static void snd_sonicvibes_setpll(struct sonicvibes * sonic,
  467. unsigned char reg,
  468. unsigned int rate)
  469. {
  470. unsigned long flags;
  471. unsigned int r, m, n;
  472. snd_sonicvibes_pll(rate, &r, &m, &n);
  473. if (sonic != NULL) {
  474. spin_lock_irqsave(&sonic->reg_lock, flags);
  475. snd_sonicvibes_out1(sonic, reg, m);
  476. snd_sonicvibes_out1(sonic, reg + 1, r | n);
  477. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  478. }
  479. }
  480. static void snd_sonicvibes_set_adc_rate(struct sonicvibes * sonic, unsigned int rate)
  481. {
  482. unsigned long flags;
  483. unsigned int div;
  484. unsigned char clock;
  485. div = 48000 / rate;
  486. if (div > 8)
  487. div = 8;
  488. if ((48000 / div) == rate) { /* use the alternate clock */
  489. clock = 0x10;
  490. } else { /* use the PLL source */
  491. clock = 0x00;
  492. snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, rate);
  493. }
  494. spin_lock_irqsave(&sonic->reg_lock, flags);
  495. snd_sonicvibes_out1(sonic, SV_IREG_ADC_ALT_RATE, (div - 1) << 4);
  496. snd_sonicvibes_out1(sonic, SV_IREG_ADC_CLOCK, clock);
  497. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  498. }
  499. static int snd_sonicvibes_hw_constraint_dac_rate(struct snd_pcm_hw_params *params,
  500. struct snd_pcm_hw_rule *rule)
  501. {
  502. unsigned int rate, div, r, m, n;
  503. if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min ==
  504. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max) {
  505. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min;
  506. div = 48000 / rate;
  507. if (div > 8)
  508. div = 8;
  509. if ((48000 / div) == rate) {
  510. params->rate_num = rate;
  511. params->rate_den = 1;
  512. } else {
  513. snd_sonicvibes_pll(rate, &r, &m, &n);
  514. snd_BUG_ON(SV_REFFREQUENCY % 16);
  515. snd_BUG_ON(SV_ADCMULT % 512);
  516. params->rate_num = (SV_REFFREQUENCY/16) * (n+2) * r;
  517. params->rate_den = (SV_ADCMULT/512) * (m+2);
  518. }
  519. }
  520. return 0;
  521. }
  522. static void snd_sonicvibes_set_dac_rate(struct sonicvibes * sonic, unsigned int rate)
  523. {
  524. unsigned int div;
  525. unsigned long flags;
  526. div = (rate * 65536 + SV_FULLRATE / 2) / SV_FULLRATE;
  527. if (div > 65535)
  528. div = 65535;
  529. spin_lock_irqsave(&sonic->reg_lock, flags);
  530. snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_HIGH, div >> 8);
  531. snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_LOW, div);
  532. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  533. }
  534. static int snd_sonicvibes_trigger(struct sonicvibes * sonic, int what, int cmd)
  535. {
  536. int result = 0;
  537. spin_lock(&sonic->reg_lock);
  538. if (cmd == SNDRV_PCM_TRIGGER_START) {
  539. if (!(sonic->enable & what)) {
  540. sonic->enable |= what;
  541. snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
  542. }
  543. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  544. if (sonic->enable & what) {
  545. sonic->enable &= ~what;
  546. snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
  547. }
  548. } else {
  549. result = -EINVAL;
  550. }
  551. spin_unlock(&sonic->reg_lock);
  552. return result;
  553. }
  554. static irqreturn_t snd_sonicvibes_interrupt(int irq, void *dev_id)
  555. {
  556. struct sonicvibes *sonic = dev_id;
  557. unsigned char status;
  558. status = inb(SV_REG(sonic, STATUS));
  559. if (!(status & (SV_DMAA_IRQ | SV_DMAC_IRQ | SV_MIDI_IRQ)))
  560. return IRQ_NONE;
  561. if (status == 0xff) { /* failure */
  562. outb(sonic->irqmask = ~0, SV_REG(sonic, IRQMASK));
  563. snd_printk(KERN_ERR "IRQ failure - interrupts disabled!!\n");
  564. return IRQ_HANDLED;
  565. }
  566. if (sonic->pcm) {
  567. if (status & SV_DMAA_IRQ)
  568. snd_pcm_period_elapsed(sonic->playback_substream);
  569. if (status & SV_DMAC_IRQ)
  570. snd_pcm_period_elapsed(sonic->capture_substream);
  571. }
  572. if (sonic->rmidi) {
  573. if (status & SV_MIDI_IRQ)
  574. snd_mpu401_uart_interrupt(irq, sonic->rmidi->private_data);
  575. }
  576. if (status & SV_UD_IRQ) {
  577. unsigned char udreg;
  578. int vol, oleft, oright, mleft, mright;
  579. spin_lock(&sonic->reg_lock);
  580. udreg = snd_sonicvibes_in1(sonic, SV_IREG_UD_BUTTON);
  581. vol = udreg & 0x3f;
  582. if (!(udreg & 0x40))
  583. vol = -vol;
  584. oleft = mleft = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ANALOG);
  585. oright = mright = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ANALOG);
  586. oleft &= 0x1f;
  587. oright &= 0x1f;
  588. oleft += vol;
  589. if (oleft < 0)
  590. oleft = 0;
  591. if (oleft > 0x1f)
  592. oleft = 0x1f;
  593. oright += vol;
  594. if (oright < 0)
  595. oright = 0;
  596. if (oright > 0x1f)
  597. oright = 0x1f;
  598. if (udreg & 0x80) {
  599. mleft ^= 0x80;
  600. mright ^= 0x80;
  601. }
  602. oleft |= mleft & 0x80;
  603. oright |= mright & 0x80;
  604. snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ANALOG, oleft);
  605. snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ANALOG, oright);
  606. spin_unlock(&sonic->reg_lock);
  607. snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_mute->id);
  608. snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_volume->id);
  609. }
  610. return IRQ_HANDLED;
  611. }
  612. /*
  613. * PCM part
  614. */
  615. static int snd_sonicvibes_playback_trigger(struct snd_pcm_substream *substream,
  616. int cmd)
  617. {
  618. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  619. return snd_sonicvibes_trigger(sonic, 1, cmd);
  620. }
  621. static int snd_sonicvibes_capture_trigger(struct snd_pcm_substream *substream,
  622. int cmd)
  623. {
  624. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  625. return snd_sonicvibes_trigger(sonic, 2, cmd);
  626. }
  627. static int snd_sonicvibes_hw_params(struct snd_pcm_substream *substream,
  628. struct snd_pcm_hw_params *hw_params)
  629. {
  630. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  631. }
  632. static int snd_sonicvibes_hw_free(struct snd_pcm_substream *substream)
  633. {
  634. return snd_pcm_lib_free_pages(substream);
  635. }
  636. static int snd_sonicvibes_playback_prepare(struct snd_pcm_substream *substream)
  637. {
  638. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  639. struct snd_pcm_runtime *runtime = substream->runtime;
  640. unsigned char fmt = 0;
  641. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  642. unsigned int count = snd_pcm_lib_period_bytes(substream);
  643. sonic->p_dma_size = size;
  644. count--;
  645. if (runtime->channels > 1)
  646. fmt |= 1;
  647. if (snd_pcm_format_width(runtime->format) == 16)
  648. fmt |= 2;
  649. snd_sonicvibes_setfmt(sonic, ~3, fmt);
  650. snd_sonicvibes_set_dac_rate(sonic, runtime->rate);
  651. spin_lock_irq(&sonic->reg_lock);
  652. snd_sonicvibes_setdmaa(sonic, runtime->dma_addr, size);
  653. snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_UPPER, count >> 8);
  654. snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_LOWER, count);
  655. spin_unlock_irq(&sonic->reg_lock);
  656. return 0;
  657. }
  658. static int snd_sonicvibes_capture_prepare(struct snd_pcm_substream *substream)
  659. {
  660. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  661. struct snd_pcm_runtime *runtime = substream->runtime;
  662. unsigned char fmt = 0;
  663. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  664. unsigned int count = snd_pcm_lib_period_bytes(substream);
  665. sonic->c_dma_size = size;
  666. count >>= 1;
  667. count--;
  668. if (runtime->channels > 1)
  669. fmt |= 0x10;
  670. if (snd_pcm_format_width(runtime->format) == 16)
  671. fmt |= 0x20;
  672. snd_sonicvibes_setfmt(sonic, ~0x30, fmt);
  673. snd_sonicvibes_set_adc_rate(sonic, runtime->rate);
  674. spin_lock_irq(&sonic->reg_lock);
  675. snd_sonicvibes_setdmac(sonic, runtime->dma_addr, size);
  676. snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_UPPER, count >> 8);
  677. snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_LOWER, count);
  678. spin_unlock_irq(&sonic->reg_lock);
  679. return 0;
  680. }
  681. static snd_pcm_uframes_t snd_sonicvibes_playback_pointer(struct snd_pcm_substream *substream)
  682. {
  683. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  684. size_t ptr;
  685. if (!(sonic->enable & 1))
  686. return 0;
  687. ptr = sonic->p_dma_size - snd_sonicvibes_getdmaa(sonic);
  688. return bytes_to_frames(substream->runtime, ptr);
  689. }
  690. static snd_pcm_uframes_t snd_sonicvibes_capture_pointer(struct snd_pcm_substream *substream)
  691. {
  692. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  693. size_t ptr;
  694. if (!(sonic->enable & 2))
  695. return 0;
  696. ptr = sonic->c_dma_size - snd_sonicvibes_getdmac(sonic);
  697. return bytes_to_frames(substream->runtime, ptr);
  698. }
  699. static struct snd_pcm_hardware snd_sonicvibes_playback =
  700. {
  701. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  702. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  703. SNDRV_PCM_INFO_MMAP_VALID),
  704. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  705. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  706. .rate_min = 4000,
  707. .rate_max = 48000,
  708. .channels_min = 1,
  709. .channels_max = 2,
  710. .buffer_bytes_max = (128*1024),
  711. .period_bytes_min = 32,
  712. .period_bytes_max = (128*1024),
  713. .periods_min = 1,
  714. .periods_max = 1024,
  715. .fifo_size = 0,
  716. };
  717. static struct snd_pcm_hardware snd_sonicvibes_capture =
  718. {
  719. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  720. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  721. SNDRV_PCM_INFO_MMAP_VALID),
  722. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  723. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  724. .rate_min = 4000,
  725. .rate_max = 48000,
  726. .channels_min = 1,
  727. .channels_max = 2,
  728. .buffer_bytes_max = (128*1024),
  729. .period_bytes_min = 32,
  730. .period_bytes_max = (128*1024),
  731. .periods_min = 1,
  732. .periods_max = 1024,
  733. .fifo_size = 0,
  734. };
  735. static int snd_sonicvibes_playback_open(struct snd_pcm_substream *substream)
  736. {
  737. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  738. struct snd_pcm_runtime *runtime = substream->runtime;
  739. sonic->mode |= SV_MODE_PLAY;
  740. sonic->playback_substream = substream;
  741. runtime->hw = snd_sonicvibes_playback;
  742. 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);
  743. return 0;
  744. }
  745. static int snd_sonicvibes_capture_open(struct snd_pcm_substream *substream)
  746. {
  747. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  748. struct snd_pcm_runtime *runtime = substream->runtime;
  749. sonic->mode |= SV_MODE_CAPTURE;
  750. sonic->capture_substream = substream;
  751. runtime->hw = snd_sonicvibes_capture;
  752. snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  753. &snd_sonicvibes_hw_constraints_adc_clock);
  754. return 0;
  755. }
  756. static int snd_sonicvibes_playback_close(struct snd_pcm_substream *substream)
  757. {
  758. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  759. sonic->playback_substream = NULL;
  760. sonic->mode &= ~SV_MODE_PLAY;
  761. return 0;
  762. }
  763. static int snd_sonicvibes_capture_close(struct snd_pcm_substream *substream)
  764. {
  765. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  766. sonic->capture_substream = NULL;
  767. sonic->mode &= ~SV_MODE_CAPTURE;
  768. return 0;
  769. }
  770. static struct snd_pcm_ops snd_sonicvibes_playback_ops = {
  771. .open = snd_sonicvibes_playback_open,
  772. .close = snd_sonicvibes_playback_close,
  773. .ioctl = snd_pcm_lib_ioctl,
  774. .hw_params = snd_sonicvibes_hw_params,
  775. .hw_free = snd_sonicvibes_hw_free,
  776. .prepare = snd_sonicvibes_playback_prepare,
  777. .trigger = snd_sonicvibes_playback_trigger,
  778. .pointer = snd_sonicvibes_playback_pointer,
  779. };
  780. static struct snd_pcm_ops snd_sonicvibes_capture_ops = {
  781. .open = snd_sonicvibes_capture_open,
  782. .close = snd_sonicvibes_capture_close,
  783. .ioctl = snd_pcm_lib_ioctl,
  784. .hw_params = snd_sonicvibes_hw_params,
  785. .hw_free = snd_sonicvibes_hw_free,
  786. .prepare = snd_sonicvibes_capture_prepare,
  787. .trigger = snd_sonicvibes_capture_trigger,
  788. .pointer = snd_sonicvibes_capture_pointer,
  789. };
  790. static int __devinit snd_sonicvibes_pcm(struct sonicvibes * sonic, int device, struct snd_pcm ** rpcm)
  791. {
  792. struct snd_pcm *pcm;
  793. int err;
  794. if ((err = snd_pcm_new(sonic->card, "s3_86c617", device, 1, 1, &pcm)) < 0)
  795. return err;
  796. if (snd_BUG_ON(!pcm))
  797. return -EINVAL;
  798. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sonicvibes_playback_ops);
  799. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sonicvibes_capture_ops);
  800. pcm->private_data = sonic;
  801. pcm->info_flags = 0;
  802. strcpy(pcm->name, "S3 SonicVibes");
  803. sonic->pcm = pcm;
  804. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  805. snd_dma_pci_data(sonic->pci), 64*1024, 128*1024);
  806. if (rpcm)
  807. *rpcm = pcm;
  808. return 0;
  809. }
  810. /*
  811. * Mixer part
  812. */
  813. #define SONICVIBES_MUX(xname, xindex) \
  814. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  815. .info = snd_sonicvibes_info_mux, \
  816. .get = snd_sonicvibes_get_mux, .put = snd_sonicvibes_put_mux }
  817. static int snd_sonicvibes_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  818. {
  819. static char *texts[7] = {
  820. "CD", "PCM", "Aux1", "Line", "Aux0", "Mic", "Mix"
  821. };
  822. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  823. uinfo->count = 2;
  824. uinfo->value.enumerated.items = 7;
  825. if (uinfo->value.enumerated.item >= 7)
  826. uinfo->value.enumerated.item = 6;
  827. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  828. return 0;
  829. }
  830. static int snd_sonicvibes_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  831. {
  832. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  833. spin_lock_irq(&sonic->reg_lock);
  834. ucontrol->value.enumerated.item[0] = ((snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
  835. ucontrol->value.enumerated.item[1] = ((snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
  836. spin_unlock_irq(&sonic->reg_lock);
  837. return 0;
  838. }
  839. static int snd_sonicvibes_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  840. {
  841. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  842. unsigned short left, right, oval1, oval2;
  843. int change;
  844. if (ucontrol->value.enumerated.item[0] >= 7 ||
  845. ucontrol->value.enumerated.item[1] >= 7)
  846. return -EINVAL;
  847. left = (ucontrol->value.enumerated.item[0] + 1) << 5;
  848. right = (ucontrol->value.enumerated.item[1] + 1) << 5;
  849. spin_lock_irq(&sonic->reg_lock);
  850. oval1 = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC);
  851. oval2 = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC);
  852. left = (oval1 & ~SV_RECSRC_OUT) | left;
  853. right = (oval2 & ~SV_RECSRC_OUT) | right;
  854. change = left != oval1 || right != oval2;
  855. snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ADC, left);
  856. snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ADC, right);
  857. spin_unlock_irq(&sonic->reg_lock);
  858. return change;
  859. }
  860. #define SONICVIBES_SINGLE(xname, xindex, reg, shift, mask, invert) \
  861. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  862. .info = snd_sonicvibes_info_single, \
  863. .get = snd_sonicvibes_get_single, .put = snd_sonicvibes_put_single, \
  864. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  865. static int snd_sonicvibes_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  866. {
  867. int mask = (kcontrol->private_value >> 16) & 0xff;
  868. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  869. uinfo->count = 1;
  870. uinfo->value.integer.min = 0;
  871. uinfo->value.integer.max = mask;
  872. return 0;
  873. }
  874. static int snd_sonicvibes_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  875. {
  876. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  877. int reg = kcontrol->private_value & 0xff;
  878. int shift = (kcontrol->private_value >> 8) & 0xff;
  879. int mask = (kcontrol->private_value >> 16) & 0xff;
  880. int invert = (kcontrol->private_value >> 24) & 0xff;
  881. spin_lock_irq(&sonic->reg_lock);
  882. ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, reg)>> shift) & mask;
  883. spin_unlock_irq(&sonic->reg_lock);
  884. if (invert)
  885. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  886. return 0;
  887. }
  888. static int snd_sonicvibes_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  889. {
  890. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  891. int reg = kcontrol->private_value & 0xff;
  892. int shift = (kcontrol->private_value >> 8) & 0xff;
  893. int mask = (kcontrol->private_value >> 16) & 0xff;
  894. int invert = (kcontrol->private_value >> 24) & 0xff;
  895. int change;
  896. unsigned short val, oval;
  897. val = (ucontrol->value.integer.value[0] & mask);
  898. if (invert)
  899. val = mask - val;
  900. val <<= shift;
  901. spin_lock_irq(&sonic->reg_lock);
  902. oval = snd_sonicvibes_in1(sonic, reg);
  903. val = (oval & ~(mask << shift)) | val;
  904. change = val != oval;
  905. snd_sonicvibes_out1(sonic, reg, val);
  906. spin_unlock_irq(&sonic->reg_lock);
  907. return change;
  908. }
  909. #define SONICVIBES_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  910. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  911. .info = snd_sonicvibes_info_double, \
  912. .get = snd_sonicvibes_get_double, .put = snd_sonicvibes_put_double, \
  913. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  914. static int snd_sonicvibes_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  915. {
  916. int mask = (kcontrol->private_value >> 24) & 0xff;
  917. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  918. uinfo->count = 2;
  919. uinfo->value.integer.min = 0;
  920. uinfo->value.integer.max = mask;
  921. return 0;
  922. }
  923. static int snd_sonicvibes_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  924. {
  925. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  926. int left_reg = kcontrol->private_value & 0xff;
  927. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  928. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  929. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  930. int mask = (kcontrol->private_value >> 24) & 0xff;
  931. int invert = (kcontrol->private_value >> 22) & 1;
  932. spin_lock_irq(&sonic->reg_lock);
  933. ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, left_reg) >> shift_left) & mask;
  934. ucontrol->value.integer.value[1] = (snd_sonicvibes_in1(sonic, right_reg) >> shift_right) & mask;
  935. spin_unlock_irq(&sonic->reg_lock);
  936. if (invert) {
  937. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  938. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  939. }
  940. return 0;
  941. }
  942. static int snd_sonicvibes_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  943. {
  944. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  945. int left_reg = kcontrol->private_value & 0xff;
  946. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  947. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  948. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  949. int mask = (kcontrol->private_value >> 24) & 0xff;
  950. int invert = (kcontrol->private_value >> 22) & 1;
  951. int change;
  952. unsigned short val1, val2, oval1, oval2;
  953. val1 = ucontrol->value.integer.value[0] & mask;
  954. val2 = ucontrol->value.integer.value[1] & mask;
  955. if (invert) {
  956. val1 = mask - val1;
  957. val2 = mask - val2;
  958. }
  959. val1 <<= shift_left;
  960. val2 <<= shift_right;
  961. spin_lock_irq(&sonic->reg_lock);
  962. oval1 = snd_sonicvibes_in1(sonic, left_reg);
  963. oval2 = snd_sonicvibes_in1(sonic, right_reg);
  964. val1 = (oval1 & ~(mask << shift_left)) | val1;
  965. val2 = (oval2 & ~(mask << shift_right)) | val2;
  966. change = val1 != oval1 || val2 != oval2;
  967. snd_sonicvibes_out1(sonic, left_reg, val1);
  968. snd_sonicvibes_out1(sonic, right_reg, val2);
  969. spin_unlock_irq(&sonic->reg_lock);
  970. return change;
  971. }
  972. static struct snd_kcontrol_new snd_sonicvibes_controls[] __devinitdata = {
  973. SONICVIBES_DOUBLE("Capture Volume", 0, SV_IREG_LEFT_ADC, SV_IREG_RIGHT_ADC, 0, 0, 15, 0),
  974. SONICVIBES_DOUBLE("Aux Playback Switch", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 7, 7, 1, 1),
  975. SONICVIBES_DOUBLE("Aux Playback Volume", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 0, 0, 31, 1),
  976. SONICVIBES_DOUBLE("CD Playback Switch", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 7, 7, 1, 1),
  977. SONICVIBES_DOUBLE("CD Playback Volume", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 0, 0, 31, 1),
  978. SONICVIBES_DOUBLE("Line Playback Switch", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 7, 7, 1, 1),
  979. SONICVIBES_DOUBLE("Line Playback Volume", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 0, 0, 31, 1),
  980. SONICVIBES_SINGLE("Mic Playback Switch", 0, SV_IREG_MIC, 7, 1, 1),
  981. SONICVIBES_SINGLE("Mic Playback Volume", 0, SV_IREG_MIC, 0, 15, 1),
  982. SONICVIBES_SINGLE("Mic Boost", 0, SV_IREG_LEFT_ADC, 4, 1, 0),
  983. SONICVIBES_DOUBLE("Synth Playback Switch", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 7, 7, 1, 1),
  984. SONICVIBES_DOUBLE("Synth Playback Volume", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 0, 0, 31, 1),
  985. SONICVIBES_DOUBLE("Aux Playback Switch", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 7, 7, 1, 1),
  986. SONICVIBES_DOUBLE("Aux Playback Volume", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 0, 0, 31, 1),
  987. SONICVIBES_DOUBLE("Master Playback Switch", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 7, 7, 1, 1),
  988. SONICVIBES_DOUBLE("Master Playback Volume", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 0, 0, 31, 1),
  989. SONICVIBES_DOUBLE("PCM Playback Switch", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 7, 7, 1, 1),
  990. SONICVIBES_DOUBLE("PCM Playback Volume", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 0, 0, 63, 1),
  991. SONICVIBES_SINGLE("Loopback Capture Switch", 0, SV_IREG_ADC_OUTPUT_CTRL, 0, 1, 0),
  992. SONICVIBES_SINGLE("Loopback Capture Volume", 0, SV_IREG_ADC_OUTPUT_CTRL, 2, 63, 1),
  993. SONICVIBES_MUX("Capture Source", 0)
  994. };
  995. static void snd_sonicvibes_master_free(struct snd_kcontrol *kcontrol)
  996. {
  997. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  998. sonic->master_mute = NULL;
  999. sonic->master_volume = NULL;
  1000. }
  1001. static int __devinit snd_sonicvibes_mixer(struct sonicvibes * sonic)
  1002. {
  1003. struct snd_card *card;
  1004. struct snd_kcontrol *kctl;
  1005. unsigned int idx;
  1006. int err;
  1007. if (snd_BUG_ON(!sonic || !sonic->card))
  1008. return -EINVAL;
  1009. card = sonic->card;
  1010. strcpy(card->mixername, "S3 SonicVibes");
  1011. for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_controls); idx++) {
  1012. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_sonicvibes_controls[idx], sonic))) < 0)
  1013. return err;
  1014. switch (idx) {
  1015. case 0:
  1016. case 1: kctl->private_free = snd_sonicvibes_master_free; break;
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. /*
  1022. */
  1023. static void snd_sonicvibes_proc_read(struct snd_info_entry *entry,
  1024. struct snd_info_buffer *buffer)
  1025. {
  1026. struct sonicvibes *sonic = entry->private_data;
  1027. unsigned char tmp;
  1028. tmp = sonic->srs_space & 0x0f;
  1029. snd_iprintf(buffer, "SRS 3D : %s\n",
  1030. sonic->srs_space & 0x80 ? "off" : "on");
  1031. snd_iprintf(buffer, "SRS Space : %s\n",
  1032. tmp == 0x00 ? "100%" :
  1033. tmp == 0x01 ? "75%" :
  1034. tmp == 0x02 ? "50%" :
  1035. tmp == 0x03 ? "25%" : "0%");
  1036. tmp = sonic->srs_center & 0x0f;
  1037. snd_iprintf(buffer, "SRS Center : %s\n",
  1038. tmp == 0x00 ? "100%" :
  1039. tmp == 0x01 ? "75%" :
  1040. tmp == 0x02 ? "50%" :
  1041. tmp == 0x03 ? "25%" : "0%");
  1042. tmp = sonic->wave_source & 0x03;
  1043. snd_iprintf(buffer, "WaveTable Source : %s\n",
  1044. tmp == 0x00 ? "on-board ROM" :
  1045. tmp == 0x01 ? "PCI bus" : "on-board ROM + PCI bus");
  1046. tmp = sonic->mpu_switch;
  1047. snd_iprintf(buffer, "Onboard synth : %s\n", tmp & 0x01 ? "on" : "off");
  1048. snd_iprintf(buffer, "Ext. Rx to synth : %s\n", tmp & 0x02 ? "on" : "off");
  1049. snd_iprintf(buffer, "MIDI to ext. Tx : %s\n", tmp & 0x04 ? "on" : "off");
  1050. }
  1051. static void __devinit snd_sonicvibes_proc_init(struct sonicvibes * sonic)
  1052. {
  1053. struct snd_info_entry *entry;
  1054. if (! snd_card_proc_new(sonic->card, "sonicvibes", &entry))
  1055. snd_info_set_text_ops(entry, sonic, snd_sonicvibes_proc_read);
  1056. }
  1057. /*
  1058. */
  1059. #ifdef SUPPORT_JOYSTICK
  1060. static struct snd_kcontrol_new snd_sonicvibes_game_control __devinitdata =
  1061. SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
  1062. static int __devinit snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
  1063. {
  1064. struct gameport *gp;
  1065. sonic->gameport = gp = gameport_allocate_port();
  1066. if (!gp) {
  1067. printk(KERN_ERR "sonicvibes: cannot allocate memory for gameport\n");
  1068. return -ENOMEM;
  1069. }
  1070. gameport_set_name(gp, "SonicVibes Gameport");
  1071. gameport_set_phys(gp, "pci%s/gameport0", pci_name(sonic->pci));
  1072. gameport_set_dev_parent(gp, &sonic->pci->dev);
  1073. gp->io = sonic->game_port;
  1074. gameport_register_port(gp);
  1075. snd_ctl_add(sonic->card, snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
  1076. return 0;
  1077. }
  1078. static void snd_sonicvibes_free_gameport(struct sonicvibes *sonic)
  1079. {
  1080. if (sonic->gameport) {
  1081. gameport_unregister_port(sonic->gameport);
  1082. sonic->gameport = NULL;
  1083. }
  1084. }
  1085. #else
  1086. static inline int snd_sonicvibes_create_gameport(struct sonicvibes *sonic) { return -ENOSYS; }
  1087. static inline void snd_sonicvibes_free_gameport(struct sonicvibes *sonic) { }
  1088. #endif
  1089. static int snd_sonicvibes_free(struct sonicvibes *sonic)
  1090. {
  1091. snd_sonicvibes_free_gameport(sonic);
  1092. pci_write_config_dword(sonic->pci, 0x40, sonic->dmaa_port);
  1093. pci_write_config_dword(sonic->pci, 0x48, sonic->dmac_port);
  1094. if (sonic->irq >= 0)
  1095. free_irq(sonic->irq, sonic);
  1096. release_and_free_resource(sonic->res_dmaa);
  1097. release_and_free_resource(sonic->res_dmac);
  1098. pci_release_regions(sonic->pci);
  1099. pci_disable_device(sonic->pci);
  1100. kfree(sonic);
  1101. return 0;
  1102. }
  1103. static int snd_sonicvibes_dev_free(struct snd_device *device)
  1104. {
  1105. struct sonicvibes *sonic = device->device_data;
  1106. return snd_sonicvibes_free(sonic);
  1107. }
  1108. static int __devinit snd_sonicvibes_create(struct snd_card *card,
  1109. struct pci_dev *pci,
  1110. int reverb,
  1111. int mge,
  1112. struct sonicvibes ** rsonic)
  1113. {
  1114. struct sonicvibes *sonic;
  1115. unsigned int dmaa, dmac;
  1116. int err;
  1117. static struct snd_device_ops ops = {
  1118. .dev_free = snd_sonicvibes_dev_free,
  1119. };
  1120. *rsonic = NULL;
  1121. /* enable PCI device */
  1122. if ((err = pci_enable_device(pci)) < 0)
  1123. return err;
  1124. /* check, if we can restrict PCI DMA transfers to 24 bits */
  1125. if (pci_set_dma_mask(pci, DMA_BIT_MASK(24)) < 0 ||
  1126. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(24)) < 0) {
  1127. snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
  1128. pci_disable_device(pci);
  1129. return -ENXIO;
  1130. }
  1131. sonic = kzalloc(sizeof(*sonic), GFP_KERNEL);
  1132. if (sonic == NULL) {
  1133. pci_disable_device(pci);
  1134. return -ENOMEM;
  1135. }
  1136. spin_lock_init(&sonic->reg_lock);
  1137. sonic->card = card;
  1138. sonic->pci = pci;
  1139. sonic->irq = -1;
  1140. if ((err = pci_request_regions(pci, "S3 SonicVibes")) < 0) {
  1141. kfree(sonic);
  1142. pci_disable_device(pci);
  1143. return err;
  1144. }
  1145. sonic->sb_port = pci_resource_start(pci, 0);
  1146. sonic->enh_port = pci_resource_start(pci, 1);
  1147. sonic->synth_port = pci_resource_start(pci, 2);
  1148. sonic->midi_port = pci_resource_start(pci, 3);
  1149. sonic->game_port = pci_resource_start(pci, 4);
  1150. if (request_irq(pci->irq, snd_sonicvibes_interrupt, IRQF_SHARED,
  1151. "S3 SonicVibes", sonic)) {
  1152. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1153. snd_sonicvibes_free(sonic);
  1154. return -EBUSY;
  1155. }
  1156. sonic->irq = pci->irq;
  1157. pci_read_config_dword(pci, 0x40, &dmaa);
  1158. pci_read_config_dword(pci, 0x48, &dmac);
  1159. dmaio &= ~0x0f;
  1160. dmaa &= ~0x0f;
  1161. dmac &= ~0x0f;
  1162. if (!dmaa) {
  1163. dmaa = dmaio;
  1164. dmaio += 0x10;
  1165. snd_printk(KERN_INFO "BIOS did not allocate DDMA channel A i/o, allocated at 0x%x\n", dmaa);
  1166. }
  1167. if (!dmac) {
  1168. dmac = dmaio;
  1169. dmaio += 0x10;
  1170. snd_printk(KERN_INFO "BIOS did not allocate DDMA channel C i/o, allocated at 0x%x\n", dmac);
  1171. }
  1172. pci_write_config_dword(pci, 0x40, dmaa);
  1173. pci_write_config_dword(pci, 0x48, dmac);
  1174. if ((sonic->res_dmaa = request_region(dmaa, 0x10, "S3 SonicVibes DDMA-A")) == NULL) {
  1175. snd_sonicvibes_free(sonic);
  1176. snd_printk(KERN_ERR "unable to grab DDMA-A port at 0x%x-0x%x\n", dmaa, dmaa + 0x10 - 1);
  1177. return -EBUSY;
  1178. }
  1179. if ((sonic->res_dmac = request_region(dmac, 0x10, "S3 SonicVibes DDMA-C")) == NULL) {
  1180. snd_sonicvibes_free(sonic);
  1181. snd_printk(KERN_ERR "unable to grab DDMA-C port at 0x%x-0x%x\n", dmac, dmac + 0x10 - 1);
  1182. return -EBUSY;
  1183. }
  1184. pci_read_config_dword(pci, 0x40, &sonic->dmaa_port);
  1185. pci_read_config_dword(pci, 0x48, &sonic->dmac_port);
  1186. sonic->dmaa_port &= ~0x0f;
  1187. sonic->dmac_port &= ~0x0f;
  1188. pci_write_config_dword(pci, 0x40, sonic->dmaa_port | 9); /* enable + enhanced */
  1189. pci_write_config_dword(pci, 0x48, sonic->dmac_port | 9); /* enable */
  1190. /* ok.. initialize S3 SonicVibes chip */
  1191. outb(SV_RESET, SV_REG(sonic, CONTROL)); /* reset chip */
  1192. udelay(100);
  1193. outb(0, SV_REG(sonic, CONTROL)); /* release reset */
  1194. udelay(100);
  1195. outb(SV_ENHANCED | SV_INTA | (reverb ? SV_REVERB : 0), SV_REG(sonic, CONTROL));
  1196. inb(SV_REG(sonic, STATUS)); /* clear IRQs */
  1197. #if 1
  1198. snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0); /* drive current 16mA */
  1199. #else
  1200. snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0x40); /* drive current 8mA */
  1201. #endif
  1202. snd_sonicvibes_out(sonic, SV_IREG_PC_ENABLE, sonic->enable = 0); /* disable playback & capture */
  1203. outb(sonic->irqmask = ~(SV_DMAA_MASK | SV_DMAC_MASK | SV_UD_MASK), SV_REG(sonic, IRQMASK));
  1204. inb(SV_REG(sonic, STATUS)); /* clear IRQs */
  1205. snd_sonicvibes_out(sonic, SV_IREG_ADC_CLOCK, 0); /* use PLL as clock source */
  1206. snd_sonicvibes_out(sonic, SV_IREG_ANALOG_POWER, 0); /* power up analog parts */
  1207. snd_sonicvibes_out(sonic, SV_IREG_DIGITAL_POWER, 0); /* power up digital parts */
  1208. snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, 8000);
  1209. snd_sonicvibes_out(sonic, SV_IREG_SRS_SPACE, sonic->srs_space = 0x80); /* SRS space off */
  1210. snd_sonicvibes_out(sonic, SV_IREG_SRS_CENTER, sonic->srs_center = 0x00);/* SRS center off */
  1211. snd_sonicvibes_out(sonic, SV_IREG_MPU401, sonic->mpu_switch = 0x05); /* MPU-401 switch */
  1212. snd_sonicvibes_out(sonic, SV_IREG_WAVE_SOURCE, sonic->wave_source = 0x00); /* onboard ROM */
  1213. snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_LOW, (8000 * 65536 / SV_FULLRATE) & 0xff);
  1214. snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_HIGH, ((8000 * 65536 / SV_FULLRATE) >> 8) & 0xff);
  1215. snd_sonicvibes_out(sonic, SV_IREG_LEFT_ADC, mge ? 0xd0 : 0xc0);
  1216. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ADC, 0xc0);
  1217. snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX1, 0x9f);
  1218. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX1, 0x9f);
  1219. snd_sonicvibes_out(sonic, SV_IREG_LEFT_CD, 0x9f);
  1220. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_CD, 0x9f);
  1221. snd_sonicvibes_out(sonic, SV_IREG_LEFT_LINE, 0x9f);
  1222. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_LINE, 0x9f);
  1223. snd_sonicvibes_out(sonic, SV_IREG_MIC, 0x8f);
  1224. snd_sonicvibes_out(sonic, SV_IREG_LEFT_SYNTH, 0x9f);
  1225. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_SYNTH, 0x9f);
  1226. snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX2, 0x9f);
  1227. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX2, 0x9f);
  1228. snd_sonicvibes_out(sonic, SV_IREG_LEFT_ANALOG, 0x9f);
  1229. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ANALOG, 0x9f);
  1230. snd_sonicvibes_out(sonic, SV_IREG_LEFT_PCM, 0xbf);
  1231. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_PCM, 0xbf);
  1232. snd_sonicvibes_out(sonic, SV_IREG_ADC_OUTPUT_CTRL, 0xfc);
  1233. #if 0
  1234. snd_sonicvibes_debug(sonic);
  1235. #endif
  1236. sonic->revision = snd_sonicvibes_in(sonic, SV_IREG_REVISION);
  1237. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sonic, &ops)) < 0) {
  1238. snd_sonicvibes_free(sonic);
  1239. return err;
  1240. }
  1241. snd_sonicvibes_proc_init(sonic);
  1242. snd_card_set_dev(card, &pci->dev);
  1243. *rsonic = sonic;
  1244. return 0;
  1245. }
  1246. /*
  1247. * MIDI section
  1248. */
  1249. static struct snd_kcontrol_new snd_sonicvibes_midi_controls[] __devinitdata = {
  1250. SONICVIBES_SINGLE("SonicVibes Wave Source RAM", 0, SV_IREG_WAVE_SOURCE, 0, 1, 0),
  1251. SONICVIBES_SINGLE("SonicVibes Wave Source RAM+ROM", 0, SV_IREG_WAVE_SOURCE, 1, 1, 0),
  1252. SONICVIBES_SINGLE("SonicVibes Onboard Synth", 0, SV_IREG_MPU401, 0, 1, 0),
  1253. SONICVIBES_SINGLE("SonicVibes External Rx to Synth", 0, SV_IREG_MPU401, 1, 1, 0),
  1254. SONICVIBES_SINGLE("SonicVibes External Tx", 0, SV_IREG_MPU401, 2, 1, 0)
  1255. };
  1256. static int snd_sonicvibes_midi_input_open(struct snd_mpu401 * mpu)
  1257. {
  1258. struct sonicvibes *sonic = mpu->private_data;
  1259. outb(sonic->irqmask &= ~SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
  1260. return 0;
  1261. }
  1262. static void snd_sonicvibes_midi_input_close(struct snd_mpu401 * mpu)
  1263. {
  1264. struct sonicvibes *sonic = mpu->private_data;
  1265. outb(sonic->irqmask |= SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
  1266. }
  1267. static int __devinit snd_sonicvibes_midi(struct sonicvibes * sonic,
  1268. struct snd_rawmidi *rmidi)
  1269. {
  1270. struct snd_mpu401 * mpu = rmidi->private_data;
  1271. struct snd_card *card = sonic->card;
  1272. struct snd_rawmidi_str *dir;
  1273. unsigned int idx;
  1274. int err;
  1275. mpu->private_data = sonic;
  1276. mpu->open_input = snd_sonicvibes_midi_input_open;
  1277. mpu->close_input = snd_sonicvibes_midi_input_close;
  1278. dir = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
  1279. for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_midi_controls); idx++)
  1280. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_sonicvibes_midi_controls[idx], sonic))) < 0)
  1281. return err;
  1282. return 0;
  1283. }
  1284. static int __devinit snd_sonic_probe(struct pci_dev *pci,
  1285. const struct pci_device_id *pci_id)
  1286. {
  1287. static int dev;
  1288. struct snd_card *card;
  1289. struct sonicvibes *sonic;
  1290. struct snd_rawmidi *midi_uart;
  1291. struct snd_opl3 *opl3;
  1292. int idx, err;
  1293. if (dev >= SNDRV_CARDS)
  1294. return -ENODEV;
  1295. if (!enable[dev]) {
  1296. dev++;
  1297. return -ENOENT;
  1298. }
  1299. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  1300. if (err < 0)
  1301. return err;
  1302. for (idx = 0; idx < 5; idx++) {
  1303. if (pci_resource_start(pci, idx) == 0 ||
  1304. !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
  1305. snd_card_free(card);
  1306. return -ENODEV;
  1307. }
  1308. }
  1309. if ((err = snd_sonicvibes_create(card, pci,
  1310. reverb[dev] ? 1 : 0,
  1311. mge[dev] ? 1 : 0,
  1312. &sonic)) < 0) {
  1313. snd_card_free(card);
  1314. return err;
  1315. }
  1316. strcpy(card->driver, "SonicVibes");
  1317. strcpy(card->shortname, "S3 SonicVibes");
  1318. sprintf(card->longname, "%s rev %i at 0x%llx, irq %i",
  1319. card->shortname,
  1320. sonic->revision,
  1321. (unsigned long long)pci_resource_start(pci, 1),
  1322. sonic->irq);
  1323. if ((err = snd_sonicvibes_pcm(sonic, 0, NULL)) < 0) {
  1324. snd_card_free(card);
  1325. return err;
  1326. }
  1327. if ((err = snd_sonicvibes_mixer(sonic)) < 0) {
  1328. snd_card_free(card);
  1329. return err;
  1330. }
  1331. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_SONICVIBES,
  1332. sonic->midi_port, MPU401_INFO_INTEGRATED,
  1333. sonic->irq, 0,
  1334. &midi_uart)) < 0) {
  1335. snd_card_free(card);
  1336. return err;
  1337. }
  1338. snd_sonicvibes_midi(sonic, midi_uart);
  1339. if ((err = snd_opl3_create(card, sonic->synth_port,
  1340. sonic->synth_port + 2,
  1341. OPL3_HW_OPL3_SV, 1, &opl3)) < 0) {
  1342. snd_card_free(card);
  1343. return err;
  1344. }
  1345. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  1346. snd_card_free(card);
  1347. return err;
  1348. }
  1349. snd_sonicvibes_create_gameport(sonic);
  1350. if ((err = snd_card_register(card)) < 0) {
  1351. snd_card_free(card);
  1352. return err;
  1353. }
  1354. pci_set_drvdata(pci, card);
  1355. dev++;
  1356. return 0;
  1357. }
  1358. static void __devexit snd_sonic_remove(struct pci_dev *pci)
  1359. {
  1360. snd_card_free(pci_get_drvdata(pci));
  1361. pci_set_drvdata(pci, NULL);
  1362. }
  1363. static struct pci_driver driver = {
  1364. .name = "S3 SonicVibes",
  1365. .id_table = snd_sonic_ids,
  1366. .probe = snd_sonic_probe,
  1367. .remove = __devexit_p(snd_sonic_remove),
  1368. };
  1369. static int __init alsa_card_sonicvibes_init(void)
  1370. {
  1371. return pci_register_driver(&driver);
  1372. }
  1373. static void __exit alsa_card_sonicvibes_exit(void)
  1374. {
  1375. pci_unregister_driver(&driver);
  1376. }
  1377. module_init(alsa_card_sonicvibes_init)
  1378. module_exit(alsa_card_sonicvibes_exit)