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