Merge branch 'topic/memdup_user' into for-linus
[linux-2.6] / 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 fmport=0x388
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 CMI8330 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 <linux/init.h>
47 #include <linux/err.h>
48 #include <linux/isa.h>
49 #include <linux/slab.h>
50 #include <linux/pnp.h>
51 #include <linux/moduleparam.h>
52 #include <sound/core.h>
53 #include <sound/wss.h>
54 #include <sound/opl3.h>
55 #include <sound/mpu401.h>
56 #include <sound/sb.h>
57 #include <sound/initval.h>
58
59 /*
60  */
61 /* #define ENABLE_SB_MIXER */
62 #define PLAYBACK_ON_SB
63
64 /*
65  */
66 MODULE_AUTHOR("George Talusan <gstalusan@uwaterloo.ca>");
67 MODULE_DESCRIPTION("C-Media CMI8330");
68 MODULE_LICENSE("GPL");
69 MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8330,isapnp:{CMI0001,@@@0001,@X@0001}}}");
70
71 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
72 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
73 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
74 #ifdef CONFIG_PNP
75 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
76 #endif
77 static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
78 static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
79 static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
80 static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
81 static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
82 static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
83 static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
84 static long fmport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
85 static long mpuport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
86 static int mpuirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
87
88 module_param_array(index, int, NULL, 0444);
89 MODULE_PARM_DESC(index, "Index value for CMI8330 soundcard.");
90 module_param_array(id, charp, NULL, 0444);
91 MODULE_PARM_DESC(id, "ID string  for CMI8330 soundcard.");
92 module_param_array(enable, bool, NULL, 0444);
93 MODULE_PARM_DESC(enable, "Enable CMI8330 soundcard.");
94 #ifdef CONFIG_PNP
95 module_param_array(isapnp, bool, NULL, 0444);
96 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
97 #endif
98
99 module_param_array(sbport, long, NULL, 0444);
100 MODULE_PARM_DESC(sbport, "Port # for CMI8330 SB driver.");
101 module_param_array(sbirq, int, NULL, 0444);
102 MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330 SB driver.");
103 module_param_array(sbdma8, int, NULL, 0444);
104 MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330 SB driver.");
105 module_param_array(sbdma16, int, NULL, 0444);
106 MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330 SB driver.");
107
108 module_param_array(wssport, long, NULL, 0444);
109 MODULE_PARM_DESC(wssport, "Port # for CMI8330 WSS driver.");
110 module_param_array(wssirq, int, NULL, 0444);
111 MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330 WSS driver.");
112 module_param_array(wssdma, int, NULL, 0444);
113 MODULE_PARM_DESC(wssdma, "DMA for CMI8330 WSS driver.");
114
115 module_param_array(fmport, long, NULL, 0444);
116 MODULE_PARM_DESC(fmport, "FM port # for CMI8330 driver.");
117 module_param_array(mpuport, long, NULL, 0444);
118 MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8330 driver.");
119 module_param_array(mpuirq, int, NULL, 0444);
120 MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8330 MPU-401 port.");
121 #ifdef CONFIG_PNP
122 static int isa_registered;
123 static int pnp_registered;
124 #endif
125
126 #define CMI8330_RMUX3D    16
127 #define CMI8330_MUTEMUX   17
128 #define CMI8330_OUTPUTVOL 18
129 #define CMI8330_MASTVOL   19
130 #define CMI8330_LINVOL    20
131 #define CMI8330_CDINVOL   21
132 #define CMI8330_WAVVOL    22
133 #define CMI8330_RECMUX    23
134 #define CMI8330_WAVGAIN   24
135 #define CMI8330_LINGAIN   25
136 #define CMI8330_CDINGAIN  26
137
138 static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
139 {
140         0x40,                   /* 16 - recording mux (SB-mixer-enabled) */
141 #ifdef ENABLE_SB_MIXER
142         0x40,                   /* 17 - mute mux (Mode2) */
143 #else
144         0x0,                    /* 17 - mute mux */
145 #endif
146         0x0,                    /* 18 - vol */
147         0x0,                    /* 19 - master volume */
148         0x0,                    /* 20 - line-in volume */
149         0x0,                    /* 21 - cd-in volume */
150         0x0,                    /* 22 - wave volume */
151         0x0,                    /* 23 - mute/rec mux */
152         0x0,                    /* 24 - wave rec gain */
153         0x0,                    /* 25 - line-in rec gain */
154         0x0                     /* 26 - cd-in rec gain */
155 };
156
157 typedef int (*snd_pcm_open_callback_t)(struct snd_pcm_substream *);
158
159 struct snd_cmi8330 {
160 #ifdef CONFIG_PNP
161         struct pnp_dev *cap;
162         struct pnp_dev *play;
163         struct pnp_dev *mpu;
164 #endif
165         struct snd_card *card;
166         struct snd_wss *wss;
167         struct snd_sb *sb;
168
169         struct snd_pcm *pcm;
170         struct snd_cmi8330_stream {
171                 struct snd_pcm_ops ops;
172                 snd_pcm_open_callback_t open;
173                 void *private_data; /* sb or wss */
174         } streams[2];
175 };
176
177 #ifdef CONFIG_PNP
178
179 static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
180         { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } } },
181         { .id = "" }
182 };
183
184 MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
185
186 #endif
187
188
189 static struct snd_kcontrol_new snd_cmi8330_controls[] __devinitdata = {
190 WSS_DOUBLE("Master Playback Volume", 0,
191                 CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
192 WSS_SINGLE("Loud Playback Switch", 0,
193                 CMI8330_MUTEMUX, 6, 1, 1),
194 WSS_DOUBLE("PCM Playback Switch", 0,
195                 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
196 WSS_DOUBLE("PCM Playback Volume", 0,
197                 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
198 WSS_DOUBLE("Line Playback Switch", 0,
199                 CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0),
200 WSS_DOUBLE("Line Playback Volume", 0,
201                 CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0),
202 WSS_DOUBLE("Line Capture Switch", 0,
203                 CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0),
204 WSS_DOUBLE("Line Capture Volume", 0,
205                 CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0),
206 WSS_DOUBLE("CD Playback Switch", 0,
207                 CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0),
208 WSS_DOUBLE("CD Capture Switch", 0,
209                 CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0),
210 WSS_DOUBLE("CD Playback Volume", 0,
211                 CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0),
212 WSS_DOUBLE("CD Capture Volume", 0,
213                 CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0),
214 WSS_SINGLE("Mic Playback Switch", 0,
215                 CMI8330_MUTEMUX, 0, 1, 0),
216 WSS_SINGLE("Mic Playback Volume", 0,
217                 CMI8330_OUTPUTVOL, 0, 7, 0),
218 WSS_SINGLE("Mic Capture Switch", 0,
219                 CMI8330_RMUX3D, 0, 1, 0),
220 WSS_SINGLE("Mic Capture Volume", 0,
221                 CMI8330_OUTPUTVOL, 5, 7, 0),
222 WSS_DOUBLE("Wavetable Playback Switch", 0,
223                 CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0),
224 WSS_DOUBLE("Wavetable Playback Volume", 0,
225                 CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0),
226 WSS_DOUBLE("Wavetable Capture Switch", 0,
227                 CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0),
228 WSS_DOUBLE("Wavetable Capture Volume", 0,
229                 CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0),
230 WSS_SINGLE("3D Control - Switch", 0,
231                 CMI8330_RMUX3D, 5, 1, 1),
232 WSS_SINGLE("PC Speaker Playback Volume", 0,
233                 CMI8330_OUTPUTVOL, 3, 3, 0),
234 WSS_DOUBLE("FM Playback Switch", 0,
235                 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
236 WSS_DOUBLE("FM Playback Volume", 0,
237                 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
238 WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", CAPTURE, SWITCH), 0,
239                 CMI8330_RMUX3D, 7, 1, 1),
240 WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", PLAYBACK, SWITCH), 0,
241                 CMI8330_MUTEMUX, 7, 1, 1),
242 };
243
244 #ifdef ENABLE_SB_MIXER
245 static struct sbmix_elem cmi8330_sb_mixers[] __devinitdata = {
246 SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
247 SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
248 SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
249 SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31),
250 SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31),
251 SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1),
252 SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31),
253 SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1),
254 SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31),
255 SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1),
256 SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
257 SB_SINGLE("SB PC Speaker Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
258 SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3),
259 SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3),
260 SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
261 };
262
263 static unsigned char cmi8330_sb_init_values[][2] __devinitdata = {
264         { SB_DSP4_MASTER_DEV + 0, 0 },
265         { SB_DSP4_MASTER_DEV + 1, 0 },
266         { SB_DSP4_PCM_DEV + 0, 0 },
267         { SB_DSP4_PCM_DEV + 1, 0 },
268         { SB_DSP4_SYNTH_DEV + 0, 0 },
269         { SB_DSP4_SYNTH_DEV + 1, 0 },
270         { SB_DSP4_INPUT_LEFT, 0 },
271         { SB_DSP4_INPUT_RIGHT, 0 },
272         { SB_DSP4_OUTPUT_SW, 0 },
273         { SB_DSP4_SPEAKER_DEV, 0 },
274 };
275
276
277 static int __devinit cmi8330_add_sb_mixers(struct snd_sb *chip)
278 {
279         int idx, err;
280         unsigned long flags;
281
282         spin_lock_irqsave(&chip->mixer_lock, flags);
283         snd_sbmixer_write(chip, 0x00, 0x00);            /* mixer reset */
284         spin_unlock_irqrestore(&chip->mixer_lock, flags);
285
286         /* mute and zero volume channels */
287         for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) {
288                 spin_lock_irqsave(&chip->mixer_lock, flags);
289                 snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0],
290                                   cmi8330_sb_init_values[idx][1]);
291                 spin_unlock_irqrestore(&chip->mixer_lock, flags);
292         }
293
294         for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) {
295                 if ((err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx])) < 0)
296                         return err;
297         }
298         return 0;
299 }
300 #endif
301
302 static int __devinit snd_cmi8330_mixer(struct snd_card *card, struct snd_cmi8330 *acard)
303 {
304         unsigned int idx;
305         int err;
306
307         strcpy(card->mixername, "CMI8330/C3D");
308
309         for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) {
310                 err = snd_ctl_add(card,
311                                 snd_ctl_new1(&snd_cmi8330_controls[idx],
312                                              acard->wss));
313                 if (err < 0)
314                         return err;
315         }
316
317 #ifdef ENABLE_SB_MIXER
318         if ((err = cmi8330_add_sb_mixers(acard->sb)) < 0)
319                 return err;
320 #endif
321         return 0;
322 }
323
324 #ifdef CONFIG_PNP
325 static int __devinit snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
326                                      struct pnp_card_link *card,
327                                      const struct pnp_card_device_id *id)
328 {
329         struct pnp_dev *pdev;
330         int err;
331
332         acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL);
333         if (acard->cap == NULL)
334                 return -EBUSY;
335
336         acard->play = pnp_request_card_device(card, id->devs[1].id, NULL);
337         if (acard->play == NULL)
338                 return -EBUSY;
339
340         acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL);
341         if (acard->play == NULL)
342                 return -EBUSY;
343
344         pdev = acard->cap;
345
346         err = pnp_activate_dev(pdev);
347         if (err < 0) {
348                 snd_printk(KERN_ERR "CMI8330/C3D PnP configure failure\n");
349                 return -EBUSY;
350         }
351         wssport[dev] = pnp_port_start(pdev, 0);
352         wssdma[dev] = pnp_dma(pdev, 0);
353         wssirq[dev] = pnp_irq(pdev, 0);
354         fmport[dev] = pnp_port_start(pdev, 1);
355
356         /* allocate SB16 resources */
357         pdev = acard->play;
358
359         err = pnp_activate_dev(pdev);
360         if (err < 0) {
361                 snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP configure failure\n");
362                 return -EBUSY;
363         }
364         sbport[dev] = pnp_port_start(pdev, 0);
365         sbdma8[dev] = pnp_dma(pdev, 0);
366         sbdma16[dev] = pnp_dma(pdev, 1);
367         sbirq[dev] = pnp_irq(pdev, 0);
368
369         /* allocate MPU-401 resources */
370         pdev = acard->mpu;
371
372         err = pnp_activate_dev(pdev);
373         if (err < 0) {
374                 snd_printk(KERN_ERR
375                            "CMI8330/C3D (MPU-401) PnP configure failure\n");
376                 return -EBUSY;
377         }
378         mpuport[dev] = pnp_port_start(pdev, 0);
379         mpuirq[dev] = pnp_irq(pdev, 0);
380         return 0;
381 }
382 #endif
383
384 /*
385  * PCM interface
386  *
387  * since we call the different chip interfaces for playback and capture
388  * directions, we need a trick.
389  *
390  * - copy the ops for each direction into a local record.
391  * - replace the open callback with the new one, which replaces the
392  *   substream->private_data with the corresponding chip instance
393  *   and calls again the original open callback of the chip.
394  *
395  */
396
397 #ifdef PLAYBACK_ON_SB
398 #define CMI_SB_STREAM   SNDRV_PCM_STREAM_PLAYBACK
399 #define CMI_AD_STREAM   SNDRV_PCM_STREAM_CAPTURE
400 #else
401 #define CMI_SB_STREAM   SNDRV_PCM_STREAM_CAPTURE
402 #define CMI_AD_STREAM   SNDRV_PCM_STREAM_PLAYBACK
403 #endif
404
405 static int snd_cmi8330_playback_open(struct snd_pcm_substream *substream)
406 {
407         struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
408
409         /* replace the private_data and call the original open callback */
410         substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data;
411         return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
412 }
413
414 static int snd_cmi8330_capture_open(struct snd_pcm_substream *substream)
415 {
416         struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
417
418         /* replace the private_data and call the original open callback */
419         substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data;
420         return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
421 }
422
423 static int __devinit snd_cmi8330_pcm(struct snd_card *card, struct snd_cmi8330 *chip)
424 {
425         struct snd_pcm *pcm;
426         const struct snd_pcm_ops *ops;
427         int err;
428         static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
429                 snd_cmi8330_playback_open,
430                 snd_cmi8330_capture_open
431         };
432
433         if ((err = snd_pcm_new(card, "CMI8330", 0, 1, 1, &pcm)) < 0)
434                 return err;
435         strcpy(pcm->name, "CMI8330");
436         pcm->private_data = chip;
437         
438         /* SB16 */
439         ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
440         chip->streams[CMI_SB_STREAM].ops = *ops;
441         chip->streams[CMI_SB_STREAM].open = ops->open;
442         chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM];
443         chip->streams[CMI_SB_STREAM].private_data = chip->sb;
444
445         /* AD1848 */
446         ops = snd_wss_get_pcm_ops(CMI_AD_STREAM);
447         chip->streams[CMI_AD_STREAM].ops = *ops;
448         chip->streams[CMI_AD_STREAM].open = ops->open;
449         chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM];
450         chip->streams[CMI_AD_STREAM].private_data = chip->wss;
451
452         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops);
453         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops);
454
455         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
456                                               snd_dma_isa_data(),
457                                               64*1024, 128*1024);
458         chip->pcm = pcm;
459
460         return 0;
461 }
462
463
464 #ifdef CONFIG_PM
465 static int snd_cmi8330_suspend(struct snd_card *card)
466 {
467         struct snd_cmi8330 *acard = card->private_data;
468
469         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
470         snd_pcm_suspend_all(acard->pcm);
471         acard->wss->suspend(acard->wss);
472         snd_sbmixer_suspend(acard->sb);
473         return 0;
474 }
475
476 static int snd_cmi8330_resume(struct snd_card *card)
477 {
478         struct snd_cmi8330 *acard = card->private_data;
479
480         snd_sbdsp_reset(acard->sb);
481         snd_sbmixer_suspend(acard->sb);
482         acard->wss->resume(acard->wss);
483         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
484         return 0;
485 }
486 #endif
487
488
489 /*
490  */
491
492 #ifdef CONFIG_PNP
493 #define is_isapnp_selected(dev)         isapnp[dev]
494 #else
495 #define is_isapnp_selected(dev)         0
496 #endif
497
498 #define PFX     "cmi8330: "
499
500 static int snd_cmi8330_card_new(int dev, struct snd_card **cardp)
501 {
502         struct snd_card *card;
503         struct snd_cmi8330 *acard;
504         int err;
505
506         err = snd_card_create(index[dev], id[dev], THIS_MODULE,
507                               sizeof(struct snd_cmi8330), &card);
508         if (err < 0) {
509                 snd_printk(KERN_ERR PFX "could not get a new card\n");
510                 return err;
511         }
512         acard = card->private_data;
513         acard->card = card;
514         *cardp = card;
515         return 0;
516 }
517
518 static int __devinit snd_cmi8330_probe(struct snd_card *card, int dev)
519 {
520         struct snd_cmi8330 *acard;
521         int i, err;
522         struct snd_opl3 *opl3;
523
524         acard = card->private_data;
525         err = snd_wss_create(card, wssport[dev] + 4, -1,
526                              wssirq[dev],
527                              wssdma[dev], -1,
528                              WSS_HW_DETECT, 0, &acard->wss);
529         if (err < 0) {
530                 snd_printk(KERN_ERR PFX "(CMI8330) device busy??\n");
531                 return err;
532         }
533         if (acard->wss->hardware != WSS_HW_CMI8330) {
534                 snd_printk(KERN_ERR PFX "(CMI8330) not found during probe\n");
535                 return -ENODEV;
536         }
537
538         if ((err = snd_sbdsp_create(card, sbport[dev],
539                                     sbirq[dev],
540                                     snd_sb16dsp_interrupt,
541                                     sbdma8[dev],
542                                     sbdma16[dev],
543                                     SB_HW_AUTO, &acard->sb)) < 0) {
544                 snd_printk(KERN_ERR PFX "(SB16) device busy??\n");
545                 return err;
546         }
547         if (acard->sb->hardware != SB_HW_16) {
548                 snd_printk(KERN_ERR PFX "(SB16) not found during probe\n");
549                 return err;
550         }
551
552         snd_wss_out(acard->wss, CS4231_MISC_INFO, 0x40); /* switch on MODE2 */
553         for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
554                 snd_wss_out(acard->wss, i,
555                             snd_cmi8330_image[i - CMI8330_RMUX3D]);
556
557         if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
558                 snd_printk(KERN_ERR PFX "failed to create mixers\n");
559                 return err;
560         }
561
562         if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
563                 snd_printk(KERN_ERR PFX "failed to create pcms\n");
564                 return err;
565         }
566         if (fmport[dev] != SNDRV_AUTO_PORT) {
567                 if (snd_opl3_create(card,
568                                     fmport[dev], fmport[dev] + 2,
569                                     OPL3_HW_AUTO, 0, &opl3) < 0) {
570                         snd_printk(KERN_ERR PFX
571                                    "no OPL device at 0x%lx-0x%lx ?\n",
572                                    fmport[dev], fmport[dev] + 2);
573                 } else {
574                         err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
575                         if (err < 0)
576                                 return err;
577                 }
578         }
579
580         if (mpuport[dev] != SNDRV_AUTO_PORT) {
581                 if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
582                                         mpuport[dev], 0, mpuirq[dev],
583                                         IRQF_DISABLED, NULL) < 0)
584                         printk(KERN_ERR PFX "no MPU-401 device at 0x%lx.\n",
585                                 mpuport[dev]);
586         }
587
588         strcpy(card->driver, "CMI8330/C3D");
589         strcpy(card->shortname, "C-Media CMI8330/C3D");
590         sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
591                 card->shortname,
592                 acard->wss->port,
593                 wssirq[dev],
594                 wssdma[dev]);
595
596         return snd_card_register(card);
597 }
598
599 static int __devinit snd_cmi8330_isa_match(struct device *pdev,
600                                            unsigned int dev)
601 {
602         if (!enable[dev] || is_isapnp_selected(dev))
603                 return 0;
604         if (wssport[dev] == SNDRV_AUTO_PORT) {
605                 snd_printk(KERN_ERR PFX "specify wssport\n");
606                 return 0;
607         }
608         if (sbport[dev] == SNDRV_AUTO_PORT) {
609                 snd_printk(KERN_ERR PFX "specify sbport\n");
610                 return 0;
611         }
612         return 1;
613 }
614
615 static int __devinit snd_cmi8330_isa_probe(struct device *pdev,
616                                            unsigned int dev)
617 {
618         struct snd_card *card;
619         int err;
620
621         err = snd_cmi8330_card_new(dev, &card);
622         if (err < 0)
623                 return err;
624         snd_card_set_dev(card, pdev);
625         if ((err = snd_cmi8330_probe(card, dev)) < 0) {
626                 snd_card_free(card);
627                 return err;
628         }
629         dev_set_drvdata(pdev, card);
630         return 0;
631 }
632
633 static int __devexit snd_cmi8330_isa_remove(struct device *devptr,
634                                             unsigned int dev)
635 {
636         snd_card_free(dev_get_drvdata(devptr));
637         dev_set_drvdata(devptr, NULL);
638         return 0;
639 }
640
641 #ifdef CONFIG_PM
642 static int snd_cmi8330_isa_suspend(struct device *dev, unsigned int n,
643                                    pm_message_t state)
644 {
645         return snd_cmi8330_suspend(dev_get_drvdata(dev));
646 }
647
648 static int snd_cmi8330_isa_resume(struct device *dev, unsigned int n)
649 {
650         return snd_cmi8330_resume(dev_get_drvdata(dev));
651 }
652 #endif
653
654 #define DEV_NAME        "cmi8330"
655
656 static struct isa_driver snd_cmi8330_driver = {
657         .match          = snd_cmi8330_isa_match,
658         .probe          = snd_cmi8330_isa_probe,
659         .remove         = __devexit_p(snd_cmi8330_isa_remove),
660 #ifdef CONFIG_PM
661         .suspend        = snd_cmi8330_isa_suspend,
662         .resume         = snd_cmi8330_isa_resume,
663 #endif
664         .driver         = {
665                 .name   = DEV_NAME
666         },
667 };
668
669
670 #ifdef CONFIG_PNP
671 static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *pcard,
672                                             const struct pnp_card_device_id *pid)
673 {
674         static int dev;
675         struct snd_card *card;
676         int res;
677
678         for ( ; dev < SNDRV_CARDS; dev++) {
679                 if (enable[dev] && isapnp[dev])
680                         break;
681         }
682         if (dev >= SNDRV_CARDS)
683                 return -ENODEV;
684                                
685         res = snd_cmi8330_card_new(dev, &card);
686         if (res < 0)
687                 return res;
688         if ((res = snd_cmi8330_pnp(dev, card->private_data, pcard, pid)) < 0) {
689                 snd_printk(KERN_ERR PFX "PnP detection failed\n");
690                 snd_card_free(card);
691                 return res;
692         }
693         snd_card_set_dev(card, &pcard->card->dev);
694         if ((res = snd_cmi8330_probe(card, dev)) < 0) {
695                 snd_card_free(card);
696                 return res;
697         }
698         pnp_set_card_drvdata(pcard, card);
699         dev++;
700         return 0;
701 }
702
703 static void __devexit snd_cmi8330_pnp_remove(struct pnp_card_link * pcard)
704 {
705         snd_card_free(pnp_get_card_drvdata(pcard));
706         pnp_set_card_drvdata(pcard, NULL);
707 }
708
709 #ifdef CONFIG_PM
710 static int snd_cmi8330_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
711 {
712         return snd_cmi8330_suspend(pnp_get_card_drvdata(pcard));
713 }
714
715 static int snd_cmi8330_pnp_resume(struct pnp_card_link *pcard)
716 {
717         return snd_cmi8330_resume(pnp_get_card_drvdata(pcard));
718 }
719 #endif
720
721 static struct pnp_card_driver cmi8330_pnpc_driver = {
722         .flags = PNP_DRIVER_RES_DISABLE,
723         .name = "cmi8330",
724         .id_table = snd_cmi8330_pnpids,
725         .probe = snd_cmi8330_pnp_detect,
726         .remove = __devexit_p(snd_cmi8330_pnp_remove),
727 #ifdef CONFIG_PM
728         .suspend        = snd_cmi8330_pnp_suspend,
729         .resume         = snd_cmi8330_pnp_resume,
730 #endif
731 };
732 #endif /* CONFIG_PNP */
733
734 static int __init alsa_card_cmi8330_init(void)
735 {
736         int err;
737
738         err = isa_register_driver(&snd_cmi8330_driver, SNDRV_CARDS);
739 #ifdef CONFIG_PNP
740         if (!err)
741                 isa_registered = 1;
742
743         err = pnp_register_card_driver(&cmi8330_pnpc_driver);
744         if (!err)
745                 pnp_registered = 1;
746
747         if (isa_registered)
748                 err = 0;
749 #endif
750         return err;
751 }
752
753 static void __exit alsa_card_cmi8330_exit(void)
754 {
755 #ifdef CONFIG_PNP
756         if (pnp_registered)
757                 pnp_unregister_card_driver(&cmi8330_pnpc_driver);
758
759         if (isa_registered)
760 #endif
761                 isa_unregister_driver(&snd_cmi8330_driver);
762 }
763
764 module_init(alsa_card_cmi8330_init)
765 module_exit(alsa_card_cmi8330_exit)