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