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