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