patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / isa / cmi8330.c
1 /*
2  *  Driver for C-Media's CMI8330 soundcards.
3  *  Copyright (c) by George Talusan <gstalusan@uwaterloo.ca>
4  *    http://www.undergrad.math.uwaterloo.ca/~gstalusa
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 /*
23  * NOTES
24  *
25  *  The extended registers contain mixer settings which are largely
26  *  untapped for the time being.
27  *
28  *  MPU401 and SPDIF are not supported yet.  I don't have the hardware
29  *  to aid in coding and testing, so I won't bother.
30  *
31  *  To quickly load the module,
32  *
33  *  modprobe -a snd-cmi8330 sbport=0x220 sbirq=5 sbdma8=1
34  *    sbdma16=5 wssport=0x530 wssirq=11 wssdma=0
35  *
36  *  This card has two mixers and two PCM devices.  I've cheesed it such
37  *  that recording and playback can be done through the same device.
38  *  The driver "magically" routes the capturing to the AD1848 codec,
39  *  and playback to the SB16 codec.  This allows for full-duplex mode
40  *  to some extent.
41  *  The utilities in alsa-utils are aware of both devices, so passing
42  *  the appropriate parameters to amixer and alsactl will give you
43  *  full control over both mixers.
44  */
45
46 #include <sound/driver.h>
47 #include <linux/init.h>
48 #include <linux/slab.h>
49 #include <linux/pnp.h>
50 #include <linux/moduleparam.h>
51 #include <sound/core.h>
52 #include <sound/ad1848.h>
53 #include <sound/sb.h>
54 #include <sound/initval.h>
55
56 /*
57  */
58 /* #define ENABLE_SB_MIXER */
59 #define PLAYBACK_ON_SB
60
61 /*
62  */
63 MODULE_AUTHOR("George Talusan <gstalusan@uwaterloo.ca>");
64 MODULE_DESCRIPTION("C-Media CMI8330");
65 MODULE_LICENSE("GPL");
66 MODULE_CLASSES("{sound}");
67 MODULE_DEVICES("{{C-Media,CMI8330,isapnp:{CMI0001,@@@0001,@X@0001}}}");
68
69 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
70 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
71 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
72 #ifdef CONFIG_PNP
73 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
74 #endif
75 static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
76 static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
77 static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
78 static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
79 static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
80 static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
81 static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
82 static int boot_devs;
83
84 module_param_array(index, int, boot_devs, 0444);
85 MODULE_PARM_DESC(index, "Index value for CMI8330 soundcard.");
86 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
87 module_param_array(id, charp, boot_devs, 0444);
88 MODULE_PARM_DESC(id, "ID string  for CMI8330 soundcard.");
89 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
90 module_param_array(enable, bool, boot_devs, 0444);
91 MODULE_PARM_DESC(enable, "Enable CMI8330 soundcard.");
92 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
93 #ifdef CONFIG_PNP
94 module_param_array(isapnp, bool, boot_devs, 0444);
95 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
96 MODULE_PARM_SYNTAX(isapnp, SNDRV_ISAPNP_DESC);
97 #endif
98
99 module_param_array(sbport, long, boot_devs, 0444);
100 MODULE_PARM_DESC(sbport, "Port # for CMI8330 SB driver.");
101 MODULE_PARM_SYNTAX(sbport, SNDRV_ENABLED ",allows:{{0x220,0x280,0x20}},prefers:{0x220},base:16,dialog:list");
102 module_param_array(sbirq, int, boot_devs, 0444);
103 MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330 SB driver.");
104 MODULE_PARM_SYNTAX(sbirq, SNDRV_ENABLED ",allows:{{5},{7},{9},{10},{11},{12}},prefers:{5},dialog:list");
105 module_param_array(sbdma8, int, boot_devs, 0444);
106 MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330 SB driver.");
107 MODULE_PARM_SYNTAX(sbdma8, SNDRV_DMA8_DESC ",prefers:{1}");
108 module_param_array(sbdma16, int, boot_devs, 0444);
109 MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330 SB driver.");
110 MODULE_PARM_SYNTAX(sbdma16, SNDRV_ENABLED ",allows:{{5},{7}},prefers:{5},dialog:list");
111
112 module_param_array(wssport, long, boot_devs, 0444);
113 MODULE_PARM_DESC(wssport, "Port # for CMI8330 WSS driver.");
114 MODULE_PARM_SYNTAX(wssport, SNDRV_ENABLED ",allows:{{0x530},{0xe80,0xf40,0xc0}},prefers:{0x530},base:16,dialog:list");
115 module_param_array(wssirq, int, boot_devs, 0444);
116 MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330 WSS driver.");
117 MODULE_PARM_SYNTAX(wssirq, SNDRV_ENABLED ",allows:{{5},{7},{9},{10},{11},{12}},prefers:{11},dialog:list");
118 module_param_array(wssdma, int, boot_devs, 0444);
119 MODULE_PARM_DESC(wssdma, "DMA for CMI8330 WSS driver.");
120 MODULE_PARM_SYNTAX(wssdma, SNDRV_DMA8_DESC ",prefers:{0}");
121
122 #define CMI8330_RMUX3D    16
123 #define CMI8330_MUTEMUX   17
124 #define CMI8330_OUTPUTVOL 18
125 #define CMI8330_MASTVOL   19
126 #define CMI8330_LINVOL    20
127 #define CMI8330_CDINVOL   21
128 #define CMI8330_WAVVOL    22
129 #define CMI8330_RECMUX    23
130 #define CMI8330_WAVGAIN   24
131 #define CMI8330_LINGAIN   25
132 #define CMI8330_CDINGAIN  26
133
134 static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
135 {
136         0x40,                   /* 16 - recording mux (SB-mixer-enabled) */
137 #ifdef ENABLE_SB_MIXER
138         0x40,                   /* 17 - mute mux (Mode2) */
139 #else
140         0x0,                    /* 17 - mute mux */
141 #endif
142         0x0,                    /* 18 - vol */
143         0x0,                    /* 19 - master volume */
144         0x0,                    /* 20 - line-in volume */
145         0x0,                    /* 21 - cd-in volume */
146         0x0,                    /* 22 - wave volume */
147         0x0,                    /* 23 - mute/rec mux */
148         0x0,                    /* 24 - wave rec gain */
149         0x0,                    /* 25 - line-in rec gain */
150         0x0                     /* 26 - cd-in rec gain */
151 };
152
153 typedef int (*snd_pcm_open_callback_t)(snd_pcm_substream_t *);
154
155 struct snd_cmi8330 {
156 #ifdef CONFIG_PNP
157         struct pnp_dev *cap;
158         struct pnp_dev *play;
159 #endif
160         snd_card_t *card;
161         ad1848_t *wss;
162         sb_t *sb;
163
164         snd_pcm_t *pcm;
165         struct snd_cmi8330_stream {
166                 snd_pcm_ops_t ops;
167                 snd_pcm_open_callback_t open;
168                 void *private_data; /* sb or wss */
169         } streams[2];
170 };
171
172 static snd_card_t *snd_cmi8330_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
173
174 #ifdef CONFIG_PNP
175
176 static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
177         { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" } } },
178         { .id = "" }
179 };
180
181 MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
182
183 #endif
184
185
186 static struct ad1848_mix_elem snd_cmi8330_controls[] __initdata = {
187 AD1848_DOUBLE("Master Playback Volume", 0, CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
188 AD1848_SINGLE("Loud Playback Switch", 0, CMI8330_MUTEMUX, 6, 1, 1),
189 AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
190 AD1848_DOUBLE("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1),
191 AD1848_DOUBLE("Line Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0),
192 AD1848_DOUBLE("Line Playback Volume", 0, CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0),
193 AD1848_DOUBLE("Line Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0),
194 AD1848_DOUBLE("Line Capture Volume", 0, CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0),
195 AD1848_DOUBLE("CD Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0),
196 AD1848_DOUBLE("CD Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0),
197 AD1848_DOUBLE("CD Playback Volume", 0, CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0),
198 AD1848_DOUBLE("CD Capture Volume", 0, CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0),
199 AD1848_SINGLE("Mic Playback Switch", 0, CMI8330_MUTEMUX, 0, 1, 0),
200 AD1848_SINGLE("Mic Playback Volume", 0, CMI8330_OUTPUTVOL, 0, 7, 0),
201 AD1848_SINGLE("Mic Capture Switch", 0, CMI8330_RMUX3D, 0, 1, 0),
202 AD1848_SINGLE("Mic Capture Volume", 0, CMI8330_OUTPUTVOL, 5, 7, 0),
203 AD1848_DOUBLE("Wavetable Playback Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0),
204 AD1848_DOUBLE("Wavetable Playback Volume", 0, CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0),
205 AD1848_DOUBLE("Wavetable Capture Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0),
206 AD1848_DOUBLE("Wavetable Capture Volume", 0, CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0),
207 AD1848_SINGLE("3D Control - Switch", 0, CMI8330_RMUX3D, 5, 1, 1),
208 AD1848_SINGLE("PC Speaker Playback Volume", 0, CMI8330_OUTPUTVOL, 3, 3, 0),
209 AD1848_SINGLE("FM Playback Switch", 0, CMI8330_RECMUX, 3, 1, 1),
210 AD1848_SINGLE("IEC958 Input Capture Switch", 0, CMI8330_RMUX3D, 7, 1, 1),
211 AD1848_SINGLE("IEC958 Input Playback Switch", 0, CMI8330_MUTEMUX, 7, 1, 1),
212 };
213
214 #ifdef ENABLE_SB_MIXER
215 static struct sbmix_elem cmi8330_sb_mixers[] __initdata = {
216 SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
217 SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
218 SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
219 SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31),
220 SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31),
221 SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1),
222 SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31),
223 SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1),
224 SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31),
225 SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1),
226 SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
227 SB_SINGLE("SB PC Speaker Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
228 SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3),
229 SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3),
230 SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
231 };
232
233 static unsigned char cmi8330_sb_init_values[][2] __initdata = {
234         { SB_DSP4_MASTER_DEV + 0, 0 },
235         { SB_DSP4_MASTER_DEV + 1, 0 },
236         { SB_DSP4_PCM_DEV + 0, 0 },
237         { SB_DSP4_PCM_DEV + 1, 0 },
238         { SB_DSP4_SYNTH_DEV + 0, 0 },
239         { SB_DSP4_SYNTH_DEV + 1, 0 },
240         { SB_DSP4_INPUT_LEFT, 0 },
241         { SB_DSP4_INPUT_RIGHT, 0 },
242         { SB_DSP4_OUTPUT_SW, 0 },
243         { SB_DSP4_SPEAKER_DEV, 0 },
244 };
245
246
247 static int __devinit cmi8330_add_sb_mixers(sb_t *chip)
248 {
249         int idx, err;
250         unsigned long flags;
251
252         spin_lock_irqsave(&chip->mixer_lock, flags);
253         snd_sbmixer_write(chip, 0x00, 0x00);            /* mixer reset */
254         spin_unlock_irqrestore(&chip->mixer_lock, flags);
255
256         /* mute and zero volume channels */
257         for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) {
258                 spin_lock_irqsave(&chip->mixer_lock, flags);
259                 snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0],
260                                   cmi8330_sb_init_values[idx][1]);
261                 spin_unlock_irqrestore(&chip->mixer_lock, flags);
262         }
263
264         for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) {
265                 if ((err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx])) < 0)
266                         return err;
267         }
268         return 0;
269 }
270 #endif
271
272 static int __devinit snd_cmi8330_mixer(snd_card_t *card, struct snd_cmi8330 *acard)
273 {
274         unsigned int idx;
275         int err;
276
277         strcpy(card->mixername, "CMI8330/C3D");
278
279         for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) {
280                 if ((err = snd_ad1848_add_ctl_elem(acard->wss, &snd_cmi8330_controls[idx])) < 0)
281                         return err;
282         }
283
284 #ifdef ENABLE_SB_MIXER
285         if ((err = cmi8330_add_sb_mixers(acard->sb)) < 0)
286                 return err;
287 #endif
288         return 0;
289 }
290
291 #ifdef CONFIG_PNP
292 static int __devinit snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
293                                      struct pnp_card_link *card,
294                                      const struct pnp_card_device_id *id)
295 {
296         struct pnp_dev *pdev;
297         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
298         int err;
299
300         acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL);
301         if (acard->cap == NULL) {
302                 kfree(cfg);
303                 return -EBUSY;
304         }
305         acard->play = pnp_request_card_device(card, id->devs[1].id, NULL);
306         if (acard->play == NULL) {
307                 kfree(cfg);
308                 return -EBUSY;
309         }
310
311         pdev = acard->cap;
312         pnp_init_resource_table(cfg);
313         /* allocate AD1848 resources */
314         if (wssport[dev] != SNDRV_AUTO_PORT)
315                 pnp_resource_change(&cfg->port_resource[0], wssport[dev], 8);
316         if (wssdma[dev] != SNDRV_AUTO_DMA)
317                 pnp_resource_change(&cfg->dma_resource[0], wssdma[dev], 1);
318         if (wssirq[dev] != SNDRV_AUTO_IRQ)
319                 pnp_resource_change(&cfg->irq_resource[0], wssirq[dev], 1);
320
321         err = pnp_manual_config_dev(pdev, cfg, 0);
322         if (err < 0)
323                 snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP manual resources are invalid, using auto config\n");
324         err = pnp_activate_dev(pdev);
325         if (err < 0) {
326                 snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP configure failure\n");
327                 kfree(cfg);
328                 return -EBUSY;
329         }
330         wssport[dev] = pnp_port_start(pdev, 0);
331         wssdma[dev] = pnp_dma(pdev, 0);
332         wssirq[dev] = pnp_irq(pdev, 0);
333
334         /* allocate SB16 resources */
335         pdev = acard->play;
336         pnp_init_resource_table(cfg);
337         if (sbport[dev] != SNDRV_AUTO_PORT)
338                 pnp_resource_change(&cfg->port_resource[0], sbport[dev], 16);
339         if (sbdma8[dev] != SNDRV_AUTO_DMA)
340                 pnp_resource_change(&cfg->dma_resource[0], sbdma8[dev], 1);
341         if (sbdma16[dev] != SNDRV_AUTO_DMA)
342                 pnp_resource_change(&cfg->dma_resource[1], sbdma16[dev], 1);
343         if (sbirq[dev] != SNDRV_AUTO_IRQ)
344                 pnp_resource_change(&cfg->irq_resource[0], sbirq[dev], 1);
345
346         err = pnp_manual_config_dev(pdev, cfg, 0);
347         if (err < 0)
348                 snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP manual resources are invalid, using auto config\n");
349         err = pnp_activate_dev(pdev);
350         if (err < 0) {
351                 snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP configure failure\n");
352                 kfree(cfg);
353                 return -EBUSY;
354         }
355         sbport[dev] = pnp_port_start(pdev, 0);
356         sbdma8[dev] = pnp_dma(pdev, 0);
357         sbdma16[dev] = pnp_dma(pdev, 1);
358         sbirq[dev] = pnp_irq(pdev, 0);
359
360         kfree(cfg);
361         return 0;
362 }
363 #endif
364
365 /*
366  * PCM interface
367  *
368  * since we call the different chip interfaces for playback and capture
369  * directions, we need a trick.
370  *
371  * - copy the ops for each direction into a local record.
372  * - replace the open callback with the new one, which replaces the
373  *   substream->private_data with the corresponding chip instance
374  *   and calls again the original open callback of the chip.
375  *
376  */
377
378 #ifdef PLAYBACK_ON_SB
379 #define CMI_SB_STREAM   SNDRV_PCM_STREAM_PLAYBACK
380 #define CMI_AD_STREAM   SNDRV_PCM_STREAM_CAPTURE
381 #else
382 #define CMI_SB_STREAM   SNDRV_PCM_STREAM_CAPTURE
383 #define CMI_AD_STREAM   SNDRV_PCM_STREAM_PLAYBACK
384 #endif
385
386 static int snd_cmi8330_playback_open(snd_pcm_substream_t * substream)
387 {
388         struct snd_cmi8330 *chip = (struct snd_cmi8330 *)_snd_pcm_substream_chip(substream);
389
390         /* replace the private_data and call the original open callback */
391         substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data;
392         return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
393 }
394
395 static int snd_cmi8330_capture_open(snd_pcm_substream_t * substream)
396 {
397         struct snd_cmi8330 *chip = (struct snd_cmi8330 *)_snd_pcm_substream_chip(substream);
398
399         /* replace the private_data and call the original open callback */
400         substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data;
401         return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
402 }
403
404 static void snd_cmi8330_pcm_free(snd_pcm_t *pcm)
405 {
406         snd_pcm_lib_preallocate_free_for_all(pcm);
407 }
408
409 static int __devinit snd_cmi8330_pcm(snd_card_t *card, struct snd_cmi8330 *chip)
410 {
411         snd_pcm_t *pcm;
412         const snd_pcm_ops_t *ops;
413         int err;
414         static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
415                 snd_cmi8330_playback_open,
416                 snd_cmi8330_capture_open
417         };
418
419         if ((err = snd_pcm_new(card, "CMI8330", 0, 1, 1, &pcm)) < 0)
420                 return err;
421         strcpy(pcm->name, "CMI8330");
422         pcm->private_data = chip;
423         pcm->private_free = snd_cmi8330_pcm_free;
424         
425         /* SB16 */
426         ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
427         chip->streams[CMI_SB_STREAM].ops = *ops;
428         chip->streams[CMI_SB_STREAM].open = ops->open;
429         chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM];
430         chip->streams[CMI_SB_STREAM].private_data = chip->sb;
431
432         /* AD1848 */
433         ops = snd_ad1848_get_pcm_ops(CMI_AD_STREAM);
434         chip->streams[CMI_AD_STREAM].ops = *ops;
435         chip->streams[CMI_AD_STREAM].open = ops->open;
436         chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM];
437         chip->streams[CMI_AD_STREAM].private_data = chip->wss;
438
439         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops);
440         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops);
441
442         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
443                                               snd_dma_isa_data(),
444                                               64*1024, 128*1024);
445         chip->pcm = pcm;
446
447         return 0;
448 }
449
450
451 /*
452  */
453
454 static int __devinit snd_cmi8330_probe(int dev,
455                                        struct pnp_card_link *pcard,
456                                        const struct pnp_card_device_id *pid)
457 {
458         snd_card_t *card;
459         struct snd_cmi8330 *acard;
460         unsigned long flags;
461         int i, err;
462
463 #ifdef CONFIG_PNP
464         if (!isapnp[dev]) {
465 #endif
466                 if (wssport[dev] == SNDRV_AUTO_PORT) {
467                         snd_printk("specify wssport\n");
468                         return -EINVAL;
469                 }
470                 if (sbport[dev] == SNDRV_AUTO_PORT) {
471                         snd_printk("specify sbport\n");
472                         return -EINVAL;
473                 }
474 #ifdef CONFIG_PNP
475         }
476 #endif
477         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
478                             sizeof(struct snd_cmi8330));
479         if (card == NULL) {
480                 snd_printk("could not get a new card\n");
481                 return -ENOMEM;
482         }
483         acard = (struct snd_cmi8330 *)card->private_data;
484         acard->card = card;
485
486 #ifdef CONFIG_PNP
487         if (isapnp[dev]) {
488                 if ((err = snd_cmi8330_pnp(dev, acard, pcard, pid)) < 0) {
489                         snd_printk("PnP detection failed\n");
490                         snd_card_free(card);
491                         return err;
492                 }
493                 snd_card_set_dev(card, &pcard->card->dev);
494         }
495 #endif
496
497         if ((err = snd_ad1848_create(card,
498                                      wssport[dev] + 4,
499                                      wssirq[dev],
500                                      wssdma[dev],
501                                      AD1848_HW_DETECT,
502                                      &acard->wss)) < 0) {
503                 snd_printk("(AD1848) device busy??\n");
504                 snd_card_free(card);
505                 return err;
506         }
507         if (acard->wss->hardware != AD1848_HW_CMI8330) {
508                 snd_printk("(AD1848) not found during probe\n");
509                 snd_card_free(card);
510                 return -ENODEV;
511         }
512
513         if ((err = snd_sbdsp_create(card, sbport[dev],
514                                     sbirq[dev],
515                                     snd_sb16dsp_interrupt,
516                                     sbdma8[dev],
517                                     sbdma16[dev],
518                                     SB_HW_AUTO, &acard->sb)) < 0) {
519                 snd_printk("(SB16) device busy??\n");
520                 snd_card_free(card);
521                 return err;
522         }
523         if (acard->sb->hardware != SB_HW_16) {
524                 snd_printk("(SB16) not found during probe\n");
525                 snd_card_free(card);
526                 return -ENODEV;
527         }
528
529         spin_lock_irqsave(&acard->wss->reg_lock, flags);
530         snd_ad1848_out(acard->wss, AD1848_MISC_INFO, 0x40); /* switch on MODE2 */
531         for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
532                 snd_ad1848_out(acard->wss, i, snd_cmi8330_image[i - CMI8330_RMUX3D]);
533         spin_unlock_irqrestore(&acard->wss->reg_lock, flags);
534
535         if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
536                 snd_printk("failed to create mixers\n");
537                 snd_card_free(card);
538                 return err;
539         }
540
541         if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
542                 snd_printk("failed to create pcms\n");
543                 snd_card_free(card);
544                 return err;
545         }
546
547         strcpy(card->driver, "CMI8330/C3D");
548         strcpy(card->shortname, "C-Media CMI8330/C3D");
549         sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
550                 card->shortname,
551                 acard->wss->port,
552                 wssirq[dev],
553                 wssdma[dev]);
554
555         if ((err = snd_card_register(card)) < 0) {
556                 snd_card_free(card);
557                 return err;
558         }
559
560         if (pcard)
561                 pnp_set_card_drvdata(pcard, card);
562         else
563                 snd_cmi8330_legacy[dev] = card;
564         return 0;
565 }
566
567 #ifdef CONFIG_PNP
568 static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *card,
569                                             const struct pnp_card_device_id *id)
570 {
571         static int dev;
572         int res;
573
574         for ( ; dev < SNDRV_CARDS; dev++) {
575                 if (!enable[dev] || !isapnp[dev])
576                         continue;
577                 res = snd_cmi8330_probe(dev, card, id);
578                 if (res < 0)
579                         return res;
580                 dev++;
581                 return 0;
582         }
583         return -ENODEV;
584 }
585
586 static void __devexit snd_cmi8330_pnp_remove(struct pnp_card_link * pcard)
587 {
588         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
589
590         snd_card_disconnect(card);
591         snd_card_free_in_thread(card);
592 }
593
594 static struct pnp_card_driver cmi8330_pnpc_driver = {
595         .flags = PNP_DRIVER_RES_DISABLE,
596         .name = "cmi8330",
597         .id_table = snd_cmi8330_pnpids,
598         .probe = snd_cmi8330_pnp_detect,
599         .remove = __devexit_p(snd_cmi8330_pnp_remove),
600 };
601 #endif /* CONFIG_PNP */
602
603 static int __init alsa_card_cmi8330_init(void)
604 {
605         int dev, cards = 0;
606
607         for (dev = 0; dev < SNDRV_CARDS; dev++) {
608                 if (!enable[dev])
609                         continue;
610 #ifdef CONFIG_PNP
611                 if (isapnp[dev])
612                         continue;
613 #endif
614                 if (snd_cmi8330_probe(dev, NULL, NULL) >= 0)
615                         cards++;
616         }
617 #ifdef CONFIG_PNP
618         cards += pnp_register_card_driver(&cmi8330_pnpc_driver);
619 #endif
620
621         if (!cards) {
622 #ifdef CONFIG_PNP
623                 pnp_unregister_card_driver(&cmi8330_pnpc_driver);
624 #endif
625 #ifdef MODULE
626                 snd_printk(KERN_ERR "CMI8330 not found or device busy\n");
627 #endif
628                 return -ENODEV;
629         }
630         return 0;
631 }
632
633 static void __exit alsa_card_cmi8330_exit(void)
634 {
635         int i;
636
637 #ifdef CONFIG_PNP
638         /* PnP cards first */
639         pnp_unregister_card_driver(&cmi8330_pnpc_driver);
640 #endif
641         for (i = 0; i < SNDRV_CARDS; i++)
642                 snd_card_free(snd_cmi8330_legacy[i]);
643 }
644
645 module_init(alsa_card_cmi8330_init)
646 module_exit(alsa_card_cmi8330_exit)