Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[pandora-kernel.git] / sound / pci / ca0106 / ca0106_main.c
1 /*
2  *  Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
3  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
4  *  Version: 0.0.23
5  *
6  *  FEATURES currently supported:
7  *    Front, Rear and Center/LFE.
8  *    Surround40 and Surround51.
9  *    Capture from MIC an LINE IN input.
10  *    SPDIF digital playback of PCM stereo and AC3/DTS works.
11  *    (One can use a standard mono mini-jack to one RCA plugs cable.
12  *     or one can use a standard stereo mini-jack to two RCA plugs cable.
13  *     Plug one of the RCA plugs into the Coax input of the external decoder/receiver.)
14  *    ( In theory one could output 3 different AC3 streams at once, to 3 different SPDIF outputs. )
15  *    Notes on how to capture sound:
16  *      The AC97 is used in the PLAYBACK direction.
17  *      The output from the AC97 chip, instead of reaching the speakers, is fed into the Philips 1361T ADC.
18  *      So, to record from the MIC, set the MIC Playback volume to max,
19  *      unmute the MIC and turn up the MASTER Playback volume.
20  *      So, to prevent feedback when capturing, minimise the "Capture feedback into Playback" volume.
21  *   
22  *    The only playback controls that currently do anything are: -
23  *    Analog Front
24  *    Analog Rear
25  *    Analog Center/LFE
26  *    SPDIF Front
27  *    SPDIF Rear
28  *    SPDIF Center/LFE
29  *   
30  *    For capture from Mic in or Line in.
31  *    Digital/Analog ( switch must be in Analog mode for CAPTURE. )
32  * 
33  *    CAPTURE feedback into PLAYBACK
34  * 
35  *  Changelog:
36  *    Support interrupts per period.
37  *    Removed noise from Center/LFE channel when in Analog mode.
38  *    Rename and remove mixer controls.
39  *  0.0.6
40  *    Use separate card based DMA buffer for periods table list.
41  *  0.0.7
42  *    Change remove and rename ctrls into lists.
43  *  0.0.8
44  *    Try to fix capture sources.
45  *  0.0.9
46  *    Fix AC3 output.
47  *    Enable S32_LE format support.
48  *  0.0.10
49  *    Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
50  *  0.0.11
51  *    Add Model name recognition.
52  *  0.0.12
53  *    Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
54  *    Remove redundent "voice" handling.
55  *  0.0.13
56  *    Single trigger call for multi channels.
57  *  0.0.14
58  *    Set limits based on what the sound card hardware can do.
59  *    playback periods_min=2, periods_max=8
60  *    capture hw constraints require period_size = n * 64 bytes.
61  *    playback hw constraints require period_size = n * 64 bytes.
62  *  0.0.15
63  *    Minor updates.
64  *  0.0.16
65  *    Implement 192000 sample rate.
66  *  0.0.17
67  *    Add support for SB0410 and SB0413.
68  *  0.0.18
69  *    Modified Copyright message.
70  *  0.0.19
71  *    Finally fix support for SB Live 24 bit. SB0410 and SB0413.
72  *    The output codec needs resetting, otherwise all output is muted.
73  *  0.0.20
74  *    Merge "pci_disable_device(pci);" fixes.
75  *  0.0.21
76  *    Add 4 capture channels. (SPDIF only comes in on channel 0. )
77  *    Add SPDIF capture using optional digital I/O module for SB Live 24bit. (Analog capture does not yet work.)
78  *  0.0.22
79  *    Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
80  *  0.0.23
81  *    Implement support for Line-in capture on SB Live 24bit.
82  *
83  *  BUGS:
84  *    Some stability problems when unloading the snd-ca0106 kernel module.
85  *    --
86  *
87  *  TODO:
88  *    4 Capture channels, only one implemented so far.
89  *    Other capture rates apart from 48khz not implemented.
90  *    MIDI
91  *    --
92  *  GENERAL INFO:
93  *    Model: SB0310
94  *    P17 Chip: CA0106-DAT
95  *    AC97 Codec: STAC 9721
96  *    ADC: Philips 1361T (Stereo 24bit)
97  *    DAC: WM8746EDS (6-channel, 24bit, 192Khz)
98  *
99  *  GENERAL INFO:
100  *    Model: SB0410
101  *    P17 Chip: CA0106-DAT
102  *    AC97 Codec: None
103  *    ADC: WM8775EDS (4 Channel)
104  *    DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
105  *    SPDIF Out control switches between Mic in and SPDIF out.
106  *    No sound out or mic input working yet.
107  * 
108  *  GENERAL INFO:
109  *    Model: SB0413
110  *    P17 Chip: CA0106-DAT
111  *    AC97 Codec: None.
112  *    ADC: Unknown
113  *    DAC: Unknown
114  *    Trying to handle it like the SB0410.
115  *
116  *  This code was initally based on code from ALSA's emu10k1x.c which is:
117  *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
118  *
119  *   This program is free software; you can redistribute it and/or modify
120  *   it under the terms of the GNU General Public License as published by
121  *   the Free Software Foundation; either version 2 of the License, or
122  *   (at your option) any later version.
123  *
124  *   This program is distributed in the hope that it will be useful,
125  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
126  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127  *   GNU General Public License for more details.
128  *
129  *   You should have received a copy of the GNU General Public License
130  *   along with this program; if not, write to the Free Software
131  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
132  *
133  */
134 #include <sound/driver.h>
135 #include <linux/delay.h>
136 #include <linux/init.h>
137 #include <linux/interrupt.h>
138 #include <linux/pci.h>
139 #include <linux/slab.h>
140 #include <linux/moduleparam.h>
141 #include <linux/dma-mapping.h>
142 #include <sound/core.h>
143 #include <sound/initval.h>
144 #include <sound/pcm.h>
145 #include <sound/ac97_codec.h>
146 #include <sound/info.h>
147
148 MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
149 MODULE_DESCRIPTION("CA0106");
150 MODULE_LICENSE("GPL");
151 MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
152
153 // module parameters (see "Module Parameters")
154 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
155 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
156 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
157 static uint subsystem[SNDRV_CARDS]; /* Force card subsystem model */
158
159 module_param_array(index, int, NULL, 0444);
160 MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
161 module_param_array(id, charp, NULL, 0444);
162 MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
163 module_param_array(enable, bool, NULL, 0444);
164 MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
165 module_param_array(subsystem, uint, NULL, 0444);
166 MODULE_PARM_DESC(subsystem, "Force card subsystem model.");
167
168 #include "ca0106.h"
169
170 static struct snd_ca0106_details ca0106_chip_details[] = {
171          /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
172          /* It is really just a normal SB Live 24bit. */
173          /*
174           * CTRL:CA0111-WTLF
175           * ADC: WM8775SEDS
176           * DAC: CS4382-KQZ
177           */
178          /* Tested:
179           * Playback on front, rear, center/lfe speakers
180           * Capture from Mic in.
181           * Not-Tested:
182           * Capture from Line in.
183           * Playback to digital out.
184           */
185          { .serial = 0x10121102,
186            .name   = "X-Fi Extreme Audio [SB0790]",
187            .gpio_type = 1,
188            .i2c_adc = 1 } ,
189          /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97.  */
190          /* AudigyLS[SB0310] */
191          { .serial = 0x10021102,
192            .name   = "AudigyLS [SB0310]",
193            .ac97   = 1 } , 
194          /* Unknown AudigyLS that also says SB0310 on it */
195          { .serial = 0x10051102,
196            .name   = "AudigyLS [SB0310b]",
197            .ac97   = 1 } ,
198          /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
199          { .serial = 0x10061102,
200            .name   = "Live! 7.1 24bit [SB0410]",
201            .gpio_type = 1,
202            .i2c_adc = 1 } ,
203          /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97.  */
204          { .serial = 0x10071102,
205            .name   = "Live! 7.1 24bit [SB0413]",
206            .gpio_type = 1,
207            .i2c_adc = 1 } ,
208          /* New Audigy SE. Has a different DAC. */
209          /* SB0570:
210           * CTRL:CA0106-DAT
211           * ADC: WM8775EDS
212           * DAC: WM8768GEDS
213           */
214          { .serial = 0x100a1102,
215            .name   = "Audigy SE [SB0570]",
216            .gpio_type = 1,
217            .i2c_adc = 1,
218            .spi_dac = 1 } ,
219          /* New Audigy LS. Has a different DAC. */
220          /* SB0570:
221           * CTRL:CA0106-DAT
222           * ADC: WM8775EDS
223           * DAC: WM8768GEDS
224           */
225          { .serial = 0x10111102,
226            .name   = "Audigy SE OEM [SB0570a]",
227            .gpio_type = 1,
228            .i2c_adc = 1,
229            .spi_dac = 1 } ,
230          /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
231          /* SB0438
232           * CTRL:CA0106-DAT
233           * ADC: WM8775SEDS
234           * DAC: CS4382-KQZ
235           */
236          { .serial = 0x10091462,
237            .name   = "MSI K8N Diamond MB [SB0438]",
238            .gpio_type = 2,
239            .i2c_adc = 1 } ,
240          /* Shuttle XPC SD31P which has an onboard Creative Labs
241           * Sound Blaster Live! 24-bit EAX
242           * high-definition 7.1 audio processor".
243           * Added using info from andrewvegan in alsa bug #1298
244           */
245          { .serial = 0x30381297,
246            .name   = "Shuttle XPC SD31P [SD31P]",
247            .gpio_type = 1,
248            .i2c_adc = 1 } ,
249         /* Shuttle XPC SD11G5 which has an onboard Creative Labs
250          * Sound Blaster Live! 24-bit EAX
251          * high-definition 7.1 audio processor".
252          * Fixes ALSA bug#1600
253          */
254         { .serial = 0x30411297,
255           .name = "Shuttle XPC SD11G5 [SD11G5]",
256           .gpio_type = 1,
257           .i2c_adc = 1 } ,
258          { .serial = 0,
259            .name   = "AudigyLS [Unknown]" }
260 };
261
262 /* hardware definition */
263 static struct snd_pcm_hardware snd_ca0106_playback_hw = {
264         .info =                 (SNDRV_PCM_INFO_MMAP | 
265                                  SNDRV_PCM_INFO_INTERLEAVED |
266                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
267                                  SNDRV_PCM_INFO_MMAP_VALID),
268         .formats =              SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
269         .rates =                (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
270                                  SNDRV_PCM_RATE_192000),
271         .rate_min =             48000,
272         .rate_max =             192000,
273         .channels_min =         2,  //1,
274         .channels_max =         2,  //6,
275         .buffer_bytes_max =     ((65536 - 64) * 8),
276         .period_bytes_min =     64,
277         .period_bytes_max =     (65536 - 64),
278         .periods_min =          2,
279         .periods_max =          8,
280         .fifo_size =            0,
281 };
282
283 static struct snd_pcm_hardware snd_ca0106_capture_hw = {
284         .info =                 (SNDRV_PCM_INFO_MMAP | 
285                                  SNDRV_PCM_INFO_INTERLEAVED |
286                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
287                                  SNDRV_PCM_INFO_MMAP_VALID),
288         .formats =              SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
289         .rates =                (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
290                                  SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
291         .rate_min =             44100,
292         .rate_max =             192000,
293         .channels_min =         2,
294         .channels_max =         2,
295         .buffer_bytes_max =     ((65536 - 64) * 8),
296         .period_bytes_min =     64,
297         .period_bytes_max =     (65536 - 64),
298         .periods_min =          2,
299         .periods_max =          2,
300         .fifo_size =            0,
301 };
302
303 unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu, 
304                                           unsigned int reg, 
305                                           unsigned int chn)
306 {
307         unsigned long flags;
308         unsigned int regptr, val;
309   
310         regptr = (reg << 16) | chn;
311
312         spin_lock_irqsave(&emu->emu_lock, flags);
313         outl(regptr, emu->port + PTR);
314         val = inl(emu->port + DATA);
315         spin_unlock_irqrestore(&emu->emu_lock, flags);
316         return val;
317 }
318
319 void snd_ca0106_ptr_write(struct snd_ca0106 *emu, 
320                                    unsigned int reg, 
321                                    unsigned int chn, 
322                                    unsigned int data)
323 {
324         unsigned int regptr;
325         unsigned long flags;
326
327         regptr = (reg << 16) | chn;
328
329         spin_lock_irqsave(&emu->emu_lock, flags);
330         outl(regptr, emu->port + PTR);
331         outl(data, emu->port + DATA);
332         spin_unlock_irqrestore(&emu->emu_lock, flags);
333 }
334
335 int snd_ca0106_spi_write(struct snd_ca0106 * emu,
336                                    unsigned int data)
337 {
338         unsigned int reset, set;
339         unsigned int reg, tmp;
340         int n, result;
341         reg = SPI;
342         if (data > 0xffff) /* Only 16bit values allowed */
343                 return 1;
344         tmp = snd_ca0106_ptr_read(emu, reg, 0);
345         reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
346         set = reset | 0x10000; /* Set xxx1xxxx */
347         snd_ca0106_ptr_write(emu, reg, 0, reset | data);
348         tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
349         snd_ca0106_ptr_write(emu, reg, 0, set | data);
350         result = 1;
351         /* Wait for status bit to return to 0 */
352         for (n = 0; n < 100; n++) {
353                 udelay(10);
354                 tmp = snd_ca0106_ptr_read(emu, reg, 0);
355                 if (!(tmp & 0x10000)) {
356                         result = 0;
357                         break;
358                 }
359         }
360         if (result) /* Timed out */
361                 return 1;
362         snd_ca0106_ptr_write(emu, reg, 0, reset | data);
363         tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
364         return 0;
365 }
366
367 /* The ADC does not support i2c read, so only write is implemented */
368 int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
369                                 u32 reg,
370                                 u32 value)
371 {
372         u32 tmp;
373         int timeout = 0;
374         int status;
375         int retry;
376         if ((reg > 0x7f) || (value > 0x1ff)) {
377                 snd_printk(KERN_ERR "i2c_write: invalid values.\n");
378                 return -EINVAL;
379         }
380
381         tmp = reg << 25 | value << 16;
382         // snd_printk("I2C-write:reg=0x%x, value=0x%x\n", reg, value);
383         /* Not sure what this I2C channel controls. */
384         /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
385
386         /* This controls the I2C connected to the WM8775 ADC Codec */
387         snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
388
389         for (retry = 0; retry < 10; retry++) {
390                 /* Send the data to i2c */
391                 //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
392                 //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
393                 tmp = 0;
394                 tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
395                 snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
396
397                 /* Wait till the transaction ends */
398                 while (1) {
399                         status = snd_ca0106_ptr_read(emu, I2C_A, 0);
400                         //snd_printk("I2C:status=0x%x\n", status);
401                         timeout++;
402                         if ((status & I2C_A_ADC_START) == 0)
403                                 break;
404
405                         if (timeout > 1000)
406                                 break;
407                 }
408                 //Read back and see if the transaction is successful
409                 if ((status & I2C_A_ADC_ABORT) == 0)
410                         break;
411         }
412
413         if (retry == 10) {
414                 snd_printk(KERN_ERR "Writing to ADC failed!\n");
415                 return -EINVAL;
416         }
417     
418         return 0;
419 }
420
421
422 static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
423 {
424         unsigned long flags;
425         unsigned int enable;
426   
427         spin_lock_irqsave(&emu->emu_lock, flags);
428         enable = inl(emu->port + INTE) | intrenb;
429         outl(enable, emu->port + INTE);
430         spin_unlock_irqrestore(&emu->emu_lock, flags);
431 }
432
433 static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
434 {
435         unsigned long flags;
436         unsigned int enable;
437   
438         spin_lock_irqsave(&emu->emu_lock, flags);
439         enable = inl(emu->port + INTE) & ~intrenb;
440         outl(enable, emu->port + INTE);
441         spin_unlock_irqrestore(&emu->emu_lock, flags);
442 }
443
444
445 static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
446 {
447         kfree(runtime->private_data);
448 }
449
450 /* open_playback callback */
451 static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
452                                                 int channel_id)
453 {
454         struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
455         struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
456         struct snd_ca0106_pcm *epcm;
457         struct snd_pcm_runtime *runtime = substream->runtime;
458         int err;
459
460         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
461
462         if (epcm == NULL)
463                 return -ENOMEM;
464         epcm->emu = chip;
465         epcm->substream = substream;
466         epcm->channel_id=channel_id;
467   
468         runtime->private_data = epcm;
469         runtime->private_free = snd_ca0106_pcm_free_substream;
470   
471         runtime->hw = snd_ca0106_playback_hw;
472
473         channel->emu = chip;
474         channel->number = channel_id;
475
476         channel->use = 1;
477         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
478         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
479         channel->epcm = epcm;
480         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
481                 return err;
482         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
483                 return err;
484         return 0;
485 }
486
487 /* close callback */
488 static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
489 {
490         struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
491         struct snd_pcm_runtime *runtime = substream->runtime;
492         struct snd_ca0106_pcm *epcm = runtime->private_data;
493         chip->playback_channels[epcm->channel_id].use = 0;
494         /* FIXME: maybe zero others */
495         return 0;
496 }
497
498 static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
499 {
500         return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
501 }
502
503 static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
504 {
505         return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
506 }
507
508 static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
509 {
510         return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
511 }
512
513 static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
514 {
515         return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
516 }
517
518 /* open_capture callback */
519 static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
520                                                int channel_id)
521 {
522         struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
523         struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
524         struct snd_ca0106_pcm *epcm;
525         struct snd_pcm_runtime *runtime = substream->runtime;
526         int err;
527
528         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
529         if (epcm == NULL) {
530                 snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
531                 return -ENOMEM;
532         }
533         epcm->emu = chip;
534         epcm->substream = substream;
535         epcm->channel_id=channel_id;
536   
537         runtime->private_data = epcm;
538         runtime->private_free = snd_ca0106_pcm_free_substream;
539   
540         runtime->hw = snd_ca0106_capture_hw;
541
542         channel->emu = chip;
543         channel->number = channel_id;
544
545         channel->use = 1;
546         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
547         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
548         channel->epcm = epcm;
549         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
550                 return err;
551         //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
552         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
553                 return err;
554         return 0;
555 }
556
557 /* close callback */
558 static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
559 {
560         struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
561         struct snd_pcm_runtime *runtime = substream->runtime;
562         struct snd_ca0106_pcm *epcm = runtime->private_data;
563         chip->capture_channels[epcm->channel_id].use = 0;
564         /* FIXME: maybe zero others */
565         return 0;
566 }
567
568 static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
569 {
570         return snd_ca0106_pcm_open_capture_channel(substream, 0);
571 }
572
573 static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
574 {
575         return snd_ca0106_pcm_open_capture_channel(substream, 1);
576 }
577
578 static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
579 {
580         return snd_ca0106_pcm_open_capture_channel(substream, 2);
581 }
582
583 static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
584 {
585         return snd_ca0106_pcm_open_capture_channel(substream, 3);
586 }
587
588 /* hw_params callback */
589 static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
590                                       struct snd_pcm_hw_params *hw_params)
591 {
592         return snd_pcm_lib_malloc_pages(substream,
593                                         params_buffer_bytes(hw_params));
594 }
595
596 /* hw_free callback */
597 static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
598 {
599         return snd_pcm_lib_free_pages(substream);
600 }
601
602 /* hw_params callback */
603 static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
604                                       struct snd_pcm_hw_params *hw_params)
605 {
606         return snd_pcm_lib_malloc_pages(substream,
607                                         params_buffer_bytes(hw_params));
608 }
609
610 /* hw_free callback */
611 static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
612 {
613         return snd_pcm_lib_free_pages(substream);
614 }
615
616 /* prepare playback callback */
617 static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
618 {
619         struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
620         struct snd_pcm_runtime *runtime = substream->runtime;
621         struct snd_ca0106_pcm *epcm = runtime->private_data;
622         int channel = epcm->channel_id;
623         u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
624         u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
625         u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
626         u32 hcfg_set = 0x00000000;
627         u32 hcfg;
628         u32 reg40_mask = 0x30000 << (channel<<1);
629         u32 reg40_set = 0;
630         u32 reg40;
631         /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
632         u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
633         u32 reg71_set = 0;
634         u32 reg71;
635         int i;
636         
637         //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
638         //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
639         //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
640         /* Rate can be set per channel. */
641         /* reg40 control host to fifo */
642         /* reg71 controls DAC rate. */
643         switch (runtime->rate) {
644         case 44100:
645                 reg40_set = 0x10000 << (channel<<1);
646                 reg71_set = 0x01010000; 
647                 break;
648         case 48000:
649                 reg40_set = 0;
650                 reg71_set = 0; 
651                 break;
652         case 96000:
653                 reg40_set = 0x20000 << (channel<<1);
654                 reg71_set = 0x02020000; 
655                 break;
656         case 192000:
657                 reg40_set = 0x30000 << (channel<<1);
658                 reg71_set = 0x03030000; 
659                 break;
660         default:
661                 reg40_set = 0;
662                 reg71_set = 0; 
663                 break;
664         }
665         /* Format is a global setting */
666         /* FIXME: Only let the first channel accessed set this. */
667         switch (runtime->format) {
668         case SNDRV_PCM_FORMAT_S16_LE:
669                 hcfg_set = 0;
670                 break;
671         case SNDRV_PCM_FORMAT_S32_LE:
672                 hcfg_set = HCFG_PLAYBACK_S32_LE;
673                 break;
674         default:
675                 hcfg_set = 0;
676                 break;
677         }
678         hcfg = inl(emu->port + HCFG) ;
679         hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
680         outl(hcfg, emu->port + HCFG);
681         reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
682         reg40 = (reg40 & ~reg40_mask) | reg40_set;
683         snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
684         reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
685         reg71 = (reg71 & ~reg71_mask) | reg71_set;
686         snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
687
688         /* FIXME: Check emu->buffer.size before actually writing to it. */
689         for(i=0; i < runtime->periods; i++) {
690                 table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
691                 table_base[i*2+1] = period_size_bytes << 16;
692         }
693  
694         snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
695         snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
696         snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
697         snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
698         snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
699         /* FIXME  test what 0 bytes does. */
700         snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
701         snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
702         snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
703         snd_ca0106_ptr_write(emu, 0x08, channel, 0);
704         snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
705 #if 0
706         snd_ca0106_ptr_write(emu, SPCS0, 0,
707                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
708                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
709                                SPCS_GENERATIONSTATUS | 0x00001200 |
710                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
711         }
712 #endif
713
714         return 0;
715 }
716
717 /* prepare capture callback */
718 static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
719 {
720         struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
721         struct snd_pcm_runtime *runtime = substream->runtime;
722         struct snd_ca0106_pcm *epcm = runtime->private_data;
723         int channel = epcm->channel_id;
724         u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
725         u32 hcfg_set = 0x00000000;
726         u32 hcfg;
727         u32 over_sampling=0x2;
728         u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
729         u32 reg71_set = 0;
730         u32 reg71;
731         
732         //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
733         //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
734         //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
735         /* reg71 controls ADC rate. */
736         switch (runtime->rate) {
737         case 44100:
738                 reg71_set = 0x00004000;
739                 break;
740         case 48000:
741                 reg71_set = 0; 
742                 break;
743         case 96000:
744                 reg71_set = 0x00008000;
745                 over_sampling=0xa;
746                 break;
747         case 192000:
748                 reg71_set = 0x0000c000; 
749                 over_sampling=0xa;
750                 break;
751         default:
752                 reg71_set = 0; 
753                 break;
754         }
755         /* Format is a global setting */
756         /* FIXME: Only let the first channel accessed set this. */
757         switch (runtime->format) {
758         case SNDRV_PCM_FORMAT_S16_LE:
759                 hcfg_set = 0;
760                 break;
761         case SNDRV_PCM_FORMAT_S32_LE:
762                 hcfg_set = HCFG_CAPTURE_S32_LE;
763                 break;
764         default:
765                 hcfg_set = 0;
766                 break;
767         }
768         hcfg = inl(emu->port + HCFG) ;
769         hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
770         outl(hcfg, emu->port + HCFG);
771         reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
772         reg71 = (reg71 & ~reg71_mask) | reg71_set;
773         snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
774         if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
775                 snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
776         }
777
778
779         //printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size,  frames_to_bytes(runtime, 1));
780         snd_ca0106_ptr_write(emu, 0x13, channel, 0);
781         snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
782         snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
783         snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
784
785         return 0;
786 }
787
788 /* trigger_playback callback */
789 static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
790                                     int cmd)
791 {
792         struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
793         struct snd_pcm_runtime *runtime;
794         struct snd_ca0106_pcm *epcm;
795         int channel;
796         int result = 0;
797         struct snd_pcm_substream *s;
798         u32 basic = 0;
799         u32 extended = 0;
800         int running=0;
801
802         switch (cmd) {
803         case SNDRV_PCM_TRIGGER_START:
804                 running=1;
805                 break;
806         case SNDRV_PCM_TRIGGER_STOP:
807         default:
808                 running=0;
809                 break;
810         }
811         snd_pcm_group_for_each_entry(s, substream) {
812                 runtime = s->runtime;
813                 epcm = runtime->private_data;
814                 channel = epcm->channel_id;
815                 //snd_printk("channel=%d\n",channel);
816                 epcm->running = running;
817                 basic |= (0x1<<channel);
818                 extended |= (0x10<<channel);
819                 snd_pcm_trigger_done(s, substream);
820         }
821         //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
822
823         switch (cmd) {
824         case SNDRV_PCM_TRIGGER_START:
825                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
826                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
827                 break;
828         case SNDRV_PCM_TRIGGER_STOP:
829                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
830                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
831                 break;
832         default:
833                 result = -EINVAL;
834                 break;
835         }
836         return result;
837 }
838
839 /* trigger_capture callback */
840 static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
841                                     int cmd)
842 {
843         struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
844         struct snd_pcm_runtime *runtime = substream->runtime;
845         struct snd_ca0106_pcm *epcm = runtime->private_data;
846         int channel = epcm->channel_id;
847         int result = 0;
848
849         switch (cmd) {
850         case SNDRV_PCM_TRIGGER_START:
851                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
852                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
853                 epcm->running = 1;
854                 break;
855         case SNDRV_PCM_TRIGGER_STOP:
856                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
857                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
858                 epcm->running = 0;
859                 break;
860         default:
861                 result = -EINVAL;
862                 break;
863         }
864         return result;
865 }
866
867 /* pointer_playback callback */
868 static snd_pcm_uframes_t
869 snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
870 {
871         struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
872         struct snd_pcm_runtime *runtime = substream->runtime;
873         struct snd_ca0106_pcm *epcm = runtime->private_data;
874         snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
875         int channel = epcm->channel_id;
876
877         if (!epcm->running)
878                 return 0;
879
880         ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
881         ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
882         ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
883         if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
884         ptr2 = bytes_to_frames(runtime, ptr1);
885         ptr2+= (ptr4 >> 3) * runtime->period_size;
886         ptr=ptr2;
887         if (ptr >= runtime->buffer_size)
888                 ptr -= runtime->buffer_size;
889         //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
890
891         return ptr;
892 }
893
894 /* pointer_capture callback */
895 static snd_pcm_uframes_t
896 snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
897 {
898         struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
899         struct snd_pcm_runtime *runtime = substream->runtime;
900         struct snd_ca0106_pcm *epcm = runtime->private_data;
901         snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
902         int channel = channel=epcm->channel_id;
903
904         if (!epcm->running)
905                 return 0;
906
907         ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
908         ptr2 = bytes_to_frames(runtime, ptr1);
909         ptr=ptr2;
910         if (ptr >= runtime->buffer_size)
911                 ptr -= runtime->buffer_size;
912         //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
913
914         return ptr;
915 }
916
917 /* operators */
918 static struct snd_pcm_ops snd_ca0106_playback_front_ops = {
919         .open =        snd_ca0106_pcm_open_playback_front,
920         .close =       snd_ca0106_pcm_close_playback,
921         .ioctl =       snd_pcm_lib_ioctl,
922         .hw_params =   snd_ca0106_pcm_hw_params_playback,
923         .hw_free =     snd_ca0106_pcm_hw_free_playback,
924         .prepare =     snd_ca0106_pcm_prepare_playback,
925         .trigger =     snd_ca0106_pcm_trigger_playback,
926         .pointer =     snd_ca0106_pcm_pointer_playback,
927 };
928
929 static struct snd_pcm_ops snd_ca0106_capture_0_ops = {
930         .open =        snd_ca0106_pcm_open_0_capture,
931         .close =       snd_ca0106_pcm_close_capture,
932         .ioctl =       snd_pcm_lib_ioctl,
933         .hw_params =   snd_ca0106_pcm_hw_params_capture,
934         .hw_free =     snd_ca0106_pcm_hw_free_capture,
935         .prepare =     snd_ca0106_pcm_prepare_capture,
936         .trigger =     snd_ca0106_pcm_trigger_capture,
937         .pointer =     snd_ca0106_pcm_pointer_capture,
938 };
939
940 static struct snd_pcm_ops snd_ca0106_capture_1_ops = {
941         .open =        snd_ca0106_pcm_open_1_capture,
942         .close =       snd_ca0106_pcm_close_capture,
943         .ioctl =       snd_pcm_lib_ioctl,
944         .hw_params =   snd_ca0106_pcm_hw_params_capture,
945         .hw_free =     snd_ca0106_pcm_hw_free_capture,
946         .prepare =     snd_ca0106_pcm_prepare_capture,
947         .trigger =     snd_ca0106_pcm_trigger_capture,
948         .pointer =     snd_ca0106_pcm_pointer_capture,
949 };
950
951 static struct snd_pcm_ops snd_ca0106_capture_2_ops = {
952         .open =        snd_ca0106_pcm_open_2_capture,
953         .close =       snd_ca0106_pcm_close_capture,
954         .ioctl =       snd_pcm_lib_ioctl,
955         .hw_params =   snd_ca0106_pcm_hw_params_capture,
956         .hw_free =     snd_ca0106_pcm_hw_free_capture,
957         .prepare =     snd_ca0106_pcm_prepare_capture,
958         .trigger =     snd_ca0106_pcm_trigger_capture,
959         .pointer =     snd_ca0106_pcm_pointer_capture,
960 };
961
962 static struct snd_pcm_ops snd_ca0106_capture_3_ops = {
963         .open =        snd_ca0106_pcm_open_3_capture,
964         .close =       snd_ca0106_pcm_close_capture,
965         .ioctl =       snd_pcm_lib_ioctl,
966         .hw_params =   snd_ca0106_pcm_hw_params_capture,
967         .hw_free =     snd_ca0106_pcm_hw_free_capture,
968         .prepare =     snd_ca0106_pcm_prepare_capture,
969         .trigger =     snd_ca0106_pcm_trigger_capture,
970         .pointer =     snd_ca0106_pcm_pointer_capture,
971 };
972
973 static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
974         .open =         snd_ca0106_pcm_open_playback_center_lfe,
975         .close =        snd_ca0106_pcm_close_playback,
976         .ioctl =        snd_pcm_lib_ioctl,
977         .hw_params =    snd_ca0106_pcm_hw_params_playback,
978         .hw_free =      snd_ca0106_pcm_hw_free_playback,
979         .prepare =      snd_ca0106_pcm_prepare_playback,     
980         .trigger =      snd_ca0106_pcm_trigger_playback,  
981         .pointer =      snd_ca0106_pcm_pointer_playback, 
982 };
983
984 static struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
985         .open =         snd_ca0106_pcm_open_playback_unknown,
986         .close =        snd_ca0106_pcm_close_playback,
987         .ioctl =        snd_pcm_lib_ioctl,
988         .hw_params =    snd_ca0106_pcm_hw_params_playback,
989         .hw_free =      snd_ca0106_pcm_hw_free_playback,
990         .prepare =      snd_ca0106_pcm_prepare_playback,     
991         .trigger =      snd_ca0106_pcm_trigger_playback,  
992         .pointer =      snd_ca0106_pcm_pointer_playback, 
993 };
994
995 static struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
996         .open =         snd_ca0106_pcm_open_playback_rear,
997         .close =        snd_ca0106_pcm_close_playback,
998         .ioctl =        snd_pcm_lib_ioctl,
999         .hw_params =    snd_ca0106_pcm_hw_params_playback,
1000                 .hw_free =      snd_ca0106_pcm_hw_free_playback,
1001         .prepare =      snd_ca0106_pcm_prepare_playback,     
1002         .trigger =      snd_ca0106_pcm_trigger_playback,  
1003         .pointer =      snd_ca0106_pcm_pointer_playback, 
1004 };
1005
1006
1007 static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
1008                                              unsigned short reg)
1009 {
1010         struct snd_ca0106 *emu = ac97->private_data;
1011         unsigned long flags;
1012         unsigned short val;
1013
1014         spin_lock_irqsave(&emu->emu_lock, flags);
1015         outb(reg, emu->port + AC97ADDRESS);
1016         val = inw(emu->port + AC97DATA);
1017         spin_unlock_irqrestore(&emu->emu_lock, flags);
1018         return val;
1019 }
1020
1021 static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
1022                                     unsigned short reg, unsigned short val)
1023 {
1024         struct snd_ca0106 *emu = ac97->private_data;
1025         unsigned long flags;
1026   
1027         spin_lock_irqsave(&emu->emu_lock, flags);
1028         outb(reg, emu->port + AC97ADDRESS);
1029         outw(val, emu->port + AC97DATA);
1030         spin_unlock_irqrestore(&emu->emu_lock, flags);
1031 }
1032
1033 static int snd_ca0106_ac97(struct snd_ca0106 *chip)
1034 {
1035         struct snd_ac97_bus *pbus;
1036         struct snd_ac97_template ac97;
1037         int err;
1038         static struct snd_ac97_bus_ops ops = {
1039                 .write = snd_ca0106_ac97_write,
1040                 .read = snd_ca0106_ac97_read,
1041         };
1042   
1043         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1044                 return err;
1045         pbus->no_vra = 1; /* we don't need VRA */
1046
1047         memset(&ac97, 0, sizeof(ac97));
1048         ac97.private_data = chip;
1049         ac97.scaps = AC97_SCAP_NO_SPDIF;
1050         return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1051 }
1052
1053 static int snd_ca0106_free(struct snd_ca0106 *chip)
1054 {
1055         if (chip->res_port != NULL) {    /* avoid access to already used hardware */
1056                 // disable interrupts
1057                 snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
1058                 outl(0, chip->port + INTE);
1059                 snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
1060                 udelay(1000);
1061                 // disable audio
1062                 //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
1063                 outl(0, chip->port + HCFG);
1064                 /* FIXME: We need to stop and DMA transfers here.
1065                  *        But as I am not sure how yet, we cannot from the dma pages.
1066                  * So we can fix: snd-malloc: Memory leak?  pages not freed = 8
1067                  */
1068         }
1069         // release the data
1070 #if 1
1071         if (chip->buffer.area)
1072                 snd_dma_free_pages(&chip->buffer);
1073 #endif
1074
1075         // release the i/o port
1076         release_and_free_resource(chip->res_port);
1077
1078         // release the irq
1079         if (chip->irq >= 0)
1080                 free_irq(chip->irq, chip);
1081         pci_disable_device(chip->pci);
1082         kfree(chip);
1083         return 0;
1084 }
1085
1086 static int snd_ca0106_dev_free(struct snd_device *device)
1087 {
1088         struct snd_ca0106 *chip = device->device_data;
1089         return snd_ca0106_free(chip);
1090 }
1091
1092 static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
1093 {
1094         unsigned int status;
1095
1096         struct snd_ca0106 *chip = dev_id;
1097         int i;
1098         int mask;
1099         unsigned int stat76;
1100         struct snd_ca0106_channel *pchannel;
1101
1102         status = inl(chip->port + IPR);
1103         if (! status)
1104                 return IRQ_NONE;
1105
1106         stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
1107         //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
1108         //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
1109         mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
1110         for(i = 0; i < 4; i++) {
1111                 pchannel = &(chip->playback_channels[i]);
1112                 if (stat76 & mask) {
1113 /* FIXME: Select the correct substream for period elapsed */
1114                         if(pchannel->use) {
1115                                 snd_pcm_period_elapsed(pchannel->epcm->substream);
1116                                 //printk(KERN_INFO "interrupt [%d] used\n", i);
1117                         }
1118                 }
1119                 //printk(KERN_INFO "channel=%p\n",pchannel);
1120                 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1121                 mask <<= 1;
1122         }
1123         mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
1124         for(i = 0; i < 4; i++) {
1125                 pchannel = &(chip->capture_channels[i]);
1126                 if (stat76 & mask) {
1127 /* FIXME: Select the correct substream for period elapsed */
1128                         if(pchannel->use) {
1129                                 snd_pcm_period_elapsed(pchannel->epcm->substream);
1130                                 //printk(KERN_INFO "interrupt [%d] used\n", i);
1131                         }
1132                 }
1133                 //printk(KERN_INFO "channel=%p\n",pchannel);
1134                 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
1135                 mask <<= 1;
1136         }
1137
1138         snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
1139
1140         if (chip->midi.dev_id &&
1141             (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
1142                 if (chip->midi.interrupt)
1143                         chip->midi.interrupt(&chip->midi, status);
1144                 else
1145                         chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
1146         }
1147
1148         // acknowledge the interrupt if necessary
1149         outl(status, chip->port+IPR);
1150
1151         return IRQ_HANDLED;
1152 }
1153
1154 static int __devinit snd_ca0106_pcm(struct snd_ca0106 *emu, int device, struct snd_pcm **rpcm)
1155 {
1156         struct snd_pcm *pcm;
1157         struct snd_pcm_substream *substream;
1158         int err;
1159   
1160         if (rpcm)
1161                 *rpcm = NULL;
1162         if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
1163                 return err;
1164   
1165         pcm->private_data = emu;
1166
1167         switch (device) {
1168         case 0:
1169           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
1170           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
1171           break;
1172         case 1:
1173           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
1174           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
1175           break;
1176         case 2:
1177           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
1178           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
1179           break;
1180         case 3:
1181           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
1182           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
1183           break;
1184         }
1185
1186         pcm->info_flags = 0;
1187         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1188         strcpy(pcm->name, "CA0106");
1189         emu->pcm = pcm;
1190
1191         for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 
1192             substream; 
1193             substream = substream->next) {
1194                 if ((err = snd_pcm_lib_preallocate_pages(substream, 
1195                                                          SNDRV_DMA_TYPE_DEV, 
1196                                                          snd_dma_pci_data(emu->pci), 
1197                                                          64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
1198                         return err;
1199         }
1200
1201         for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 
1202               substream; 
1203               substream = substream->next) {
1204                 if ((err = snd_pcm_lib_preallocate_pages(substream, 
1205                                                    SNDRV_DMA_TYPE_DEV, 
1206                                                    snd_dma_pci_data(emu->pci), 
1207                                                    64*1024, 64*1024)) < 0)
1208                         return err;
1209         }
1210   
1211         if (rpcm)
1212                 *rpcm = pcm;
1213   
1214         return 0;
1215 }
1216
1217 static unsigned int spi_dac_init[] = {
1218         0x00ff,
1219         0x02ff,
1220         0x0400,
1221         0x0520,
1222         0x0620, /* Set 24 bit. Was 0x0600 */
1223         0x08ff,
1224         0x0aff,
1225         0x0cff,
1226         0x0eff,
1227         0x10ff,
1228         0x1200,
1229         0x1400,
1230         0x1480,
1231         0x1800,
1232         0x1aff,
1233         0x1cff,
1234         0x1e00,
1235         0x0530,
1236         0x0602,
1237         0x0622,
1238         0x1400,
1239 };
1240
1241 static unsigned int i2c_adc_init[][2] = {
1242         { 0x17, 0x00 }, /* Reset */
1243         { 0x07, 0x00 }, /* Timeout */
1244         { 0x0b, 0x22 },  /* Interface control */
1245         { 0x0c, 0x22 },  /* Master mode control */
1246         { 0x0d, 0x08 },  /* Powerdown control */
1247         { 0x0e, 0xcf },  /* Attenuation Left  0x01 = -103dB, 0xff = 24dB */
1248         { 0x0f, 0xcf },  /* Attenuation Right 0.5dB steps */
1249         { 0x10, 0x7b },  /* ALC Control 1 */
1250         { 0x11, 0x00 },  /* ALC Control 2 */
1251         { 0x12, 0x32 },  /* ALC Control 3 */
1252         { 0x13, 0x00 },  /* Noise gate control */
1253         { 0x14, 0xa6 },  /* Limiter control */
1254         { 0x15, ADC_MUX_LINEIN },  /* ADC Mixer control */
1255 };
1256
1257 static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
1258                                          struct pci_dev *pci,
1259                                          struct snd_ca0106 **rchip)
1260 {
1261         struct snd_ca0106 *chip;
1262         struct snd_ca0106_details *c;
1263         int err;
1264         int ch;
1265         static struct snd_device_ops ops = {
1266                 .dev_free = snd_ca0106_dev_free,
1267         };
1268   
1269         *rchip = NULL;
1270   
1271         if ((err = pci_enable_device(pci)) < 0)
1272                 return err;
1273         if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
1274             pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
1275                 printk(KERN_ERR "error to set 32bit mask DMA\n");
1276                 pci_disable_device(pci);
1277                 return -ENXIO;
1278         }
1279   
1280         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1281         if (chip == NULL) {
1282                 pci_disable_device(pci);
1283                 return -ENOMEM;
1284         }
1285   
1286         chip->card = card;
1287         chip->pci = pci;
1288         chip->irq = -1;
1289
1290         spin_lock_init(&chip->emu_lock);
1291   
1292         chip->port = pci_resource_start(pci, 0);
1293         if ((chip->res_port = request_region(chip->port, 0x20,
1294                                              "snd_ca0106")) == NULL) { 
1295                 snd_ca0106_free(chip);
1296                 printk(KERN_ERR "cannot allocate the port\n");
1297                 return -EBUSY;
1298         }
1299
1300         if (request_irq(pci->irq, snd_ca0106_interrupt,
1301                         IRQF_SHARED, "snd_ca0106", chip)) {
1302                 snd_ca0106_free(chip);
1303                 printk(KERN_ERR "cannot grab irq\n");
1304                 return -EBUSY;
1305         }
1306         chip->irq = pci->irq;
1307   
1308         /* This stores the periods table. */ 
1309         if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
1310                 snd_ca0106_free(chip);
1311                 return -ENOMEM;
1312         }
1313
1314         pci_set_master(pci);
1315         /* read serial */
1316         pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
1317         pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
1318 #if 1
1319         printk(KERN_INFO "snd-ca0106: Model %04x Rev %08x Serial %08x\n", chip->model,
1320                pci->revision, chip->serial);
1321 #endif
1322         strcpy(card->driver, "CA0106");
1323         strcpy(card->shortname, "CA0106");
1324
1325         for (c = ca0106_chip_details; c->serial; c++) {
1326                 if (subsystem[dev]) {
1327                         if (c->serial == subsystem[dev])
1328                                 break;
1329                 } else if (c->serial == chip->serial)
1330                         break;
1331         }
1332         chip->details = c;
1333         if (subsystem[dev]) {
1334                 printk(KERN_INFO "snd-ca0106: Sound card name=%s, subsystem=0x%x. Forced to subsystem=0x%x\n",
1335                         c->name, chip->serial, subsystem[dev]);
1336         }
1337
1338         sprintf(card->longname, "%s at 0x%lx irq %i",
1339                 c->name, chip->port, chip->irq);
1340
1341         outl(0, chip->port + INTE);
1342
1343         /*
1344          *  Init to 0x02109204 :
1345          *  Clock accuracy    = 0     (1000ppm)
1346          *  Sample Rate       = 2     (48kHz)
1347          *  Audio Channel     = 1     (Left of 2)
1348          *  Source Number     = 0     (Unspecified)
1349          *  Generation Status = 1     (Original for Cat Code 12)
1350          *  Cat Code          = 12    (Digital Signal Mixer)
1351          *  Mode              = 0     (Mode 0)
1352          *  Emphasis          = 0     (None)
1353          *  CP                = 1     (Copyright unasserted)
1354          *  AN                = 0     (Audio data)
1355          *  P                 = 0     (Consumer)
1356          */
1357         snd_ca0106_ptr_write(chip, SPCS0, 0,
1358                                 chip->spdif_bits[0] =
1359                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1360                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1361                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1362                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1363         /* Only SPCS1 has been tested */
1364         snd_ca0106_ptr_write(chip, SPCS1, 0,
1365                                 chip->spdif_bits[1] =
1366                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1367                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1368                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1369                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1370         snd_ca0106_ptr_write(chip, SPCS2, 0,
1371                                 chip->spdif_bits[2] =
1372                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1373                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1374                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1375                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1376         snd_ca0106_ptr_write(chip, SPCS3, 0,
1377                                 chip->spdif_bits[3] =
1378                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1379                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1380                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1381                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1382
1383         snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
1384         snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
1385
1386         /* Write 0x8000 to AC97_REC_GAIN to mute it. */
1387         outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
1388         outw(0x8000, chip->port + AC97DATA);
1389 #if 0
1390         snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
1391         snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
1392         snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
1393         snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
1394 #endif
1395
1396         //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
1397         /* Analog or Digital output */
1398         snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
1399         snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
1400         chip->spdif_enable = 0; /* Set digital SPDIF output off */
1401         //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
1402         //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
1403
1404         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
1405         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
1406         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
1407         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
1408         snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
1409         snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
1410         snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
1411         snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
1412         for(ch = 0; ch < 4; ch++) {
1413                 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
1414                 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
1415                 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
1416                 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
1417                 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
1418                 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
1419         }
1420         if (chip->details->i2c_adc == 1) {
1421                 /* Select MIC, Line in, TAD in, AUX in */
1422                 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
1423                 /* Default to CAPTURE_SOURCE to i2s in */
1424                 chip->capture_source = 3;
1425         } else if (chip->details->ac97 == 1) {
1426                 /* Default to AC97 in */
1427                 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x444400e4);
1428                 /* Default to CAPTURE_SOURCE to AC97 in */
1429                 chip->capture_source = 4;
1430         } else {
1431                 /* Select MIC, Line in, TAD in, AUX in */
1432                 snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
1433                 /* Default to Set CAPTURE_SOURCE to i2s in */
1434                 chip->capture_source = 3;
1435         }
1436
1437         if (chip->details->gpio_type == 2) { /* The SB0438 use GPIO differently. */
1438                 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1439                 outl(0x0, chip->port+GPIO);
1440                 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1441                 outl(0x005f5301, chip->port+GPIO); /* Analog */
1442         } else if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
1443                 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1444                 outl(0x0, chip->port+GPIO);
1445                 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1446                 outl(0x005f5301, chip->port+GPIO); /* Analog */
1447         } else {
1448                 outl(0x0, chip->port+GPIO);
1449                 outl(0x005f03a3, chip->port+GPIO); /* Analog */
1450                 //outl(0x005f02a2, chip->port+GPIO);   /* SPDIF */
1451         }
1452         snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
1453
1454         //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1455         //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
1456         //outl(0x00000009, chip->port+HCFG);
1457         outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
1458
1459         if (chip->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
1460                 int size, n;
1461
1462                 size = ARRAY_SIZE(i2c_adc_init);
1463                 //snd_printk("I2C:array size=0x%x\n", size);
1464                 for (n=0; n < size; n++) {
1465                         snd_ca0106_i2c_write(chip, i2c_adc_init[n][0], i2c_adc_init[n][1]);
1466                 }
1467                 for (n=0; n < 4; n++) {
1468                         chip->i2c_capture_volume[n][0]= 0xcf;
1469                         chip->i2c_capture_volume[n][1]= 0xcf;
1470                 }
1471                 chip->i2c_capture_source=2; /* Line in */
1472                 //snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
1473         }
1474         if (chip->details->spi_dac == 1) { /* The SB0570 use SPI to control DAC. */
1475                 int size, n;
1476
1477                 size = ARRAY_SIZE(spi_dac_init);
1478                 for (n=0; n < size; n++)
1479                         snd_ca0106_spi_write(chip, spi_dac_init[n]);
1480         }
1481
1482         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1483                                   chip, &ops)) < 0) {
1484                 snd_ca0106_free(chip);
1485                 return err;
1486         }
1487         *rchip = chip;
1488         return 0;
1489 }
1490
1491
1492 static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
1493 {
1494         snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
1495 }
1496
1497 static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
1498 {
1499         snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
1500 }
1501
1502 static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
1503 {
1504         return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
1505                                                   midi->port + idx, 0);
1506 }
1507
1508 static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
1509 {
1510         snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
1511 }
1512
1513 static struct snd_card *ca0106_dev_id_card(void *dev_id)
1514 {
1515         return ((struct snd_ca0106 *)dev_id)->card;
1516 }
1517
1518 static int ca0106_dev_id_port(void *dev_id)
1519 {
1520         return ((struct snd_ca0106 *)dev_id)->port;
1521 }
1522
1523 static int __devinit snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
1524 {
1525         struct snd_ca_midi *midi;
1526         char *name;
1527         int err;
1528
1529         if (channel == CA0106_MIDI_CHAN_B) {
1530                 name = "CA0106 MPU-401 (UART) B";
1531                 midi =  &chip->midi2;
1532                 midi->tx_enable = INTE_MIDI_TX_B;
1533                 midi->rx_enable = INTE_MIDI_RX_B;
1534                 midi->ipr_tx = IPR_MIDI_TX_B;
1535                 midi->ipr_rx = IPR_MIDI_RX_B;
1536                 midi->port = MIDI_UART_B_DATA;
1537         } else {
1538                 name = "CA0106 MPU-401 (UART)";
1539                 midi =  &chip->midi;
1540                 midi->tx_enable = INTE_MIDI_TX_A;
1541                 midi->rx_enable = INTE_MIDI_TX_B;
1542                 midi->ipr_tx = IPR_MIDI_TX_A;
1543                 midi->ipr_rx = IPR_MIDI_RX_A;
1544                 midi->port = MIDI_UART_A_DATA;
1545         }
1546
1547         midi->reset = CA0106_MPU401_RESET;
1548         midi->enter_uart = CA0106_MPU401_ENTER_UART;
1549         midi->ack = CA0106_MPU401_ACK;
1550
1551         midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
1552         midi->output_ready = CA0106_MIDI_OUTPUT_READY;
1553
1554         midi->channel = channel;
1555
1556         midi->interrupt_enable = ca0106_midi_interrupt_enable;
1557         midi->interrupt_disable = ca0106_midi_interrupt_disable;
1558
1559         midi->read = ca0106_midi_read;
1560         midi->write = ca0106_midi_write;
1561
1562         midi->get_dev_id_card = ca0106_dev_id_card;
1563         midi->get_dev_id_port = ca0106_dev_id_port;
1564
1565         midi->dev_id = chip;
1566         
1567         if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
1568                 return err;
1569
1570         return 0;
1571 }
1572
1573
1574 static int __devinit snd_ca0106_probe(struct pci_dev *pci,
1575                                         const struct pci_device_id *pci_id)
1576 {
1577         static int dev;
1578         struct snd_card *card;
1579         struct snd_ca0106 *chip;
1580         int err;
1581
1582         if (dev >= SNDRV_CARDS)
1583                 return -ENODEV;
1584         if (!enable[dev]) {
1585                 dev++;
1586                 return -ENOENT;
1587         }
1588
1589         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1590         if (card == NULL)
1591                 return -ENOMEM;
1592
1593         if ((err = snd_ca0106_create(dev, card, pci, &chip)) < 0) {
1594                 snd_card_free(card);
1595                 return err;
1596         }
1597
1598         if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
1599                 snd_card_free(card);
1600                 return err;
1601         }
1602         if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
1603                 snd_card_free(card);
1604                 return err;
1605         }
1606         if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
1607                 snd_card_free(card);
1608                 return err;
1609         }
1610         if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
1611                 snd_card_free(card);
1612                 return err;
1613         }
1614         if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
1615                 if ((err = snd_ca0106_ac97(chip)) < 0) {
1616                         snd_card_free(card);
1617                         return err;
1618                 }
1619         }
1620         if ((err = snd_ca0106_mixer(chip)) < 0) {
1621                 snd_card_free(card);
1622                 return err;
1623         }
1624
1625         snd_printdd("ca0106: probe for MIDI channel A ...");
1626         if ((err = snd_ca0106_midi(chip,CA0106_MIDI_CHAN_A)) < 0) {
1627                 snd_card_free(card);
1628                 snd_printdd(" failed, err=0x%x\n",err);
1629                 return err;
1630         }
1631         snd_printdd(" done.\n");
1632
1633 #ifdef CONFIG_PROC_FS
1634         snd_ca0106_proc_init(chip);
1635 #endif
1636
1637         snd_card_set_dev(card, &pci->dev);
1638
1639         if ((err = snd_card_register(card)) < 0) {
1640                 snd_card_free(card);
1641                 return err;
1642         }
1643
1644         pci_set_drvdata(pci, card);
1645         dev++;
1646         return 0;
1647 }
1648
1649 static void __devexit snd_ca0106_remove(struct pci_dev *pci)
1650 {
1651         snd_card_free(pci_get_drvdata(pci));
1652         pci_set_drvdata(pci, NULL);
1653 }
1654
1655 // PCI IDs
1656 static struct pci_device_id snd_ca0106_ids[] = {
1657         { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },    /* Audigy LS or Live 24bit */
1658         { 0, }
1659 };
1660 MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
1661
1662 // pci_driver definition
1663 static struct pci_driver driver = {
1664         .name = "CA0106",
1665         .id_table = snd_ca0106_ids,
1666         .probe = snd_ca0106_probe,
1667         .remove = __devexit_p(snd_ca0106_remove),
1668 };
1669
1670 // initialization of the module
1671 static int __init alsa_card_ca0106_init(void)
1672 {
1673         return pci_register_driver(&driver);
1674 }
1675
1676 // clean up the module
1677 static void __exit alsa_card_ca0106_exit(void)
1678 {
1679         pci_unregister_driver(&driver);
1680 }
1681
1682 module_init(alsa_card_ca0106_init)
1683 module_exit(alsa_card_ca0106_exit)