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