vserver 1.9.3
[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_SUPPORTED_DEVICE("{{C-Media,CMI8330,isapnp:{CMI0001,@@@0001,@X@0001}}}");
67
68 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
69 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
70 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
71 #ifdef CONFIG_PNP
72 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
73 #endif
74 static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
75 static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
76 static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
77 static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
78 static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
79 static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
80 static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
81 static int boot_devs;
82
83 module_param_array(index, int, boot_devs, 0444);
84 MODULE_PARM_DESC(index, "Index value for CMI8330 soundcard.");
85 module_param_array(id, charp, boot_devs, 0444);
86 MODULE_PARM_DESC(id, "ID string  for CMI8330 soundcard.");
87 module_param_array(enable, bool, boot_devs, 0444);
88 MODULE_PARM_DESC(enable, "Enable CMI8330 soundcard.");
89 #ifdef CONFIG_PNP
90 module_param_array(isapnp, bool, boot_devs, 0444);
91 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
92 #endif
93
94 module_param_array(sbport, long, boot_devs, 0444);
95 MODULE_PARM_DESC(sbport, "Port # for CMI8330 SB driver.");
96 module_param_array(sbirq, int, boot_devs, 0444);
97 MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330 SB driver.");
98 module_param_array(sbdma8, int, boot_devs, 0444);
99 MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330 SB driver.");
100 module_param_array(sbdma16, int, boot_devs, 0444);
101 MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330 SB driver.");
102
103 module_param_array(wssport, long, boot_devs, 0444);
104 MODULE_PARM_DESC(wssport, "Port # for CMI8330 WSS driver.");
105 module_param_array(wssirq, int, boot_devs, 0444);
106 MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330 WSS driver.");
107 module_param_array(wssdma, int, boot_devs, 0444);
108 MODULE_PARM_DESC(wssdma, "DMA for CMI8330 WSS driver.");
109
110 #define CMI8330_RMUX3D    16
111 #define CMI8330_MUTEMUX   17
112 #define CMI8330_OUTPUTVOL 18
113 #define CMI8330_MASTVOL   19
114 #define CMI8330_LINVOL    20
115 #define CMI8330_CDINVOL   21
116 #define CMI8330_WAVVOL    22
117 #define CMI8330_RECMUX    23
118 #define CMI8330_WAVGAIN   24
119 #define CMI8330_LINGAIN   25
120 #define CMI8330_CDINGAIN  26
121
122 static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
123 {
124         0x40,                   /* 16 - recording mux (SB-mixer-enabled) */
125 #ifdef ENABLE_SB_MIXER
126         0x40,                   /* 17 - mute mux (Mode2) */
127 #else
128         0x0,                    /* 17 - mute mux */
129 #endif
130         0x0,                    /* 18 - vol */
131         0x0,                    /* 19 - master volume */
132         0x0,                    /* 20 - line-in volume */
133         0x0,                    /* 21 - cd-in volume */
134         0x0,                    /* 22 - wave volume */
135         0x0,                    /* 23 - mute/rec mux */
136         0x0,                    /* 24 - wave rec gain */
137         0x0,                    /* 25 - line-in rec gain */
138         0x0                     /* 26 - cd-in rec gain */
139 };
140
141 typedef int (*snd_pcm_open_callback_t)(snd_pcm_substream_t *);
142
143 struct snd_cmi8330 {
144 #ifdef CONFIG_PNP
145         struct pnp_dev *cap;
146         struct pnp_dev *play;
147 #endif
148         snd_card_t *card;
149         ad1848_t *wss;
150         sb_t *sb;
151
152         snd_pcm_t *pcm;
153         struct snd_cmi8330_stream {
154                 snd_pcm_ops_t ops;
155                 snd_pcm_open_callback_t open;
156                 void *private_data; /* sb or wss */
157         } streams[2];
158 };
159
160 static snd_card_t *snd_cmi8330_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
161
162 #ifdef CONFIG_PNP
163
164 static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
165         { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" } } },
166         { .id = "" }
167 };
168
169 MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
170
171 #endif
172
173
174 static struct ad1848_mix_elem snd_cmi8330_controls[] __initdata = {
175 AD1848_DOUBLE("Master Playback Volume", 0, CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
176 AD1848_SINGLE("Loud Playback Switch", 0, CMI8330_MUTEMUX, 6, 1, 1),
177 AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
178 AD1848_DOUBLE("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1),
179 AD1848_DOUBLE("Line Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0),
180 AD1848_DOUBLE("Line Playback Volume", 0, CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0),
181 AD1848_DOUBLE("Line Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0),
182 AD1848_DOUBLE("Line Capture Volume", 0, CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0),
183 AD1848_DOUBLE("CD Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0),
184 AD1848_DOUBLE("CD Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0),
185 AD1848_DOUBLE("CD Playback Volume", 0, CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0),
186 AD1848_DOUBLE("CD Capture Volume", 0, CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0),
187 AD1848_SINGLE("Mic Playback Switch", 0, CMI8330_MUTEMUX, 0, 1, 0),
188 AD1848_SINGLE("Mic Playback Volume", 0, CMI8330_OUTPUTVOL, 0, 7, 0),
189 AD1848_SINGLE("Mic Capture Switch", 0, CMI8330_RMUX3D, 0, 1, 0),
190 AD1848_SINGLE("Mic Capture Volume", 0, CMI8330_OUTPUTVOL, 5, 7, 0),
191 AD1848_DOUBLE("Wavetable Playback Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0),
192 AD1848_DOUBLE("Wavetable Playback Volume", 0, CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0),
193 AD1848_DOUBLE("Wavetable Capture Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0),
194 AD1848_DOUBLE("Wavetable Capture Volume", 0, CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0),
195 AD1848_SINGLE("3D Control - Switch", 0, CMI8330_RMUX3D, 5, 1, 1),
196 AD1848_SINGLE("PC Speaker Playback Volume", 0, CMI8330_OUTPUTVOL, 3, 3, 0),
197 AD1848_SINGLE("FM Playback Switch", 0, CMI8330_RECMUX, 3, 1, 1),
198 AD1848_SINGLE("IEC958 Input Capture Switch", 0, CMI8330_RMUX3D, 7, 1, 1),
199 AD1848_SINGLE("IEC958 Input Playback Switch", 0, CMI8330_MUTEMUX, 7, 1, 1),
200 };
201
202 #ifdef ENABLE_SB_MIXER
203 static struct sbmix_elem cmi8330_sb_mixers[] __initdata = {
204 SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
205 SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
206 SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
207 SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31),
208 SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31),
209 SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1),
210 SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31),
211 SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1),
212 SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31),
213 SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1),
214 SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
215 SB_SINGLE("SB PC Speaker Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
216 SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3),
217 SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3),
218 SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
219 };
220
221 static unsigned char cmi8330_sb_init_values[][2] __initdata = {
222         { SB_DSP4_MASTER_DEV + 0, 0 },
223         { SB_DSP4_MASTER_DEV + 1, 0 },
224         { SB_DSP4_PCM_DEV + 0, 0 },
225         { SB_DSP4_PCM_DEV + 1, 0 },
226         { SB_DSP4_SYNTH_DEV + 0, 0 },
227         { SB_DSP4_SYNTH_DEV + 1, 0 },
228         { SB_DSP4_INPUT_LEFT, 0 },
229         { SB_DSP4_INPUT_RIGHT, 0 },
230         { SB_DSP4_OUTPUT_SW, 0 },
231         { SB_DSP4_SPEAKER_DEV, 0 },
232 };
233
234
235 static int __devinit cmi8330_add_sb_mixers(sb_t *chip)
236 {
237         int idx, err;
238         unsigned long flags;
239
240         spin_lock_irqsave(&chip->mixer_lock, flags);
241         snd_sbmixer_write(chip, 0x00, 0x00);            /* mixer reset */
242         spin_unlock_irqrestore(&chip->mixer_lock, flags);
243
244         /* mute and zero volume channels */
245         for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) {
246                 spin_lock_irqsave(&chip->mixer_lock, flags);
247                 snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0],
248                                   cmi8330_sb_init_values[idx][1]);
249                 spin_unlock_irqrestore(&chip->mixer_lock, flags);
250         }
251
252         for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) {
253                 if ((err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx])) < 0)
254                         return err;
255         }
256         return 0;
257 }
258 #endif
259
260 static int __devinit snd_cmi8330_mixer(snd_card_t *card, struct snd_cmi8330 *acard)
261 {
262         unsigned int idx;
263         int err;
264
265         strcpy(card->mixername, "CMI8330/C3D");
266
267         for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) {
268                 if ((err = snd_ad1848_add_ctl_elem(acard->wss, &snd_cmi8330_controls[idx])) < 0)
269                         return err;
270         }
271
272 #ifdef ENABLE_SB_MIXER
273         if ((err = cmi8330_add_sb_mixers(acard->sb)) < 0)
274                 return err;
275 #endif
276         return 0;
277 }
278
279 #ifdef CONFIG_PNP
280 static int __devinit snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
281                                      struct pnp_card_link *card,
282                                      const struct pnp_card_device_id *id)
283 {
284         struct pnp_dev *pdev;
285         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
286         int err;
287
288         acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL);
289         if (acard->cap == NULL) {
290                 kfree(cfg);
291                 return -EBUSY;
292         }
293         acard->play = pnp_request_card_device(card, id->devs[1].id, NULL);
294         if (acard->play == NULL) {
295                 kfree(cfg);
296                 return -EBUSY;
297         }
298
299         pdev = acard->cap;
300         pnp_init_resource_table(cfg);
301         /* allocate AD1848 resources */
302         if (wssport[dev] != SNDRV_AUTO_PORT)
303                 pnp_resource_change(&cfg->port_resource[0], wssport[dev], 8);
304         if (wssdma[dev] != SNDRV_AUTO_DMA)
305                 pnp_resource_change(&cfg->dma_resource[0], wssdma[dev], 1);
306         if (wssirq[dev] != SNDRV_AUTO_IRQ)
307                 pnp_resource_change(&cfg->irq_resource[0], wssirq[dev], 1);
308
309         err = pnp_manual_config_dev(pdev, cfg, 0);
310         if (err < 0)
311                 snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP manual resources are invalid, using auto config\n");
312         err = pnp_activate_dev(pdev);
313         if (err < 0) {
314                 snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP configure failure\n");
315                 kfree(cfg);
316                 return -EBUSY;
317         }
318         wssport[dev] = pnp_port_start(pdev, 0);
319         wssdma[dev] = pnp_dma(pdev, 0);
320         wssirq[dev] = pnp_irq(pdev, 0);
321
322         /* allocate SB16 resources */
323         pdev = acard->play;
324         pnp_init_resource_table(cfg);
325         if (sbport[dev] != SNDRV_AUTO_PORT)
326                 pnp_resource_change(&cfg->port_resource[0], sbport[dev], 16);
327         if (sbdma8[dev] != SNDRV_AUTO_DMA)
328                 pnp_resource_change(&cfg->dma_resource[0], sbdma8[dev], 1);
329         if (sbdma16[dev] != SNDRV_AUTO_DMA)
330                 pnp_resource_change(&cfg->dma_resource[1], sbdma16[dev], 1);
331         if (sbirq[dev] != SNDRV_AUTO_IRQ)
332                 pnp_resource_change(&cfg->irq_resource[0], sbirq[dev], 1);
333
334         err = pnp_manual_config_dev(pdev, cfg, 0);
335         if (err < 0)
336                 snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP manual resources are invalid, using auto config\n");
337         err = pnp_activate_dev(pdev);
338         if (err < 0) {
339                 snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP configure failure\n");
340                 kfree(cfg);
341                 return -EBUSY;
342         }
343         sbport[dev] = pnp_port_start(pdev, 0);
344         sbdma8[dev] = pnp_dma(pdev, 0);
345         sbdma16[dev] = pnp_dma(pdev, 1);
346         sbirq[dev] = pnp_irq(pdev, 0);
347
348         kfree(cfg);
349         return 0;
350 }
351 #endif
352
353 /*
354  * PCM interface
355  *
356  * since we call the different chip interfaces for playback and capture
357  * directions, we need a trick.
358  *
359  * - copy the ops for each direction into a local record.
360  * - replace the open callback with the new one, which replaces the
361  *   substream->private_data with the corresponding chip instance
362  *   and calls again the original open callback of the chip.
363  *
364  */
365
366 #ifdef PLAYBACK_ON_SB
367 #define CMI_SB_STREAM   SNDRV_PCM_STREAM_PLAYBACK
368 #define CMI_AD_STREAM   SNDRV_PCM_STREAM_CAPTURE
369 #else
370 #define CMI_SB_STREAM   SNDRV_PCM_STREAM_CAPTURE
371 #define CMI_AD_STREAM   SNDRV_PCM_STREAM_PLAYBACK
372 #endif
373
374 static int snd_cmi8330_playback_open(snd_pcm_substream_t * substream)
375 {
376         struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
377
378         /* replace the private_data and call the original open callback */
379         substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data;
380         return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
381 }
382
383 static int snd_cmi8330_capture_open(snd_pcm_substream_t * substream)
384 {
385         struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
386
387         /* replace the private_data and call the original open callback */
388         substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data;
389         return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
390 }
391
392 static void snd_cmi8330_pcm_free(snd_pcm_t *pcm)
393 {
394         snd_pcm_lib_preallocate_free_for_all(pcm);
395 }
396
397 static int __devinit snd_cmi8330_pcm(snd_card_t *card, struct snd_cmi8330 *chip)
398 {
399         snd_pcm_t *pcm;
400         const snd_pcm_ops_t *ops;
401         int err;
402         static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
403                 snd_cmi8330_playback_open,
404                 snd_cmi8330_capture_open
405         };
406
407         if ((err = snd_pcm_new(card, "CMI8330", 0, 1, 1, &pcm)) < 0)
408                 return err;
409         strcpy(pcm->name, "CMI8330");
410         pcm->private_data = chip;
411         pcm->private_free = snd_cmi8330_pcm_free;
412         
413         /* SB16 */
414         ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
415         chip->streams[CMI_SB_STREAM].ops = *ops;
416         chip->streams[CMI_SB_STREAM].open = ops->open;
417         chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM];
418         chip->streams[CMI_SB_STREAM].private_data = chip->sb;
419
420         /* AD1848 */
421         ops = snd_ad1848_get_pcm_ops(CMI_AD_STREAM);
422         chip->streams[CMI_AD_STREAM].ops = *ops;
423         chip->streams[CMI_AD_STREAM].open = ops->open;
424         chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM];
425         chip->streams[CMI_AD_STREAM].private_data = chip->wss;
426
427         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops);
428         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops);
429
430         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
431                                               snd_dma_isa_data(),
432                                               64*1024, 128*1024);
433         chip->pcm = pcm;
434
435         return 0;
436 }
437
438
439 /*
440  */
441
442 static int __devinit snd_cmi8330_probe(int dev,
443                                        struct pnp_card_link *pcard,
444                                        const struct pnp_card_device_id *pid)
445 {
446         snd_card_t *card;
447         struct snd_cmi8330 *acard;
448         unsigned long flags;
449         int i, err;
450
451 #ifdef CONFIG_PNP
452         if (!isapnp[dev]) {
453 #endif
454                 if (wssport[dev] == SNDRV_AUTO_PORT) {
455                         snd_printk("specify wssport\n");
456                         return -EINVAL;
457                 }
458                 if (sbport[dev] == SNDRV_AUTO_PORT) {
459                         snd_printk("specify sbport\n");
460                         return -EINVAL;
461                 }
462 #ifdef CONFIG_PNP
463         }
464 #endif
465         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
466                             sizeof(struct snd_cmi8330));
467         if (card == NULL) {
468                 snd_printk("could not get a new card\n");
469                 return -ENOMEM;
470         }
471         acard = (struct snd_cmi8330 *)card->private_data;
472         acard->card = card;
473
474 #ifdef CONFIG_PNP
475         if (isapnp[dev]) {
476                 if ((err = snd_cmi8330_pnp(dev, acard, pcard, pid)) < 0) {
477                         snd_printk("PnP detection failed\n");
478                         snd_card_free(card);
479                         return err;
480                 }
481                 snd_card_set_dev(card, &pcard->card->dev);
482         }
483 #endif
484
485         if ((err = snd_ad1848_create(card,
486                                      wssport[dev] + 4,
487                                      wssirq[dev],
488                                      wssdma[dev],
489                                      AD1848_HW_DETECT,
490                                      &acard->wss)) < 0) {
491                 snd_printk("(AD1848) device busy??\n");
492                 snd_card_free(card);
493                 return err;
494         }
495         if (acard->wss->hardware != AD1848_HW_CMI8330) {
496                 snd_printk("(AD1848) not found during probe\n");
497                 snd_card_free(card);
498                 return -ENODEV;
499         }
500
501         if ((err = snd_sbdsp_create(card, sbport[dev],
502                                     sbirq[dev],
503                                     snd_sb16dsp_interrupt,
504                                     sbdma8[dev],
505                                     sbdma16[dev],
506                                     SB_HW_AUTO, &acard->sb)) < 0) {
507                 snd_printk("(SB16) device busy??\n");
508                 snd_card_free(card);
509                 return err;
510         }
511         if (acard->sb->hardware != SB_HW_16) {
512                 snd_printk("(SB16) not found during probe\n");
513                 snd_card_free(card);
514                 return -ENODEV;
515         }
516
517         spin_lock_irqsave(&acard->wss->reg_lock, flags);
518         snd_ad1848_out(acard->wss, AD1848_MISC_INFO, 0x40); /* switch on MODE2 */
519         for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
520                 snd_ad1848_out(acard->wss, i, snd_cmi8330_image[i - CMI8330_RMUX3D]);
521         spin_unlock_irqrestore(&acard->wss->reg_lock, flags);
522
523         if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
524                 snd_printk("failed to create mixers\n");
525                 snd_card_free(card);
526                 return err;
527         }
528
529         if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
530                 snd_printk("failed to create pcms\n");
531                 snd_card_free(card);
532                 return err;
533         }
534
535         strcpy(card->driver, "CMI8330/C3D");
536         strcpy(card->shortname, "C-Media CMI8330/C3D");
537         sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
538                 card->shortname,
539                 acard->wss->port,
540                 wssirq[dev],
541                 wssdma[dev]);
542
543         if ((err = snd_card_register(card)) < 0) {
544                 snd_card_free(card);
545                 return err;
546         }
547
548         if (pcard)
549                 pnp_set_card_drvdata(pcard, card);
550         else
551                 snd_cmi8330_legacy[dev] = card;
552         return 0;
553 }
554
555 #ifdef CONFIG_PNP
556 static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *card,
557                                             const struct pnp_card_device_id *id)
558 {
559         static int dev;
560         int res;
561
562         for ( ; dev < SNDRV_CARDS; dev++) {
563                 if (!enable[dev] || !isapnp[dev])
564                         continue;
565                 res = snd_cmi8330_probe(dev, card, id);
566                 if (res < 0)
567                         return res;
568                 dev++;
569                 return 0;
570         }
571         return -ENODEV;
572 }
573
574 static void __devexit snd_cmi8330_pnp_remove(struct pnp_card_link * pcard)
575 {
576         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
577
578         snd_card_disconnect(card);
579         snd_card_free_in_thread(card);
580 }
581
582 static struct pnp_card_driver cmi8330_pnpc_driver = {
583         .flags = PNP_DRIVER_RES_DISABLE,
584         .name = "cmi8330",
585         .id_table = snd_cmi8330_pnpids,
586         .probe = snd_cmi8330_pnp_detect,
587         .remove = __devexit_p(snd_cmi8330_pnp_remove),
588 };
589 #endif /* CONFIG_PNP */
590
591 static int __init alsa_card_cmi8330_init(void)
592 {
593         int dev, cards = 0;
594
595         for (dev = 0; dev < SNDRV_CARDS; dev++) {
596                 if (!enable[dev])
597                         continue;
598 #ifdef CONFIG_PNP
599                 if (isapnp[dev])
600                         continue;
601 #endif
602                 if (snd_cmi8330_probe(dev, NULL, NULL) >= 0)
603                         cards++;
604         }
605 #ifdef CONFIG_PNP
606         cards += pnp_register_card_driver(&cmi8330_pnpc_driver);
607 #endif
608
609         if (!cards) {
610 #ifdef CONFIG_PNP
611                 pnp_unregister_card_driver(&cmi8330_pnpc_driver);
612 #endif
613 #ifdef MODULE
614                 snd_printk(KERN_ERR "CMI8330 not found or device busy\n");
615 #endif
616                 return -ENODEV;
617         }
618         return 0;
619 }
620
621 static void __exit alsa_card_cmi8330_exit(void)
622 {
623         int i;
624
625 #ifdef CONFIG_PNP
626         /* PnP cards first */
627         pnp_unregister_card_driver(&cmi8330_pnpc_driver);
628 #endif
629         for (i = 0; i < SNDRV_CARDS; i++)
630                 snd_card_free(snd_cmi8330_legacy[i]);
631 }
632
633 module_init(alsa_card_cmi8330_init)
634 module_exit(alsa_card_cmi8330_exit)