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