[ALSA] Skip ac97 SPDIF controls
[linux-2.6.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.22
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  *
81  *  BUGS:
82  *    Some stability problems when unloading the snd-ca0106 kernel module.
83  *    --
84  *
85  *  TODO:
86  *    4 Capture channels, only one implemented so far.
87  *    Other capture rates apart from 48khz not implemented.
88  *    MIDI
89  *    --
90  *  GENERAL INFO:
91  *    Model: SB0310
92  *    P17 Chip: CA0106-DAT
93  *    AC97 Codec: STAC 9721
94  *    ADC: Philips 1361T (Stereo 24bit)
95  *    DAC: WM8746EDS (6-channel, 24bit, 192Khz)
96  *
97  *  GENERAL INFO:
98  *    Model: SB0410
99  *    P17 Chip: CA0106-DAT
100  *    AC97 Codec: None
101  *    ADC: WM8775EDS (4 Channel)
102  *    DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
103  *    SPDIF Out control switches between Mic in and SPDIF out.
104  *    No sound out or mic input working yet.
105  * 
106  *  GENERAL INFO:
107  *    Model: SB0413
108  *    P17 Chip: CA0106-DAT
109  *    AC97 Codec: None.
110  *    ADC: Unknown
111  *    DAC: Unknown
112  *    Trying to handle it like the SB0410.
113  *
114  *  This code was initally based on code from ALSA's emu10k1x.c which is:
115  *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
116  *
117  *   This program is free software; you can redistribute it and/or modify
118  *   it under the terms of the GNU General Public License as published by
119  *   the Free Software Foundation; either version 2 of the License, or
120  *   (at your option) any later version.
121  *
122  *   This program is distributed in the hope that it will be useful,
123  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
124  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
125  *   GNU General Public License for more details.
126  *
127  *   You should have received a copy of the GNU General Public License
128  *   along with this program; if not, write to the Free Software
129  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
130  *
131  */
132 #include <sound/driver.h>
133 #include <linux/delay.h>
134 #include <linux/init.h>
135 #include <linux/interrupt.h>
136 #include <linux/pci.h>
137 #include <linux/slab.h>
138 #include <linux/moduleparam.h>
139 #include <sound/core.h>
140 #include <sound/initval.h>
141 #include <sound/pcm.h>
142 #include <sound/ac97_codec.h>
143 #include <sound/info.h>
144
145 MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
146 MODULE_DESCRIPTION("CA0106");
147 MODULE_LICENSE("GPL");
148 MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
149
150 // module parameters (see "Module Parameters")
151 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
152 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
153 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
154
155 module_param_array(index, int, NULL, 0444);
156 MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
157 module_param_array(id, charp, NULL, 0444);
158 MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
159 module_param_array(enable, bool, NULL, 0444);
160 MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
161
162 #include "ca0106.h"
163
164 typedef struct {
165         u32 serial;
166         char * name;
167 } ca0106_names_t;
168
169 static ca0106_names_t ca0106_chip_names[] = {
170          { 0x10021102, "AudigyLS [SB0310]"} , 
171          { 0x10051102, "AudigyLS [SB0310b]"} , /* Unknown AudigyLS that also says SB0310 on it */
172          { 0x10061102, "Live! 7.1 24bit [SB0410]"} , /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
173          { 0x10071102, "Live! 7.1 24bit [SB0413]"} , /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97.  */
174          { 0x10091462, "MSI K8N Diamond MB [SB0438]"}, /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
175          { 0, "AudigyLS [Unknown]" }
176 };
177
178 /* hardware definition */
179 static snd_pcm_hardware_t snd_ca0106_playback_hw = {
180         .info =                 (SNDRV_PCM_INFO_MMAP | 
181                                  SNDRV_PCM_INFO_INTERLEAVED |
182                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
183                                  SNDRV_PCM_INFO_MMAP_VALID),
184         .formats =              SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
185         .rates =                SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000,
186         .rate_min =             48000,
187         .rate_max =             192000,
188         .channels_min =         2,  //1,
189         .channels_max =         2,  //6,
190         .buffer_bytes_max =     ((65536 - 64) * 8),
191         .period_bytes_min =     64,
192         .period_bytes_max =     (65536 - 64),
193         .periods_min =          2,
194         .periods_max =          8,
195         .fifo_size =            0,
196 };
197
198 static snd_pcm_hardware_t snd_ca0106_capture_hw = {
199         .info =                 (SNDRV_PCM_INFO_MMAP | 
200                                  SNDRV_PCM_INFO_INTERLEAVED |
201                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
202                                  SNDRV_PCM_INFO_MMAP_VALID),
203         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
204         .rates =                SNDRV_PCM_RATE_48000,
205         .rate_min =             48000,
206         .rate_max =             48000,
207         .channels_min =         2,
208         .channels_max =         2,
209         .buffer_bytes_max =     ((65536 - 64) * 8),
210         .period_bytes_min =     64,
211         .period_bytes_max =     (65536 - 64),
212         .periods_min =          2,
213         .periods_max =          2,
214         .fifo_size =            0,
215 };
216
217 unsigned int snd_ca0106_ptr_read(ca0106_t * emu, 
218                                           unsigned int reg, 
219                                           unsigned int chn)
220 {
221         unsigned long flags;
222         unsigned int regptr, val;
223   
224         regptr = (reg << 16) | chn;
225
226         spin_lock_irqsave(&emu->emu_lock, flags);
227         outl(regptr, emu->port + PTR);
228         val = inl(emu->port + DATA);
229         spin_unlock_irqrestore(&emu->emu_lock, flags);
230         return val;
231 }
232
233 void snd_ca0106_ptr_write(ca0106_t *emu, 
234                                    unsigned int reg, 
235                                    unsigned int chn, 
236                                    unsigned int data)
237 {
238         unsigned int regptr;
239         unsigned long flags;
240
241         regptr = (reg << 16) | chn;
242
243         spin_lock_irqsave(&emu->emu_lock, flags);
244         outl(regptr, emu->port + PTR);
245         outl(data, emu->port + DATA);
246         spin_unlock_irqrestore(&emu->emu_lock, flags);
247 }
248
249 static void snd_ca0106_intr_enable(ca0106_t *emu, unsigned int intrenb)
250 {
251         unsigned long flags;
252         unsigned int enable;
253   
254         spin_lock_irqsave(&emu->emu_lock, flags);
255         enable = inl(emu->port + INTE) | intrenb;
256         outl(enable, emu->port + INTE);
257         spin_unlock_irqrestore(&emu->emu_lock, flags);
258 }
259
260 static void snd_ca0106_pcm_free_substream(snd_pcm_runtime_t *runtime)
261 {
262         ca0106_pcm_t *epcm = runtime->private_data;
263   
264         if (epcm) {
265                 kfree(epcm);
266         }
267 }
268
269 /* open_playback callback */
270 static int snd_ca0106_pcm_open_playback_channel(snd_pcm_substream_t *substream, int channel_id)
271 {
272         ca0106_t *chip = snd_pcm_substream_chip(substream);
273         ca0106_channel_t *channel = &(chip->playback_channels[channel_id]);
274         ca0106_pcm_t *epcm;
275         snd_pcm_runtime_t *runtime = substream->runtime;
276         int err;
277
278         epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
279
280         if (epcm == NULL)
281                 return -ENOMEM;
282         epcm->emu = chip;
283         epcm->substream = substream;
284         epcm->channel_id=channel_id;
285   
286         runtime->private_data = epcm;
287         runtime->private_free = snd_ca0106_pcm_free_substream;
288   
289         runtime->hw = snd_ca0106_playback_hw;
290
291         channel->emu = chip;
292         channel->number = channel_id;
293
294         channel->use=1;
295         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
296         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
297         channel->epcm=epcm;
298         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
299                 return err;
300         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
301                 return err;
302         return 0;
303 }
304
305 /* close callback */
306 static int snd_ca0106_pcm_close_playback(snd_pcm_substream_t *substream)
307 {
308         ca0106_t *chip = snd_pcm_substream_chip(substream);
309         snd_pcm_runtime_t *runtime = substream->runtime;
310         ca0106_pcm_t *epcm = runtime->private_data;
311         chip->playback_channels[epcm->channel_id].use=0;
312 /* FIXME: maybe zero others */
313         return 0;
314 }
315
316 static int snd_ca0106_pcm_open_playback_front(snd_pcm_substream_t *substream)
317 {
318         return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
319 }
320
321 static int snd_ca0106_pcm_open_playback_center_lfe(snd_pcm_substream_t *substream)
322 {
323         return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
324 }
325
326 static int snd_ca0106_pcm_open_playback_unknown(snd_pcm_substream_t *substream)
327 {
328         return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
329 }
330
331 static int snd_ca0106_pcm_open_playback_rear(snd_pcm_substream_t *substream)
332 {
333         return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
334 }
335
336 /* open_capture callback */
337 static int snd_ca0106_pcm_open_capture_channel(snd_pcm_substream_t *substream, int channel_id)
338 {
339         ca0106_t *chip = snd_pcm_substream_chip(substream);
340         ca0106_channel_t *channel = &(chip->capture_channels[channel_id]);
341         ca0106_pcm_t *epcm;
342         snd_pcm_runtime_t *runtime = substream->runtime;
343         int err;
344
345         epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
346         if (epcm == NULL) {
347                 snd_printk("open_capture_channel: failed epcm alloc\n");
348                 return -ENOMEM;
349         }
350         epcm->emu = chip;
351         epcm->substream = substream;
352         epcm->channel_id=channel_id;
353   
354         runtime->private_data = epcm;
355         runtime->private_free = snd_ca0106_pcm_free_substream;
356   
357         runtime->hw = snd_ca0106_capture_hw;
358
359         channel->emu = chip;
360         channel->number = channel_id;
361
362         channel->use=1;
363         //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
364         //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
365         channel->epcm=epcm;
366         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
367                 return err;
368         //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
369         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
370                 return err;
371         return 0;
372 }
373
374 /* close callback */
375 static int snd_ca0106_pcm_close_capture(snd_pcm_substream_t *substream)
376 {
377         ca0106_t *chip = snd_pcm_substream_chip(substream);
378         snd_pcm_runtime_t *runtime = substream->runtime;
379         ca0106_pcm_t *epcm = runtime->private_data;
380         chip->capture_channels[epcm->channel_id].use=0;
381 /* FIXME: maybe zero others */
382         return 0;
383 }
384
385 static int snd_ca0106_pcm_open_0_capture(snd_pcm_substream_t *substream)
386 {
387         return snd_ca0106_pcm_open_capture_channel(substream, 0);
388 }
389
390 static int snd_ca0106_pcm_open_1_capture(snd_pcm_substream_t *substream)
391 {
392         return snd_ca0106_pcm_open_capture_channel(substream, 1);
393 }
394
395 static int snd_ca0106_pcm_open_2_capture(snd_pcm_substream_t *substream)
396 {
397         return snd_ca0106_pcm_open_capture_channel(substream, 2);
398 }
399
400 static int snd_ca0106_pcm_open_3_capture(snd_pcm_substream_t *substream)
401 {
402         return snd_ca0106_pcm_open_capture_channel(substream, 3);
403 }
404
405 /* hw_params callback */
406 static int snd_ca0106_pcm_hw_params_playback(snd_pcm_substream_t *substream,
407                                       snd_pcm_hw_params_t * hw_params)
408 {
409         return snd_pcm_lib_malloc_pages(substream,
410                                         params_buffer_bytes(hw_params));
411 }
412
413 /* hw_free callback */
414 static int snd_ca0106_pcm_hw_free_playback(snd_pcm_substream_t *substream)
415 {
416         return snd_pcm_lib_free_pages(substream);
417 }
418
419 /* hw_params callback */
420 static int snd_ca0106_pcm_hw_params_capture(snd_pcm_substream_t *substream,
421                                       snd_pcm_hw_params_t * hw_params)
422 {
423         return snd_pcm_lib_malloc_pages(substream,
424                                         params_buffer_bytes(hw_params));
425 }
426
427 /* hw_free callback */
428 static int snd_ca0106_pcm_hw_free_capture(snd_pcm_substream_t *substream)
429 {
430         return snd_pcm_lib_free_pages(substream);
431 }
432
433 /* prepare playback callback */
434 static int snd_ca0106_pcm_prepare_playback(snd_pcm_substream_t *substream)
435 {
436         ca0106_t *emu = snd_pcm_substream_chip(substream);
437         snd_pcm_runtime_t *runtime = substream->runtime;
438         ca0106_pcm_t *epcm = runtime->private_data;
439         int channel = epcm->channel_id;
440         u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
441         u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
442         u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
443         u32 hcfg_set = 0x00000000;
444         u32 hcfg;
445         u32 reg40_mask = 0x30000 << (channel<<1);
446         u32 reg40_set = 0;
447         u32 reg40;
448         /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
449         u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
450         u32 reg71_set = 0;
451         u32 reg71;
452         int i;
453         
454         //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));
455         //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
456         //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
457         /* Rate can be set per channel. */
458         /* reg40 control host to fifo */
459         /* reg71 controls DAC rate. */
460         switch (runtime->rate) {
461         case 44100:
462                 reg40_set = 0x10000 << (channel<<1);
463                 reg71_set = 0x01010000; 
464                 break;
465         case 48000:
466                 reg40_set = 0;
467                 reg71_set = 0; 
468                 break;
469         case 96000:
470                 reg40_set = 0x20000 << (channel<<1);
471                 reg71_set = 0x02020000; 
472                 break;
473         case 192000:
474                 reg40_set = 0x30000 << (channel<<1);
475                 reg71_set = 0x03030000; 
476                 break;
477         default:
478                 reg40_set = 0;
479                 reg71_set = 0; 
480                 break;
481         }
482         /* Format is a global setting */
483         /* FIXME: Only let the first channel accessed set this. */
484         switch (runtime->format) {
485         case SNDRV_PCM_FORMAT_S16_LE:
486                 hcfg_set = 0;
487                 break;
488         case SNDRV_PCM_FORMAT_S32_LE:
489                 hcfg_set = HCFG_PLAYBACK_S32_LE;
490                 break;
491         default:
492                 hcfg_set = 0;
493                 break;
494         }
495         hcfg = inl(emu->port + HCFG) ;
496         hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
497         outl(hcfg, emu->port + HCFG);
498         reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
499         reg40 = (reg40 & ~reg40_mask) | reg40_set;
500         snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
501         reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
502         reg71 = (reg71 & ~reg71_mask) | reg71_set;
503         snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
504
505         /* FIXME: Check emu->buffer.size before actually writing to it. */
506         for(i=0; i < runtime->periods; i++) {
507                 table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
508                 table_base[(i*2)+1]=period_size_bytes<<16;
509         }
510  
511         snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
512         snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
513         snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
514         snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
515         snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
516         /* FIXME  test what 0 bytes does. */
517         snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
518         snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
519         snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
520         snd_ca0106_ptr_write(emu, 0x08, channel, 0);
521         snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
522 #if 0
523         snd_ca0106_ptr_write(emu, SPCS0, 0,
524                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
525                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
526                                SPCS_GENERATIONSTATUS | 0x00001200 |
527                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
528         }
529 #endif
530
531         return 0;
532 }
533
534 /* prepare capture callback */
535 static int snd_ca0106_pcm_prepare_capture(snd_pcm_substream_t *substream)
536 {
537         ca0106_t *emu = snd_pcm_substream_chip(substream);
538         snd_pcm_runtime_t *runtime = substream->runtime;
539         ca0106_pcm_t *epcm = runtime->private_data;
540         int channel = epcm->channel_id;
541         //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));
542         snd_ca0106_ptr_write(emu, 0x13, channel, 0);
543         snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
544         snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
545         snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
546
547         return 0;
548 }
549
550 /* trigger_playback callback */
551 static int snd_ca0106_pcm_trigger_playback(snd_pcm_substream_t *substream,
552                                     int cmd)
553 {
554         ca0106_t *emu = snd_pcm_substream_chip(substream);
555         snd_pcm_runtime_t *runtime;
556         ca0106_pcm_t *epcm;
557         int channel;
558         int result = 0;
559         struct list_head *pos;
560         snd_pcm_substream_t *s;
561         u32 basic = 0;
562         u32 extended = 0;
563         int running=0;
564
565         switch (cmd) {
566         case SNDRV_PCM_TRIGGER_START:
567                 running=1;
568                 break;
569         case SNDRV_PCM_TRIGGER_STOP:
570         default:
571                 running=0;
572                 break;
573         }
574         snd_pcm_group_for_each(pos, substream) {
575                 s = snd_pcm_group_substream_entry(pos);
576                 runtime = s->runtime;
577                 epcm = runtime->private_data;
578                 channel = epcm->channel_id;
579                 //snd_printk("channel=%d\n",channel);
580                 epcm->running = running;
581                 basic |= (0x1<<channel);
582                 extended |= (0x10<<channel);
583                 snd_pcm_trigger_done(s, substream);
584         }
585         //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
586
587         switch (cmd) {
588         case SNDRV_PCM_TRIGGER_START:
589                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
590                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
591                 break;
592         case SNDRV_PCM_TRIGGER_STOP:
593                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
594                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
595                 break;
596         default:
597                 result = -EINVAL;
598                 break;
599         }
600         return result;
601 }
602
603 /* trigger_capture callback */
604 static int snd_ca0106_pcm_trigger_capture(snd_pcm_substream_t *substream,
605                                     int cmd)
606 {
607         ca0106_t *emu = snd_pcm_substream_chip(substream);
608         snd_pcm_runtime_t *runtime = substream->runtime;
609         ca0106_pcm_t *epcm = runtime->private_data;
610         int channel = epcm->channel_id;
611         int result = 0;
612
613         switch (cmd) {
614         case SNDRV_PCM_TRIGGER_START:
615                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
616                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
617                 epcm->running = 1;
618                 break;
619         case SNDRV_PCM_TRIGGER_STOP:
620                 snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
621                 snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
622                 epcm->running = 0;
623                 break;
624         default:
625                 result = -EINVAL;
626                 break;
627         }
628         return result;
629 }
630
631 /* pointer_playback callback */
632 static snd_pcm_uframes_t
633 snd_ca0106_pcm_pointer_playback(snd_pcm_substream_t *substream)
634 {
635         ca0106_t *emu = snd_pcm_substream_chip(substream);
636         snd_pcm_runtime_t *runtime = substream->runtime;
637         ca0106_pcm_t *epcm = runtime->private_data;
638         snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
639         int channel = epcm->channel_id;
640
641         if (!epcm->running)
642                 return 0;
643
644         ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
645         ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
646         ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
647         if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
648         ptr2 = bytes_to_frames(runtime, ptr1);
649         ptr2+= (ptr4 >> 3) * runtime->period_size;
650         ptr=ptr2;
651         if (ptr >= runtime->buffer_size)
652                 ptr -= runtime->buffer_size;
653         //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);
654
655         return ptr;
656 }
657
658 /* pointer_capture callback */
659 static snd_pcm_uframes_t
660 snd_ca0106_pcm_pointer_capture(snd_pcm_substream_t *substream)
661 {
662         ca0106_t *emu = snd_pcm_substream_chip(substream);
663         snd_pcm_runtime_t *runtime = substream->runtime;
664         ca0106_pcm_t *epcm = runtime->private_data;
665         snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
666         int channel = channel=epcm->channel_id;
667
668         if (!epcm->running)
669                 return 0;
670
671         ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
672         ptr2 = bytes_to_frames(runtime, ptr1);
673         ptr=ptr2;
674         if (ptr >= runtime->buffer_size)
675                 ptr -= runtime->buffer_size;
676         //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);
677
678         return ptr;
679 }
680
681 /* operators */
682 static snd_pcm_ops_t snd_ca0106_playback_front_ops = {
683         .open =        snd_ca0106_pcm_open_playback_front,
684         .close =       snd_ca0106_pcm_close_playback,
685         .ioctl =       snd_pcm_lib_ioctl,
686         .hw_params =   snd_ca0106_pcm_hw_params_playback,
687         .hw_free =     snd_ca0106_pcm_hw_free_playback,
688         .prepare =     snd_ca0106_pcm_prepare_playback,
689         .trigger =     snd_ca0106_pcm_trigger_playback,
690         .pointer =     snd_ca0106_pcm_pointer_playback,
691 };
692
693 static snd_pcm_ops_t snd_ca0106_capture_0_ops = {
694         .open =        snd_ca0106_pcm_open_0_capture,
695         .close =       snd_ca0106_pcm_close_capture,
696         .ioctl =       snd_pcm_lib_ioctl,
697         .hw_params =   snd_ca0106_pcm_hw_params_capture,
698         .hw_free =     snd_ca0106_pcm_hw_free_capture,
699         .prepare =     snd_ca0106_pcm_prepare_capture,
700         .trigger =     snd_ca0106_pcm_trigger_capture,
701         .pointer =     snd_ca0106_pcm_pointer_capture,
702 };
703
704 static snd_pcm_ops_t snd_ca0106_capture_1_ops = {
705         .open =        snd_ca0106_pcm_open_1_capture,
706         .close =       snd_ca0106_pcm_close_capture,
707         .ioctl =       snd_pcm_lib_ioctl,
708         .hw_params =   snd_ca0106_pcm_hw_params_capture,
709         .hw_free =     snd_ca0106_pcm_hw_free_capture,
710         .prepare =     snd_ca0106_pcm_prepare_capture,
711         .trigger =     snd_ca0106_pcm_trigger_capture,
712         .pointer =     snd_ca0106_pcm_pointer_capture,
713 };
714
715 static snd_pcm_ops_t snd_ca0106_capture_2_ops = {
716         .open =        snd_ca0106_pcm_open_2_capture,
717         .close =       snd_ca0106_pcm_close_capture,
718         .ioctl =       snd_pcm_lib_ioctl,
719         .hw_params =   snd_ca0106_pcm_hw_params_capture,
720         .hw_free =     snd_ca0106_pcm_hw_free_capture,
721         .prepare =     snd_ca0106_pcm_prepare_capture,
722         .trigger =     snd_ca0106_pcm_trigger_capture,
723         .pointer =     snd_ca0106_pcm_pointer_capture,
724 };
725
726 static snd_pcm_ops_t snd_ca0106_capture_3_ops = {
727         .open =        snd_ca0106_pcm_open_3_capture,
728         .close =       snd_ca0106_pcm_close_capture,
729         .ioctl =       snd_pcm_lib_ioctl,
730         .hw_params =   snd_ca0106_pcm_hw_params_capture,
731         .hw_free =     snd_ca0106_pcm_hw_free_capture,
732         .prepare =     snd_ca0106_pcm_prepare_capture,
733         .trigger =     snd_ca0106_pcm_trigger_capture,
734         .pointer =     snd_ca0106_pcm_pointer_capture,
735 };
736
737 static snd_pcm_ops_t snd_ca0106_playback_center_lfe_ops = {
738         .open =         snd_ca0106_pcm_open_playback_center_lfe,
739         .close =        snd_ca0106_pcm_close_playback,
740         .ioctl =        snd_pcm_lib_ioctl,
741         .hw_params =    snd_ca0106_pcm_hw_params_playback,
742         .hw_free =      snd_ca0106_pcm_hw_free_playback,
743         .prepare =      snd_ca0106_pcm_prepare_playback,     
744         .trigger =      snd_ca0106_pcm_trigger_playback,  
745         .pointer =      snd_ca0106_pcm_pointer_playback, 
746 };
747
748 static snd_pcm_ops_t snd_ca0106_playback_unknown_ops = {
749         .open =         snd_ca0106_pcm_open_playback_unknown,
750         .close =        snd_ca0106_pcm_close_playback,
751         .ioctl =        snd_pcm_lib_ioctl,
752         .hw_params =    snd_ca0106_pcm_hw_params_playback,
753         .hw_free =      snd_ca0106_pcm_hw_free_playback,
754         .prepare =      snd_ca0106_pcm_prepare_playback,     
755         .trigger =      snd_ca0106_pcm_trigger_playback,  
756         .pointer =      snd_ca0106_pcm_pointer_playback, 
757 };
758
759 static snd_pcm_ops_t snd_ca0106_playback_rear_ops = {
760         .open =         snd_ca0106_pcm_open_playback_rear,
761         .close =        snd_ca0106_pcm_close_playback,
762         .ioctl =        snd_pcm_lib_ioctl,
763         .hw_params =    snd_ca0106_pcm_hw_params_playback,
764                 .hw_free =      snd_ca0106_pcm_hw_free_playback,
765         .prepare =      snd_ca0106_pcm_prepare_playback,     
766         .trigger =      snd_ca0106_pcm_trigger_playback,  
767         .pointer =      snd_ca0106_pcm_pointer_playback, 
768 };
769
770
771 static unsigned short snd_ca0106_ac97_read(ac97_t *ac97,
772                                              unsigned short reg)
773 {
774         ca0106_t *emu = ac97->private_data;
775         unsigned long flags;
776         unsigned short val;
777
778         spin_lock_irqsave(&emu->emu_lock, flags);
779         outb(reg, emu->port + AC97ADDRESS);
780         val = inw(emu->port + AC97DATA);
781         spin_unlock_irqrestore(&emu->emu_lock, flags);
782         return val;
783 }
784
785 static void snd_ca0106_ac97_write(ac97_t *ac97,
786                                     unsigned short reg, unsigned short val)
787 {
788         ca0106_t *emu = ac97->private_data;
789         unsigned long flags;
790   
791         spin_lock_irqsave(&emu->emu_lock, flags);
792         outb(reg, emu->port + AC97ADDRESS);
793         outw(val, emu->port + AC97DATA);
794         spin_unlock_irqrestore(&emu->emu_lock, flags);
795 }
796
797 static int snd_ca0106_ac97(ca0106_t *chip)
798 {
799         ac97_bus_t *pbus;
800         ac97_template_t ac97;
801         int err;
802         static ac97_bus_ops_t ops = {
803                 .write = snd_ca0106_ac97_write,
804                 .read = snd_ca0106_ac97_read,
805         };
806   
807         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
808                 return err;
809         pbus->no_vra = 1; /* we don't need VRA */
810
811         memset(&ac97, 0, sizeof(ac97));
812         ac97.private_data = chip;
813         ac97.scaps = AC97_SCAP_NO_SPDIF;
814         return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
815 }
816
817 static int snd_ca0106_free(ca0106_t *chip)
818 {
819         if (chip->res_port != NULL) {    /* avoid access to already used hardware */
820                 // disable interrupts
821                 snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
822                 outl(0, chip->port + INTE);
823                 snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
824                 udelay(1000);
825                 // disable audio
826                 //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
827                 outl(0, chip->port + HCFG);
828                 /* FIXME: We need to stop and DMA transfers here.
829                  *        But as I am not sure how yet, we cannot from the dma pages.
830                  * So we can fix: snd-malloc: Memory leak?  pages not freed = 8
831                  */
832         }
833         // release the data
834 #if 1
835         if (chip->buffer.area)
836                 snd_dma_free_pages(&chip->buffer);
837 #endif
838
839         // release the i/o port
840         if (chip->res_port) {
841                 release_resource(chip->res_port);
842                 kfree_nocheck(chip->res_port);
843         }
844         // release the irq
845         if (chip->irq >= 0)
846                 free_irq(chip->irq, (void *)chip);
847         pci_disable_device(chip->pci);
848         kfree(chip);
849         return 0;
850 }
851
852 static int snd_ca0106_dev_free(snd_device_t *device)
853 {
854         ca0106_t *chip = device->device_data;
855         return snd_ca0106_free(chip);
856 }
857
858 static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id,
859                                           struct pt_regs *regs)
860 {
861         unsigned int status;
862
863         ca0106_t *chip = dev_id;
864         int i;
865         int mask;
866         unsigned int stat76;
867         ca0106_channel_t *pchannel;
868
869         spin_lock(&chip->emu_lock);
870
871         status = inl(chip->port + IPR);
872
873         // call updater, unlock before it
874         spin_unlock(&chip->emu_lock);
875   
876         if (! status)
877                 return IRQ_NONE;
878
879         stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
880         //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
881         //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
882         mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
883         for(i = 0; i < 4; i++) {
884                 pchannel = &(chip->playback_channels[i]);
885                 if(stat76 & mask) {
886 /* FIXME: Select the correct substream for period elapsed */
887                         if(pchannel->use) {
888                           snd_pcm_period_elapsed(pchannel->epcm->substream);
889                         //printk(KERN_INFO "interrupt [%d] used\n", i);
890                         }
891                 }
892                 //printk(KERN_INFO "channel=%p\n",pchannel);
893                 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
894                 mask <<= 1;
895         }
896         mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
897         for(i = 0; i < 4; i++) {
898                 pchannel = &(chip->capture_channels[i]);
899                 if(stat76 & mask) {
900 /* FIXME: Select the correct substream for period elapsed */
901                         if(pchannel->use) {
902                           snd_pcm_period_elapsed(pchannel->epcm->substream);
903                         //printk(KERN_INFO "interrupt [%d] used\n", i);
904                         }
905                 }
906                 //printk(KERN_INFO "channel=%p\n",pchannel);
907                 //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
908                 mask <<= 1;
909         }
910
911         snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
912         spin_lock(&chip->emu_lock);
913         // acknowledge the interrupt if necessary
914         outl(status, chip->port+IPR);
915
916         spin_unlock(&chip->emu_lock);
917
918         return IRQ_HANDLED;
919 }
920
921 static void snd_ca0106_pcm_free(snd_pcm_t *pcm)
922 {
923         ca0106_t *emu = pcm->private_data;
924         emu->pcm = NULL;
925         snd_pcm_lib_preallocate_free_for_all(pcm);
926 }
927
928 static int __devinit snd_ca0106_pcm(ca0106_t *emu, int device, snd_pcm_t **rpcm)
929 {
930         snd_pcm_t *pcm;
931         snd_pcm_substream_t *substream;
932         int err;
933   
934         if (rpcm)
935                 *rpcm = NULL;
936         if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
937                 return err;
938   
939         pcm->private_data = emu;
940         pcm->private_free = snd_ca0106_pcm_free;
941
942         switch (device) {
943         case 0:
944           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
945           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
946           break;
947         case 1:
948           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
949           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
950           break;
951         case 2:
952           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
953           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
954           break;
955         case 3:
956           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
957           snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
958           break;
959         }
960
961         pcm->info_flags = 0;
962         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
963         strcpy(pcm->name, "CA0106");
964         emu->pcm = pcm;
965
966         for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 
967             substream; 
968             substream = substream->next) {
969                 if ((err = snd_pcm_lib_preallocate_pages(substream, 
970                                                          SNDRV_DMA_TYPE_DEV, 
971                                                          snd_dma_pci_data(emu->pci), 
972                                                          64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
973                         return err;
974         }
975
976         for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 
977               substream; 
978               substream = substream->next) {
979                 if ((err = snd_pcm_lib_preallocate_pages(substream, 
980                                                    SNDRV_DMA_TYPE_DEV, 
981                                                    snd_dma_pci_data(emu->pci), 
982                                                    64*1024, 64*1024)) < 0)
983                         return err;
984         }
985   
986         if (rpcm)
987                 *rpcm = pcm;
988   
989         return 0;
990 }
991
992 static int __devinit snd_ca0106_create(snd_card_t *card,
993                                          struct pci_dev *pci,
994                                          ca0106_t **rchip)
995 {
996         ca0106_t *chip;
997         int err;
998         int ch;
999         static snd_device_ops_t ops = {
1000                 .dev_free = snd_ca0106_dev_free,
1001         };
1002   
1003         *rchip = NULL;
1004   
1005         if ((err = pci_enable_device(pci)) < 0)
1006                 return err;
1007         if (pci_set_dma_mask(pci, 0xffffffffUL) < 0 ||
1008             pci_set_consistent_dma_mask(pci, 0xffffffffUL) < 0) {
1009                 printk(KERN_ERR "error to set 32bit mask DMA\n");
1010                 pci_disable_device(pci);
1011                 return -ENXIO;
1012         }
1013   
1014         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1015         if (chip == NULL) {
1016                 pci_disable_device(pci);
1017                 return -ENOMEM;
1018         }
1019   
1020         chip->card = card;
1021         chip->pci = pci;
1022         chip->irq = -1;
1023
1024         spin_lock_init(&chip->emu_lock);
1025   
1026         chip->port = pci_resource_start(pci, 0);
1027         if ((chip->res_port = request_region(chip->port, 0x20,
1028                                              "snd_ca0106")) == NULL) { 
1029                 snd_ca0106_free(chip);
1030                 printk(KERN_ERR "cannot allocate the port\n");
1031                 return -EBUSY;
1032         }
1033
1034         if (request_irq(pci->irq, snd_ca0106_interrupt,
1035                         SA_INTERRUPT|SA_SHIRQ, "snd_ca0106",
1036                         (void *)chip)) {
1037                 snd_ca0106_free(chip);
1038                 printk(KERN_ERR "cannot grab irq\n");
1039                 return -EBUSY;
1040         }
1041         chip->irq = pci->irq;
1042   
1043         /* This stores the periods table. */ 
1044         if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
1045                 snd_ca0106_free(chip);
1046                 return -ENOMEM;
1047         }
1048
1049         pci_set_master(pci);
1050         /* read revision & serial */
1051         pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
1052         pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
1053         pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
1054 #if 1
1055         printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
1056                chip->revision, chip->serial);
1057 #endif
1058
1059         outl(0, chip->port + INTE);
1060
1061         /*
1062          *  Init to 0x02109204 :
1063          *  Clock accuracy    = 0     (1000ppm)
1064          *  Sample Rate       = 2     (48kHz)
1065          *  Audio Channel     = 1     (Left of 2)
1066          *  Source Number     = 0     (Unspecified)
1067          *  Generation Status = 1     (Original for Cat Code 12)
1068          *  Cat Code          = 12    (Digital Signal Mixer)
1069          *  Mode              = 0     (Mode 0)
1070          *  Emphasis          = 0     (None)
1071          *  CP                = 1     (Copyright unasserted)
1072          *  AN                = 0     (Audio data)
1073          *  P                 = 0     (Consumer)
1074          */
1075         snd_ca0106_ptr_write(chip, SPCS0, 0,
1076                                 chip->spdif_bits[0] =
1077                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1078                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1079                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1080                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1081         /* Only SPCS1 has been tested */
1082         snd_ca0106_ptr_write(chip, SPCS1, 0,
1083                                 chip->spdif_bits[1] =
1084                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1085                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1086                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1087                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1088         snd_ca0106_ptr_write(chip, SPCS2, 0,
1089                                 chip->spdif_bits[2] =
1090                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1091                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1092                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1093                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1094         snd_ca0106_ptr_write(chip, SPCS3, 0,
1095                                 chip->spdif_bits[3] =
1096                                 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1097                                 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1098                                 SPCS_GENERATIONSTATUS | 0x00001200 |
1099                                 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1100
1101         snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
1102         snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
1103
1104         /* Write 0x8000 to AC97_REC_GAIN to mute it. */
1105         outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
1106         outw(0x8000, chip->port + AC97DATA);
1107 #if 0
1108         snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
1109         snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
1110         snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
1111         snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
1112 #endif
1113
1114         //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
1115         /* Analog or Digital output */
1116         snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
1117         snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000b0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers */
1118         chip->spdif_enable = 0; /* Set digital SPDIF output off */
1119         chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1120         //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
1121         //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
1122
1123         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
1124         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
1125         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
1126         snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
1127         snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
1128         snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
1129         snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
1130         snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
1131         for(ch = 0; ch < 4; ch++) {
1132                 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
1133                 snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
1134                 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
1135                 //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
1136                 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
1137                 snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
1138         }
1139         snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC, Line in, TAD in, AUX in */
1140         chip->capture_source = 3; /* Set CAPTURE_SOURCE */
1141
1142         if ((chip->serial == 0x10061102) || 
1143             (chip->serial == 0x10071102) ||
1144             (chip->serial == 0x10091462)) { /* The SB0410 and SB0413 use GPIO differently. */
1145                 /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
1146                 outl(0x0, chip->port+GPIO);
1147                 //outl(0x00f0e000, chip->port+GPIO); /* Analog */
1148                 outl(0x005f4300, chip->port+GPIO); /* Analog */
1149         } else {
1150                 outl(0x0, chip->port+GPIO);
1151                 outl(0x005f03a3, chip->port+GPIO); /* Analog */
1152                 //outl(0x005f02a2, chip->port+GPIO);   /* SPDIF */
1153         }
1154         snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
1155
1156         //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1157         //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
1158         //outl(0x00000009, chip->port+HCFG);
1159         outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
1160
1161         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1162                                   chip, &ops)) < 0) {
1163                 snd_ca0106_free(chip);
1164                 return err;
1165         }
1166         *rchip = chip;
1167         return 0;
1168 }
1169
1170 static int __devinit snd_ca0106_probe(struct pci_dev *pci,
1171                                         const struct pci_device_id *pci_id)
1172 {
1173         static int dev;
1174         snd_card_t *card;
1175         ca0106_t *chip;
1176         ca0106_names_t *c;
1177         int err;
1178
1179         if (dev >= SNDRV_CARDS)
1180                 return -ENODEV;
1181         if (!enable[dev]) {
1182                 dev++;
1183                 return -ENOENT;
1184         }
1185
1186         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1187         if (card == NULL)
1188                 return -ENOMEM;
1189
1190         if ((err = snd_ca0106_create(card, pci, &chip)) < 0) {
1191                 snd_card_free(card);
1192                 return err;
1193         }
1194
1195         if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
1196                 snd_card_free(card);
1197                 return err;
1198         }
1199         if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
1200                 snd_card_free(card);
1201                 return err;
1202         }
1203         if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
1204                 snd_card_free(card);
1205                 return err;
1206         }
1207         if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
1208                 snd_card_free(card);
1209                 return err;
1210         }
1211         if ((chip->serial != 0x10061102) && 
1212             (chip->serial != 0x10071102) && 
1213             (chip->serial != 0x10091462) ) { /* The SB0410 and SB0413 do not have an ac97 chip. */
1214                 if ((err = snd_ca0106_ac97(chip)) < 0) {
1215                         snd_card_free(card);
1216                         return err;
1217                 }
1218         }
1219         if ((err = snd_ca0106_mixer(chip)) < 0) {
1220                 snd_card_free(card);
1221                 return err;
1222         }
1223
1224         snd_ca0106_proc_init(chip);
1225
1226         strcpy(card->driver, "CA0106");
1227         strcpy(card->shortname, "CA0106");
1228
1229         for (c=ca0106_chip_names; c->serial; c++) {
1230                 if (c->serial == chip->serial) break;
1231         }
1232         sprintf(card->longname, "%s at 0x%lx irq %i",
1233                 c->name, chip->port, chip->irq);
1234
1235         if ((err = snd_card_register(card)) < 0) {
1236                 snd_card_free(card);
1237                 return err;
1238         }
1239
1240         pci_set_drvdata(pci, card);
1241         dev++;
1242         return 0;
1243 }
1244
1245 static void __devexit snd_ca0106_remove(struct pci_dev *pci)
1246 {
1247         snd_card_free(pci_get_drvdata(pci));
1248         pci_set_drvdata(pci, NULL);
1249 }
1250
1251 // PCI IDs
1252 static struct pci_device_id snd_ca0106_ids[] = {
1253         { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },    /* Audigy LS or Live 24bit */
1254         { 0, }
1255 };
1256 MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
1257
1258 // pci_driver definition
1259 static struct pci_driver driver = {
1260         .name = "CA0106",
1261         .id_table = snd_ca0106_ids,
1262         .probe = snd_ca0106_probe,
1263         .remove = __devexit_p(snd_ca0106_remove),
1264 };
1265
1266 // initialization of the module
1267 static int __init alsa_card_ca0106_init(void)
1268 {
1269         int err;
1270
1271         if ((err = pci_module_init(&driver)) > 0)
1272                 return err;
1273
1274         return 0;
1275 }
1276
1277 // clean up the module
1278 static void __exit alsa_card_ca0106_exit(void)
1279 {
1280         pci_unregister_driver(&driver);
1281 }
1282
1283 module_init(alsa_card_ca0106_init)
1284 module_exit(alsa_card_ca0106_exit)