ALSA: ctxfi - Clean up probe routines
[linux-2.6.git] / sound / pci / ctxfi / ctatc.c
1 /**
2  * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
3  *
4  * This source file is released under GPL v2 license (no other versions).
5  * See the COPYING file included in the main directory of this source
6  * distribution for the license terms and conditions.
7  *
8  * @File    ctatc.c
9  *
10  * @Brief
11  * This file contains the implementation of the device resource management
12  * object.
13  *
14  * @Author Liu Chun
15  * @Date Mar 28 2008
16  */
17
18 #include "ctatc.h"
19 #include "ctpcm.h"
20 #include "ctmixer.h"
21 #include "cthardware.h"
22 #include "ctsrc.h"
23 #include "ctamixer.h"
24 #include "ctdaio.h"
25 #include "cttimer.h"
26 #include <linux/delay.h>
27 #include <sound/pcm.h>
28 #include <sound/control.h>
29 #include <sound/asoundef.h>
30
31 #define MONO_SUM_SCALE  0x19a8  /* 2^(-0.5) in 14-bit floating format */
32 #define DAIONUM         7
33 #define MAX_MULTI_CHN   8
34
35 #define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
36                             | IEC958_AES0_CON_NOT_COPYRIGHT) \
37                             | ((IEC958_AES1_CON_MIXER \
38                             | IEC958_AES1_CON_ORIGINAL) << 8) \
39                             | (0x10 << 16) \
40                             | ((IEC958_AES3_CON_FS_48000) << 24))
41
42 static struct snd_pci_quirk __devinitdata subsys_20k1_list[] = {
43         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
44         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
45         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
46         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
47         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0x6000,
48                            PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "UAA", CTUAA),
49         SND_PCI_QUIRK_VENDOR(PCI_VENDOR_ID_CREATIVE,
50                              "Unknown", CT20K1_UNKNOWN),
51         { } /* terminator */
52 };
53
54 static struct snd_pci_quirk __devinitdata subsys_20k2_list[] = {
55         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
56                       "SB0760", CTSB0760),
57         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
58                       "SB0880", CTSB0880),
59         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
60                       "SB0880", CTSB0880),
61         SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
62                       "SB0880", CTSB0880),
63         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0x6000,
64                            PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "UAA", CTHENDRIX),
65         { } /* terminator */
66 };
67
68 static const char *ct_subsys_name[NUM_CTCARDS] = {
69         [CTSB055X]      = "SB055x",
70         [CTSB073X]      = "SB073x",
71         [CTSB0760]      = "SB076x",
72         [CTUAA]         = "UAA",
73         [CT20K1_UNKNOWN] = "Unknown",
74         [CTHENDRIX]     = "Hendrix",
75         [CTSB0880]      = "SB0880",
76 };
77
78 static struct {
79         int (*create)(struct ct_atc *atc,
80                         enum CTALSADEVS device, const char *device_name);
81         int (*destroy)(void *alsa_dev);
82         const char *public_name;
83 } alsa_dev_funcs[NUM_CTALSADEVS] = {
84         [FRONT]         = { .create = ct_alsa_pcm_create,
85                             .destroy = NULL,
86                             .public_name = "Front/WaveIn"},
87         [SURROUND]      = { .create = ct_alsa_pcm_create,
88                             .destroy = NULL,
89                             .public_name = "Surround"},
90         [CLFE]          = { .create = ct_alsa_pcm_create,
91                             .destroy = NULL,
92                             .public_name = "Center/LFE"},
93         [SIDE]          = { .create = ct_alsa_pcm_create,
94                             .destroy = NULL,
95                             .public_name = "Side"},
96         [IEC958]        = { .create = ct_alsa_pcm_create,
97                             .destroy = NULL,
98                             .public_name = "IEC958 Non-audio"},
99
100         [MIXER]         = { .create = ct_alsa_mix_create,
101                             .destroy = NULL,
102                             .public_name = "Mixer"}
103 };
104
105 typedef int (*create_t)(void *, void **);
106 typedef int (*destroy_t)(void *);
107
108 static struct {
109         int (*create)(void *hw, void **rmgr);
110         int (*destroy)(void *mgr);
111 } rsc_mgr_funcs[NUM_RSCTYP] = {
112         [SRC]           = { .create     = (create_t)src_mgr_create,
113                             .destroy    = (destroy_t)src_mgr_destroy    },
114         [SRCIMP]        = { .create     = (create_t)srcimp_mgr_create,
115                             .destroy    = (destroy_t)srcimp_mgr_destroy },
116         [AMIXER]        = { .create     = (create_t)amixer_mgr_create,
117                             .destroy    = (destroy_t)amixer_mgr_destroy },
118         [SUM]           = { .create     = (create_t)sum_mgr_create,
119                             .destroy    = (destroy_t)sum_mgr_destroy    },
120         [DAIO]          = { .create     = (create_t)daio_mgr_create,
121                             .destroy    = (destroy_t)daio_mgr_destroy   }
122 };
123
124 static int
125 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
126
127 /* *
128  * Only mono and interleaved modes are supported now.
129  * Always allocates a contiguous channel block.
130  * */
131
132 static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
133 {
134         struct snd_pcm_runtime *runtime;
135         struct ct_vm *vm;
136
137         if (NULL == apcm->substream)
138                 return 0;
139
140         runtime = apcm->substream->runtime;
141         vm = atc->vm;
142
143         apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
144
145         if (NULL == apcm->vm_block)
146                 return -ENOENT;
147
148         return 0;
149 }
150
151 static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
152 {
153         struct ct_vm *vm;
154
155         if (NULL == apcm->vm_block)
156                 return;
157
158         vm = atc->vm;
159
160         vm->unmap(vm, apcm->vm_block);
161
162         apcm->vm_block = NULL;
163 }
164
165 static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
166 {
167         struct ct_vm *vm;
168         void *kvirt_addr;
169         unsigned long phys_addr;
170
171         vm = atc->vm;
172         kvirt_addr = vm->get_ptp_virt(vm, index);
173         if (kvirt_addr == NULL)
174                 phys_addr = (~0UL);
175         else
176                 phys_addr = virt_to_phys(kvirt_addr);
177
178         return phys_addr;
179 }
180
181 static unsigned int convert_format(snd_pcm_format_t snd_format)
182 {
183         switch (snd_format) {
184         case SNDRV_PCM_FORMAT_U8:
185                 return SRC_SF_U8;
186         case SNDRV_PCM_FORMAT_S16_LE:
187                 return SRC_SF_S16;
188         case SNDRV_PCM_FORMAT_S24_3LE:
189                 return SRC_SF_S24;
190         case SNDRV_PCM_FORMAT_S32_LE:
191                 return SRC_SF_S32;
192         case SNDRV_PCM_FORMAT_FLOAT_LE:
193                 return SRC_SF_F32;
194         default:
195                 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
196                         snd_format);
197                 return SRC_SF_S16;
198         }
199 }
200
201 static unsigned int
202 atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
203 {
204         unsigned int pitch;
205         int b;
206
207         /* get pitch and convert to fixed-point 8.24 format. */
208         pitch = (input_rate / output_rate) << 24;
209         input_rate %= output_rate;
210         input_rate /= 100;
211         output_rate /= 100;
212         for (b = 31; ((b >= 0) && !(input_rate >> b)); )
213                 b--;
214
215         if (b >= 0) {
216                 input_rate <<= (31 - b);
217                 input_rate /= output_rate;
218                 b = 24 - (31 - b);
219                 if (b >= 0)
220                         input_rate <<= b;
221                 else
222                         input_rate >>= -b;
223
224                 pitch |= input_rate;
225         }
226
227         return pitch;
228 }
229
230 static int select_rom(unsigned int pitch)
231 {
232         if ((pitch > 0x00428f5c) && (pitch < 0x01b851ec)) {
233                 /* 0.26 <= pitch <= 1.72 */
234                 return 1;
235         } else if ((0x01d66666 == pitch) || (0x01d66667 == pitch)) {
236                 /* pitch == 1.8375 */
237                 return 2;
238         } else if (0x02000000 == pitch) {
239                 /* pitch == 2 */
240                 return 3;
241         } else if ((pitch >= 0x0) && (pitch <= 0x08000000)) {
242                 /* 0 <= pitch <= 8 */
243                 return 0;
244         } else {
245                 return -ENOENT;
246         }
247 }
248
249 static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
250 {
251         struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
252         struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
253         struct src_desc desc = {0};
254         struct amixer_desc mix_dsc = {0};
255         struct src *src;
256         struct amixer *amixer;
257         int err;
258         int n_amixer = apcm->substream->runtime->channels, i = 0;
259         int device = apcm->substream->pcm->device;
260         unsigned int pitch;
261         unsigned long flags;
262
263         if (NULL != apcm->src) {
264                 /* Prepared pcm playback */
265                 return 0;
266         }
267
268         /* first release old resources */
269         atc->pcm_release_resources(atc, apcm);
270
271         /* Get SRC resource */
272         desc.multi = apcm->substream->runtime->channels;
273         desc.msr = atc->msr;
274         desc.mode = MEMRD;
275         err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
276         if (err)
277                 goto error1;
278
279         pitch = atc_get_pitch(apcm->substream->runtime->rate,
280                                                 (atc->rsr * atc->msr));
281         src = apcm->src;
282         src->ops->set_pitch(src, pitch);
283         src->ops->set_rom(src, select_rom(pitch));
284         src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
285         src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
286
287         /* Get AMIXER resource */
288         n_amixer = (n_amixer < 2) ? 2 : n_amixer;
289         apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
290         if (NULL == apcm->amixers) {
291                 err = -ENOMEM;
292                 goto error1;
293         }
294         mix_dsc.msr = atc->msr;
295         for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
296                 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
297                                         (struct amixer **)&apcm->amixers[i]);
298                 if (err)
299                         goto error1;
300
301                 apcm->n_amixer++;
302         }
303
304         /* Set up device virtual mem map */
305         err = ct_map_audio_buffer(atc, apcm);
306         if (err < 0)
307                 goto error1;
308
309         /* Connect resources */
310         src = apcm->src;
311         for (i = 0; i < n_amixer; i++) {
312                 amixer = apcm->amixers[i];
313                 spin_lock_irqsave(&atc->atc_lock, flags);
314                 amixer->ops->setup(amixer, &src->rsc,
315                                         INIT_VOL, atc->pcm[i+device*2]);
316                 spin_unlock_irqrestore(&atc->atc_lock, flags);
317                 src = src->ops->next_interleave(src);
318                 if (NULL == src)
319                         src = apcm->src;
320         }
321
322         ct_timer_prepare(apcm->timer);
323
324         return 0;
325
326 error1:
327         atc_pcm_release_resources(atc, apcm);
328         return err;
329 }
330
331 static int
332 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
333 {
334         struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
335         struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
336         struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
337         struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
338         struct srcimp *srcimp;
339         int i;
340
341         if (NULL != apcm->srcimps) {
342                 for (i = 0; i < apcm->n_srcimp; i++) {
343                         srcimp = apcm->srcimps[i];
344                         srcimp->ops->unmap(srcimp);
345                         srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
346                         apcm->srcimps[i] = NULL;
347                 }
348                 kfree(apcm->srcimps);
349                 apcm->srcimps = NULL;
350         }
351
352         if (NULL != apcm->srccs) {
353                 for (i = 0; i < apcm->n_srcc; i++) {
354                         src_mgr->put_src(src_mgr, apcm->srccs[i]);
355                         apcm->srccs[i] = NULL;
356                 }
357                 kfree(apcm->srccs);
358                 apcm->srccs = NULL;
359         }
360
361         if (NULL != apcm->amixers) {
362                 for (i = 0; i < apcm->n_amixer; i++) {
363                         amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
364                         apcm->amixers[i] = NULL;
365                 }
366                 kfree(apcm->amixers);
367                 apcm->amixers = NULL;
368         }
369
370         if (NULL != apcm->mono) {
371                 sum_mgr->put_sum(sum_mgr, apcm->mono);
372                 apcm->mono = NULL;
373         }
374
375         if (NULL != apcm->src) {
376                 src_mgr->put_src(src_mgr, apcm->src);
377                 apcm->src = NULL;
378         }
379
380         if (NULL != apcm->vm_block) {
381                 /* Undo device virtual mem map */
382                 ct_unmap_audio_buffer(atc, apcm);
383                 apcm->vm_block = NULL;
384         }
385
386         return 0;
387 }
388
389 static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
390 {
391         unsigned int max_cisz;
392         struct src *src = apcm->src;
393
394         max_cisz = src->multi * src->rsc.msr;
395         max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
396
397         src->ops->set_sa(src, apcm->vm_block->addr);
398         src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
399         src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
400         src->ops->set_cisz(src, max_cisz);
401
402         src->ops->set_bm(src, 1);
403         src->ops->set_state(src, SRC_STATE_INIT);
404         src->ops->commit_write(src);
405
406         ct_timer_start(apcm->timer);
407         return 0;
408 }
409
410 static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
411 {
412         struct src *src;
413         int i;
414
415         ct_timer_stop(apcm->timer);
416
417         src = apcm->src;
418         src->ops->set_bm(src, 0);
419         src->ops->set_state(src, SRC_STATE_OFF);
420         src->ops->commit_write(src);
421
422         if (NULL != apcm->srccs) {
423                 for (i = 0; i < apcm->n_srcc; i++) {
424                         src = apcm->srccs[i];
425                         src->ops->set_bm(src, 0);
426                         src->ops->set_state(src, SRC_STATE_OFF);
427                         src->ops->commit_write(src);
428                 }
429         }
430
431         apcm->started = 0;
432
433         return 0;
434 }
435
436 static int
437 atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
438 {
439         struct src *src = apcm->src;
440         u32 size, max_cisz;
441         int position;
442
443         position = src->ops->get_ca(src);
444
445         size = apcm->vm_block->size;
446         max_cisz = src->multi * src->rsc.msr;
447         max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
448
449         return (position + size - max_cisz - apcm->vm_block->addr) % size;
450 }
451
452 struct src_node_conf_t {
453         unsigned int pitch;
454         unsigned int msr:8;
455         unsigned int mix_msr:8;
456         unsigned int imp_msr:8;
457         unsigned int vo:1;
458 };
459
460 static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
461                                 struct src_node_conf_t *conf, int *n_srcc)
462 {
463         unsigned int pitch;
464
465         /* get pitch and convert to fixed-point 8.24 format. */
466         pitch = atc_get_pitch((atc->rsr * atc->msr),
467                                 apcm->substream->runtime->rate);
468         *n_srcc = 0;
469
470         if (1 == atc->msr) {
471                 *n_srcc = apcm->substream->runtime->channels;
472                 conf[0].pitch = pitch;
473                 conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
474                 conf[0].vo = 1;
475         } else if (2 == atc->msr) {
476                 if (0x8000000 < pitch) {
477                         /* Need two-stage SRCs, SRCIMPs and
478                          * AMIXERs for converting format */
479                         conf[0].pitch = (atc->msr << 24);
480                         conf[0].msr = conf[0].mix_msr = 1;
481                         conf[0].imp_msr = atc->msr;
482                         conf[0].vo = 0;
483                         conf[1].pitch = atc_get_pitch(atc->rsr,
484                                         apcm->substream->runtime->rate);
485                         conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
486                         conf[1].vo = 1;
487                         *n_srcc = apcm->substream->runtime->channels * 2;
488                 } else if (0x1000000 < pitch) {
489                         /* Need one-stage SRCs, SRCIMPs and
490                          * AMIXERs for converting format */
491                         conf[0].pitch = pitch;
492                         conf[0].msr = conf[0].mix_msr
493                                     = conf[0].imp_msr = atc->msr;
494                         conf[0].vo = 1;
495                         *n_srcc = apcm->substream->runtime->channels;
496                 }
497         }
498 }
499
500 static int
501 atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
502 {
503         struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
504         struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
505         struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
506         struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
507         struct src_desc src_dsc = {0};
508         struct src *src;
509         struct srcimp_desc srcimp_dsc = {0};
510         struct srcimp *srcimp;
511         struct amixer_desc mix_dsc = {0};
512         struct sum_desc sum_dsc = {0};
513         unsigned int pitch;
514         int multi, err, i;
515         int n_srcimp, n_amixer, n_srcc, n_sum;
516         struct src_node_conf_t src_node_conf[2] = {{0} };
517
518         /* first release old resources */
519         atc->pcm_release_resources(atc, apcm);
520
521         /* The numbers of converting SRCs and SRCIMPs should be determined
522          * by pitch value. */
523
524         multi = apcm->substream->runtime->channels;
525
526         /* get pitch and convert to fixed-point 8.24 format. */
527         pitch = atc_get_pitch((atc->rsr * atc->msr),
528                                 apcm->substream->runtime->rate);
529
530         setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
531         n_sum = (1 == multi) ? 1 : 0;
532         n_amixer = n_sum * 2 + n_srcc;
533         n_srcimp = n_srcc;
534         if ((multi > 1) && (0x8000000 >= pitch)) {
535                 /* Need extra AMIXERs and SRCIMPs for special treatment
536                  * of interleaved recording of conjugate channels */
537                 n_amixer += multi * atc->msr;
538                 n_srcimp += multi * atc->msr;
539         } else {
540                 n_srcimp += multi;
541         }
542
543         if (n_srcc) {
544                 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
545                 if (NULL == apcm->srccs)
546                         return -ENOMEM;
547         }
548         if (n_amixer) {
549                 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
550                 if (NULL == apcm->amixers) {
551                         err = -ENOMEM;
552                         goto error1;
553                 }
554         }
555         apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
556         if (NULL == apcm->srcimps) {
557                 err = -ENOMEM;
558                 goto error1;
559         }
560
561         /* Allocate SRCs for sample rate conversion if needed */
562         src_dsc.multi = 1;
563         src_dsc.mode = ARCRW;
564         for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
565                 src_dsc.msr = src_node_conf[i/multi].msr;
566                 err = src_mgr->get_src(src_mgr, &src_dsc,
567                                         (struct src **)&apcm->srccs[i]);
568                 if (err)
569                         goto error1;
570
571                 src = apcm->srccs[i];
572                 pitch = src_node_conf[i/multi].pitch;
573                 src->ops->set_pitch(src, pitch);
574                 src->ops->set_rom(src, select_rom(pitch));
575                 src->ops->set_vo(src, src_node_conf[i/multi].vo);
576
577                 apcm->n_srcc++;
578         }
579
580         /* Allocate AMIXERs for routing SRCs of conversion if needed */
581         for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
582                 if (i < (n_sum*2))
583                         mix_dsc.msr = atc->msr;
584                 else if (i < (n_sum*2+n_srcc))
585                         mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
586                 else
587                         mix_dsc.msr = 1;
588
589                 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
590                                         (struct amixer **)&apcm->amixers[i]);
591                 if (err)
592                         goto error1;
593
594                 apcm->n_amixer++;
595         }
596
597         /* Allocate a SUM resource to mix all input channels together */
598         sum_dsc.msr = atc->msr;
599         err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
600         if (err)
601                 goto error1;
602
603         pitch = atc_get_pitch((atc->rsr * atc->msr),
604                                 apcm->substream->runtime->rate);
605         /* Allocate SRCIMP resources */
606         for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
607                 if (i < (n_srcc))
608                         srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
609                 else if (1 == multi)
610                         srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
611                 else
612                         srcimp_dsc.msr = 1;
613
614                 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
615                 if (err)
616                         goto error1;
617
618                 apcm->srcimps[i] = srcimp;
619                 apcm->n_srcimp++;
620         }
621
622         /* Allocate a SRC for writing data to host memory */
623         src_dsc.multi = apcm->substream->runtime->channels;
624         src_dsc.msr = 1;
625         src_dsc.mode = MEMWR;
626         err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
627         if (err)
628                 goto error1;
629
630         src = apcm->src;
631         src->ops->set_pitch(src, pitch);
632
633         /* Set up device virtual mem map */
634         err = ct_map_audio_buffer(atc, apcm);
635         if (err < 0)
636                 goto error1;
637
638         return 0;
639
640 error1:
641         atc_pcm_release_resources(atc, apcm);
642         return err;
643 }
644
645 static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
646 {
647         struct src *src;
648         struct amixer *amixer;
649         struct srcimp *srcimp;
650         struct ct_mixer *mixer = atc->mixer;
651         struct sum *mono;
652         struct rsc *out_ports[8] = {NULL};
653         int err, i, j, n_sum, multi;
654         unsigned int pitch;
655         int mix_base = 0, imp_base = 0;
656
657         if (NULL != apcm->src) {
658                 /* Prepared pcm capture */
659                 return 0;
660         }
661
662         /* Get needed resources. */
663         err = atc_pcm_capture_get_resources(atc, apcm);
664         if (err)
665                 return err;
666
667         /* Connect resources */
668         mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
669                                 &out_ports[0], &out_ports[1]);
670
671         multi = apcm->substream->runtime->channels;
672         if (1 == multi) {
673                 mono = apcm->mono;
674                 for (i = 0; i < 2; i++) {
675                         amixer = apcm->amixers[i];
676                         amixer->ops->setup(amixer, out_ports[i],
677                                                 MONO_SUM_SCALE, mono);
678                 }
679                 out_ports[0] = &mono->rsc;
680                 n_sum = 1;
681                 mix_base = n_sum * 2;
682         }
683
684         for (i = 0; i < apcm->n_srcc; i++) {
685                 src = apcm->srccs[i];
686                 srcimp = apcm->srcimps[imp_base+i];
687                 amixer = apcm->amixers[mix_base+i];
688                 srcimp->ops->map(srcimp, src, out_ports[i%multi]);
689                 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
690                 out_ports[i%multi] = &amixer->rsc;
691         }
692
693         pitch = atc_get_pitch((atc->rsr * atc->msr),
694                                 apcm->substream->runtime->rate);
695
696         if ((multi > 1) && (pitch <= 0x8000000)) {
697                 /* Special connection for interleaved
698                  * recording with conjugate channels */
699                 for (i = 0; i < multi; i++) {
700                         out_ports[i]->ops->master(out_ports[i]);
701                         for (j = 0; j < atc->msr; j++) {
702                                 amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
703                                 amixer->ops->set_input(amixer, out_ports[i]);
704                                 amixer->ops->set_scale(amixer, INIT_VOL);
705                                 amixer->ops->set_sum(amixer, NULL);
706                                 amixer->ops->commit_raw_write(amixer);
707                                 out_ports[i]->ops->next_conj(out_ports[i]);
708
709                                 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
710                                 srcimp->ops->map(srcimp, apcm->src,
711                                                         &amixer->rsc);
712                         }
713                 }
714         } else {
715                 for (i = 0; i < multi; i++) {
716                         srcimp = apcm->srcimps[apcm->n_srcc+i];
717                         srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
718                 }
719         }
720
721         ct_timer_prepare(apcm->timer);
722
723         return 0;
724 }
725
726 static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
727 {
728         struct src *src;
729         struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
730         int i, multi;
731
732         if (apcm->started)
733                 return 0;
734
735         apcm->started = 1;
736         multi = apcm->substream->runtime->channels;
737         /* Set up converting SRCs */
738         for (i = 0; i < apcm->n_srcc; i++) {
739                 src = apcm->srccs[i];
740                 src->ops->set_pm(src, ((i%multi) != (multi-1)));
741                 src_mgr->src_disable(src_mgr, src);
742         }
743
744         /*  Set up recording SRC */
745         src = apcm->src;
746         src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
747         src->ops->set_sa(src, apcm->vm_block->addr);
748         src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
749         src->ops->set_ca(src, apcm->vm_block->addr);
750         src_mgr->src_disable(src_mgr, src);
751
752         /* Disable relevant SRCs firstly */
753         src_mgr->commit_write(src_mgr);
754
755         /* Enable SRCs respectively */
756         for (i = 0; i < apcm->n_srcc; i++) {
757                 src = apcm->srccs[i];
758                 src->ops->set_state(src, SRC_STATE_RUN);
759                 src->ops->commit_write(src);
760                 src_mgr->src_enable_s(src_mgr, src);
761         }
762         src = apcm->src;
763         src->ops->set_bm(src, 1);
764         src->ops->set_state(src, SRC_STATE_RUN);
765         src->ops->commit_write(src);
766         src_mgr->src_enable_s(src_mgr, src);
767
768         /* Enable relevant SRCs synchronously */
769         src_mgr->commit_write(src_mgr);
770
771         ct_timer_start(apcm->timer);
772         return 0;
773 }
774
775 static int
776 atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
777 {
778         struct src *src = apcm->src;
779
780         return src->ops->get_ca(src) - apcm->vm_block->addr;
781 }
782
783 static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
784                                                  struct ct_atc_pcm *apcm)
785 {
786         struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
787         struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
788         struct src_desc desc = {0};
789         struct amixer_desc mix_dsc = {0};
790         struct src *src;
791         int err;
792         int n_amixer = apcm->substream->runtime->channels, i;
793         unsigned int pitch, rsr = atc->pll_rate;
794
795         /* first release old resources */
796         atc->pcm_release_resources(atc, apcm);
797
798         /* Get SRC resource */
799         desc.multi = apcm->substream->runtime->channels;
800         desc.msr = 1;
801         while (apcm->substream->runtime->rate > (rsr * desc.msr))
802                 desc.msr <<= 1;
803
804         desc.mode = MEMRD;
805         err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
806         if (err)
807                 goto error1;
808
809         pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
810         src = apcm->src;
811         src->ops->set_pitch(src, pitch);
812         src->ops->set_rom(src, select_rom(pitch));
813         src->ops->set_sf(src, convert_format(apcm->substream->runtime->format));
814         src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
815         src->ops->set_bp(src, 1);
816
817         /* Get AMIXER resource */
818         n_amixer = (n_amixer < 2) ? 2 : n_amixer;
819         apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
820         if (NULL == apcm->amixers) {
821                 err = -ENOMEM;
822                 goto error1;
823         }
824         mix_dsc.msr = desc.msr;
825         for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
826                 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
827                                         (struct amixer **)&apcm->amixers[i]);
828                 if (err)
829                         goto error1;
830
831                 apcm->n_amixer++;
832         }
833
834         /* Set up device virtual mem map */
835         err = ct_map_audio_buffer(atc, apcm);
836         if (err < 0)
837                 goto error1;
838
839         return 0;
840
841 error1:
842         atc_pcm_release_resources(atc, apcm);
843         return err;
844 }
845
846 static int atc_pll_init(struct ct_atc *atc, int rate)
847 {
848         struct hw *hw = atc->hw;
849         int err;
850         err = hw->pll_init(hw, rate);
851         atc->pll_rate = err ? 0 : rate;
852         return err;
853 }
854
855 static int
856 spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
857 {
858         struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
859         unsigned long flags;
860         unsigned int rate = apcm->substream->runtime->rate;
861         unsigned int status;
862         int err;
863         unsigned char iec958_con_fs;
864
865         switch (rate) {
866         case 48000:
867                 iec958_con_fs = IEC958_AES3_CON_FS_48000;
868                 break;
869         case 44100:
870                 iec958_con_fs = IEC958_AES3_CON_FS_44100;
871                 break;
872         case 32000:
873                 iec958_con_fs = IEC958_AES3_CON_FS_32000;
874                 break;
875         default:
876                 return -ENOENT;
877         }
878
879         spin_lock_irqsave(&atc->atc_lock, flags);
880         dao->ops->get_spos(dao, &status);
881         if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
882                 status &= ((~IEC958_AES3_CON_FS) << 24);
883                 status |= (iec958_con_fs << 24);
884                 dao->ops->set_spos(dao, status);
885                 dao->ops->commit_write(dao);
886         }
887         if ((rate != atc->pll_rate) && (32000 != rate))
888                 err = atc_pll_init(atc, rate);
889         spin_unlock_irqrestore(&atc->atc_lock, flags);
890
891         return err;
892 }
893
894 static int
895 spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
896 {
897         struct src *src;
898         struct amixer *amixer;
899         struct dao *dao;
900         int err;
901         int i;
902         unsigned long flags;
903
904         if (NULL != apcm->src)
905                 return 0;
906
907         /* Configure SPDIFOO and PLL to passthrough mode;
908          * determine pll_rate. */
909         err = spdif_passthru_playback_setup(atc, apcm);
910         if (err)
911                 return err;
912
913         /* Get needed resources. */
914         err = spdif_passthru_playback_get_resources(atc, apcm);
915         if (err)
916                 return err;
917
918         /* Connect resources */
919         src = apcm->src;
920         for (i = 0; i < apcm->n_amixer; i++) {
921                 amixer = apcm->amixers[i];
922                 amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
923                 src = src->ops->next_interleave(src);
924                 if (NULL == src)
925                         src = apcm->src;
926         }
927         /* Connect to SPDIFOO */
928         spin_lock_irqsave(&atc->atc_lock, flags);
929         dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
930         amixer = apcm->amixers[0];
931         dao->ops->set_left_input(dao, &amixer->rsc);
932         amixer = apcm->amixers[1];
933         dao->ops->set_right_input(dao, &amixer->rsc);
934         spin_unlock_irqrestore(&atc->atc_lock, flags);
935
936         ct_timer_prepare(apcm->timer);
937
938         return 0;
939 }
940
941 static int atc_select_line_in(struct ct_atc *atc)
942 {
943         struct hw *hw = atc->hw;
944         struct ct_mixer *mixer = atc->mixer;
945         struct src *src;
946
947         if (hw->is_adc_source_selected(hw, ADC_LINEIN))
948                 return 0;
949
950         mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
951         mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
952
953         hw->select_adc_source(hw, ADC_LINEIN);
954
955         src = atc->srcs[2];
956         mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
957         src = atc->srcs[3];
958         mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
959
960         return 0;
961 }
962
963 static int atc_select_mic_in(struct ct_atc *atc)
964 {
965         struct hw *hw = atc->hw;
966         struct ct_mixer *mixer = atc->mixer;
967         struct src *src;
968
969         if (hw->is_adc_source_selected(hw, ADC_MICIN))
970                 return 0;
971
972         mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
973         mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
974
975         hw->select_adc_source(hw, ADC_MICIN);
976
977         src = atc->srcs[2];
978         mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
979         src = atc->srcs[3];
980         mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
981
982         return 0;
983 }
984
985 static int atc_have_digit_io_switch(struct ct_atc *atc)
986 {
987         struct hw *hw = atc->hw;
988
989         return hw->have_digit_io_switch(hw);
990 }
991
992 static int atc_select_digit_io(struct ct_atc *atc)
993 {
994         struct hw *hw = atc->hw;
995
996         if (hw->is_adc_source_selected(hw, ADC_NONE))
997                 return 0;
998
999         hw->select_adc_source(hw, ADC_NONE);
1000
1001         return 0;
1002 }
1003
1004 static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
1005 {
1006         struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
1007
1008         if (state)
1009                 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
1010         else
1011                 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
1012
1013         daio_mgr->commit_write(daio_mgr);
1014
1015         return 0;
1016 }
1017
1018 static int
1019 atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
1020 {
1021         struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1022         return dao->ops->get_spos(dao, status);
1023 }
1024
1025 static int
1026 atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
1027 {
1028         struct dao *dao = container_of(atc->daios[type], struct dao, daio);
1029
1030         dao->ops->set_spos(dao, status);
1031         dao->ops->commit_write(dao);
1032         return 0;
1033 }
1034
1035 static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1036 {
1037         return atc_daio_unmute(atc, state, LINEO1);
1038 }
1039
1040 static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1041 {
1042         return atc_daio_unmute(atc, state, LINEO4);
1043 }
1044
1045 static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1046 {
1047         return atc_daio_unmute(atc, state, LINEO3);
1048 }
1049
1050 static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1051 {
1052         return atc_daio_unmute(atc, state, LINEO2);
1053 }
1054
1055 static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1056 {
1057         return atc_daio_unmute(atc, state, LINEIM);
1058 }
1059
1060 static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1061 {
1062         return atc_daio_unmute(atc, state, SPDIFOO);
1063 }
1064
1065 static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1066 {
1067         return atc_daio_unmute(atc, state, SPDIFIO);
1068 }
1069
1070 static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1071 {
1072         return atc_dao_get_status(atc, status, SPDIFOO);
1073 }
1074
1075 static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1076 {
1077         return atc_dao_set_status(atc, status, SPDIFOO);
1078 }
1079
1080 static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1081 {
1082         unsigned long flags;
1083         struct dao_desc da_dsc = {0};
1084         struct dao *dao;
1085         int err;
1086         struct ct_mixer *mixer = atc->mixer;
1087         struct rsc *rscs[2] = {NULL};
1088         unsigned int spos = 0;
1089
1090         spin_lock_irqsave(&atc->atc_lock, flags);
1091         dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
1092         da_dsc.msr = state ? 1 : atc->msr;
1093         da_dsc.passthru = state ? 1 : 0;
1094         err = dao->ops->reinit(dao, &da_dsc);
1095         if (state) {
1096                 spos = IEC958_DEFAULT_CON;
1097         } else {
1098                 mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
1099                                         &rscs[0], &rscs[1]);
1100                 dao->ops->set_left_input(dao, rscs[0]);
1101                 dao->ops->set_right_input(dao, rscs[1]);
1102                 /* Restore PLL to atc->rsr if needed. */
1103                 if (atc->pll_rate != atc->rsr)
1104                         err = atc_pll_init(atc, atc->rsr);
1105         }
1106         dao->ops->set_spos(dao, spos);
1107         dao->ops->commit_write(dao);
1108         spin_unlock_irqrestore(&atc->atc_lock, flags);
1109
1110         return err;
1111 }
1112
1113 static int ct_atc_destroy(struct ct_atc *atc)
1114 {
1115         struct daio_mgr *daio_mgr;
1116         struct dao *dao;
1117         struct dai *dai;
1118         struct daio *daio;
1119         struct sum_mgr *sum_mgr;
1120         struct src_mgr *src_mgr;
1121         struct srcimp_mgr *srcimp_mgr;
1122         struct srcimp *srcimp;
1123         struct ct_mixer *mixer;
1124         int i = 0;
1125
1126         if (NULL == atc)
1127                 return 0;
1128
1129         if (atc->timer) {
1130                 ct_timer_free(atc->timer);
1131                 atc->timer = NULL;
1132         }
1133
1134         /* Stop hardware and disable all interrupts */
1135         if (NULL != atc->hw)
1136                 ((struct hw *)atc->hw)->card_stop(atc->hw);
1137
1138         /* Destroy internal mixer objects */
1139         if (NULL != atc->mixer) {
1140                 mixer = atc->mixer;
1141                 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
1142                 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
1143                 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
1144                 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
1145                 mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
1146                 mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
1147                 ct_mixer_destroy(atc->mixer);
1148         }
1149
1150         if (NULL != atc->daios) {
1151                 daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1152                 for (i = 0; i < atc->n_daio; i++) {
1153                         daio = atc->daios[i];
1154                         if (daio->type < LINEIM) {
1155                                 dao = container_of(daio, struct dao, daio);
1156                                 dao->ops->clear_left_input(dao);
1157                                 dao->ops->clear_right_input(dao);
1158                         } else {
1159                                 dai = container_of(daio, struct dai, daio);
1160                                 /* some thing to do for dai ... */
1161                         }
1162                         daio_mgr->put_daio(daio_mgr, daio);
1163                 }
1164                 kfree(atc->daios);
1165         }
1166
1167         if (NULL != atc->pcm) {
1168                 sum_mgr = atc->rsc_mgrs[SUM];
1169                 for (i = 0; i < atc->n_pcm; i++)
1170                         sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
1171
1172                 kfree(atc->pcm);
1173         }
1174
1175         if (NULL != atc->srcs) {
1176                 src_mgr = atc->rsc_mgrs[SRC];
1177                 for (i = 0; i < atc->n_src; i++)
1178                         src_mgr->put_src(src_mgr, atc->srcs[i]);
1179
1180                 kfree(atc->srcs);
1181         }
1182
1183         if (NULL != atc->srcimps) {
1184                 srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1185                 for (i = 0; i < atc->n_srcimp; i++) {
1186                         srcimp = atc->srcimps[i];
1187                         srcimp->ops->unmap(srcimp);
1188                         srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
1189                 }
1190                 kfree(atc->srcimps);
1191         }
1192
1193         for (i = 0; i < NUM_RSCTYP; i++) {
1194                 if ((NULL != rsc_mgr_funcs[i].destroy) &&
1195                     (NULL != atc->rsc_mgrs[i]))
1196                         rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
1197
1198         }
1199
1200         if (NULL != atc->hw)
1201                 destroy_hw_obj((struct hw *)atc->hw);
1202
1203         /* Destroy device virtual memory manager object */
1204         if (NULL != atc->vm) {
1205                 ct_vm_destroy(atc->vm);
1206                 atc->vm = NULL;
1207         }
1208
1209         kfree(atc);
1210
1211         return 0;
1212 }
1213
1214 static int atc_dev_free(struct snd_device *dev)
1215 {
1216         struct ct_atc *atc = dev->device_data;
1217         return ct_atc_destroy(atc);
1218 }
1219
1220 static int __devinit atc_identify_card(struct ct_atc *atc)
1221 {
1222         const struct snd_pci_quirk *p;
1223         const struct snd_pci_quirk *list;
1224
1225         switch (atc->chip_type) {
1226         case ATC20K1:
1227                 atc->chip_name = "20K1";
1228                 list = subsys_20k1_list;
1229                 break;
1230         case ATC20K2:
1231                 atc->chip_name = "20K2";
1232                 list = subsys_20k2_list;
1233                 break;
1234         default:
1235                 return -ENOENT;
1236         }
1237         p = snd_pci_quirk_lookup(atc->pci, list);
1238         if (!p)
1239                 return -ENOENT;
1240         atc->model = p->value;
1241         atc->model_name = ct_subsys_name[atc->model];
1242         snd_printd("ctxfi: chip %s model %s (%04x:%04x) is found\n",
1243                    atc->chip_name, atc->model_name,
1244                    atc->pci->subsystem_vendor,
1245                    atc->pci->subsystem_device);
1246         return 0;
1247 }
1248
1249 int __devinit ct_atc_create_alsa_devs(struct ct_atc *atc)
1250 {
1251         enum CTALSADEVS i;
1252         int err;
1253
1254         alsa_dev_funcs[MIXER].public_name = atc->chip_name;
1255
1256         for (i = 0; i < NUM_CTALSADEVS; i++) {
1257                 if (NULL == alsa_dev_funcs[i].create)
1258                         continue;
1259
1260                 err = alsa_dev_funcs[i].create(atc, i,
1261                                 alsa_dev_funcs[i].public_name);
1262                 if (err) {
1263                         printk(KERN_ERR "ctxfi: "
1264                                "Creating alsa device %d failed!\n", i);
1265                         return err;
1266                 }
1267         }
1268
1269         return 0;
1270 }
1271
1272 static int __devinit atc_create_hw_devs(struct ct_atc *atc)
1273 {
1274         struct hw *hw;
1275         struct card_conf info = {0};
1276         int i, err;
1277
1278         err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
1279         if (err) {
1280                 printk(KERN_ERR "Failed to create hw obj!!!\n");
1281                 return err;
1282         }
1283         atc->hw = hw;
1284
1285         /* Initialize card hardware. */
1286         info.rsr = atc->rsr;
1287         info.msr = atc->msr;
1288         info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
1289         err = hw->card_init(hw, &info);
1290         if (err < 0)
1291                 return err;
1292
1293         for (i = 0; i < NUM_RSCTYP; i++) {
1294                 if (NULL == rsc_mgr_funcs[i].create)
1295                         continue;
1296
1297                 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1298                 if (err) {
1299                         printk(KERN_ERR "ctxfi: "
1300                                "Failed to create rsc_mgr %d!!!\n", i);
1301                         return err;
1302                 }
1303         }
1304
1305         return 0;
1306 }
1307
1308 static int __devinit atc_get_resources(struct ct_atc *atc)
1309 {
1310         struct daio_desc da_desc = {0};
1311         struct daio_mgr *daio_mgr;
1312         struct src_desc src_dsc = {0};
1313         struct src_mgr *src_mgr;
1314         struct srcimp_desc srcimp_dsc = {0};
1315         struct srcimp_mgr *srcimp_mgr;
1316         struct sum_desc sum_dsc = {0};
1317         struct sum_mgr *sum_mgr;
1318         int err, i;
1319
1320         atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1321         if (NULL == atc->daios)
1322                 return -ENOMEM;
1323
1324         atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1325         if (NULL == atc->srcs)
1326                 return -ENOMEM;
1327
1328         atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1329         if (NULL == atc->srcimps)
1330                 return -ENOMEM;
1331
1332         atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1333         if (NULL == atc->pcm)
1334                 return -ENOMEM;
1335
1336         daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
1337         da_desc.msr = atc->msr;
1338         for (i = 0, atc->n_daio = 0; i < DAIONUM-1; i++) {
1339                 da_desc.type = i;
1340                 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1341                                         (struct daio **)&atc->daios[i]);
1342                 if (err) {
1343                         printk(KERN_ERR "ctxfi: Failed to get DAIO "
1344                                         "resource %d!!!\n", i);
1345                         return err;
1346                 }
1347                 atc->n_daio++;
1348         }
1349         if (atc->model == CTSB073X)
1350                 da_desc.type = SPDIFI1;
1351         else
1352                 da_desc.type = SPDIFIO;
1353         err = daio_mgr->get_daio(daio_mgr, &da_desc,
1354                                 (struct daio **)&atc->daios[i]);
1355         if (err) {
1356                 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
1357                 return err;
1358         }
1359         atc->n_daio++;
1360
1361         src_mgr = atc->rsc_mgrs[SRC];
1362         src_dsc.multi = 1;
1363         src_dsc.msr = atc->msr;
1364         src_dsc.mode = ARCRW;
1365         for (i = 0, atc->n_src = 0; i < (2*2); i++) {
1366                 err = src_mgr->get_src(src_mgr, &src_dsc,
1367                                         (struct src **)&atc->srcs[i]);
1368                 if (err)
1369                         return err;
1370
1371                 atc->n_src++;
1372         }
1373
1374         srcimp_mgr = atc->rsc_mgrs[SRCIMP];
1375         srcimp_dsc.msr = 8; /* SRCIMPs for S/PDIFIn SRT */
1376         for (i = 0, atc->n_srcimp = 0; i < (2*1); i++) {
1377                 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1378                                         (struct srcimp **)&atc->srcimps[i]);
1379                 if (err)
1380                         return err;
1381
1382                 atc->n_srcimp++;
1383         }
1384         srcimp_dsc.msr = 8; /* SRCIMPs for LINE/MICIn SRT */
1385         for (i = 0; i < (2*1); i++) {
1386                 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
1387                                 (struct srcimp **)&atc->srcimps[2*1+i]);
1388                 if (err)
1389                         return err;
1390
1391                 atc->n_srcimp++;
1392         }
1393
1394         sum_mgr = atc->rsc_mgrs[SUM];
1395         sum_dsc.msr = atc->msr;
1396         for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
1397                 err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
1398                                         (struct sum **)&atc->pcm[i]);
1399                 if (err)
1400                         return err;
1401
1402                 atc->n_pcm++;
1403         }
1404
1405         err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1406         if (err) {
1407                 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1408                 return err;
1409         }
1410
1411         return 0;
1412 }
1413
1414 static void __devinit
1415 atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1416                 struct src **srcs, struct srcimp **srcimps)
1417 {
1418         struct rsc *rscs[2] = {NULL};
1419         struct src *src;
1420         struct srcimp *srcimp;
1421         int i = 0;
1422
1423         rscs[0] = &dai->daio.rscl;
1424         rscs[1] = &dai->daio.rscr;
1425         for (i = 0; i < 2; i++) {
1426                 src = srcs[i];
1427                 srcimp = srcimps[i];
1428                 srcimp->ops->map(srcimp, src, rscs[i]);
1429                 src_mgr->src_disable(src_mgr, src);
1430         }
1431
1432         src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1433
1434         src = srcs[0];
1435         src->ops->set_pm(src, 1);
1436         for (i = 0; i < 2; i++) {
1437                 src = srcs[i];
1438                 src->ops->set_state(src, SRC_STATE_RUN);
1439                 src->ops->commit_write(src);
1440                 src_mgr->src_enable_s(src_mgr, src);
1441         }
1442
1443         dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1444         dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1445
1446         dai->ops->set_enb_src(dai, 1);
1447         dai->ops->set_enb_srt(dai, 1);
1448         dai->ops->commit_write(dai);
1449
1450         src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1451 }
1452
1453 static void __devinit atc_connect_resources(struct ct_atc *atc)
1454 {
1455         struct dai *dai;
1456         struct dao *dao;
1457         struct src *src;
1458         struct sum *sum;
1459         struct ct_mixer *mixer;
1460         struct rsc *rscs[2] = {NULL};
1461         int i, j;
1462
1463         mixer = atc->mixer;
1464
1465         for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
1466                 mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
1467                 dao = container_of(atc->daios[j], struct dao, daio);
1468                 dao->ops->set_left_input(dao, rscs[0]);
1469                 dao->ops->set_right_input(dao, rscs[1]);
1470         }
1471
1472         dai = container_of(atc->daios[LINEIM], struct dai, daio);
1473         atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1474                         (struct src **)&atc->srcs[2],
1475                         (struct srcimp **)&atc->srcimps[2]);
1476         src = atc->srcs[2];
1477         mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1478         src = atc->srcs[3];
1479         mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
1480
1481         dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
1482         atc_connect_dai(atc->rsc_mgrs[SRC], dai,
1483                         (struct src **)&atc->srcs[0],
1484                         (struct srcimp **)&atc->srcimps[0]);
1485
1486         src = atc->srcs[0];
1487         mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1488         src = atc->srcs[1];
1489         mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1490
1491         for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1492                 sum = atc->pcm[j];
1493                 mixer->set_input_left(mixer, i, &sum->rsc);
1494                 sum = atc->pcm[j+1];
1495                 mixer->set_input_right(mixer, i, &sum->rsc);
1496         }
1497 }
1498
1499 static struct ct_atc atc_preset __devinitdata = {
1500         .map_audio_buffer = ct_map_audio_buffer,
1501         .unmap_audio_buffer = ct_unmap_audio_buffer,
1502         .pcm_playback_prepare = atc_pcm_playback_prepare,
1503         .pcm_release_resources = atc_pcm_release_resources,
1504         .pcm_playback_start = atc_pcm_playback_start,
1505         .pcm_playback_stop = atc_pcm_stop,
1506         .pcm_playback_position = atc_pcm_playback_position,
1507         .pcm_capture_prepare = atc_pcm_capture_prepare,
1508         .pcm_capture_start = atc_pcm_capture_start,
1509         .pcm_capture_stop = atc_pcm_stop,
1510         .pcm_capture_position = atc_pcm_capture_position,
1511         .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
1512         .get_ptp_phys = atc_get_ptp_phys,
1513         .select_line_in = atc_select_line_in,
1514         .select_mic_in = atc_select_mic_in,
1515         .select_digit_io = atc_select_digit_io,
1516         .line_front_unmute = atc_line_front_unmute,
1517         .line_surround_unmute = atc_line_surround_unmute,
1518         .line_clfe_unmute = atc_line_clfe_unmute,
1519         .line_rear_unmute = atc_line_rear_unmute,
1520         .line_in_unmute = atc_line_in_unmute,
1521         .spdif_out_unmute = atc_spdif_out_unmute,
1522         .spdif_in_unmute = atc_spdif_in_unmute,
1523         .spdif_out_get_status = atc_spdif_out_get_status,
1524         .spdif_out_set_status = atc_spdif_out_set_status,
1525         .spdif_out_passthru = atc_spdif_out_passthru,
1526         .have_digit_io_switch = atc_have_digit_io_switch,
1527 };
1528
1529 /**
1530  *  ct_atc_create - create and initialize a hardware manager
1531  *  @card: corresponding alsa card object
1532  *  @pci: corresponding kernel pci device object
1533  *  @ratc: return created object address in it
1534  *
1535  *  Creates and initializes a hardware manager.
1536  *
1537  *  Creates kmallocated ct_atc structure. Initializes hardware.
1538  *  Returns 0 if suceeds, or negative error code if fails.
1539  */
1540
1541 int __devinit ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1542                             unsigned int rsr, unsigned int msr,
1543                             int chip_type, struct ct_atc **ratc)
1544 {
1545         struct ct_atc *atc;
1546         static struct snd_device_ops ops = {
1547                 .dev_free = atc_dev_free,
1548         };
1549         int err;
1550
1551         *ratc = NULL;
1552
1553         atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1554         if (NULL == atc)
1555                 return -ENOMEM;
1556
1557         /* Set operations */
1558         *atc = atc_preset;
1559
1560         atc->card = card;
1561         atc->pci = pci;
1562         atc->rsr = rsr;
1563         atc->msr = msr;
1564         atc->chip_type = chip_type;
1565
1566         spin_lock_init(&atc->atc_lock);
1567
1568         /* Find card model */
1569         err = atc_identify_card(atc);
1570         if (err < 0) {
1571                 printk(KERN_ERR "ctatc: Card not recognised\n");
1572                 goto error1;
1573         }
1574
1575         /* Set up device virtual memory management object */
1576         err = ct_vm_create(&atc->vm);
1577         if (err < 0)
1578                 goto error1;
1579
1580         /* Create all atc hw devices */
1581         err = atc_create_hw_devs(atc);
1582         if (err < 0)
1583                 goto error1;
1584
1585         /* Get resources */
1586         err = atc_get_resources(atc);
1587         if (err < 0)
1588                 goto error1;
1589
1590         /* Build topology */
1591         atc_connect_resources(atc);
1592
1593         atc->timer = ct_timer_new(atc);
1594         if (!atc->timer)
1595                 goto error1;
1596
1597         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1598         if (err < 0)
1599                 goto error1;
1600
1601         snd_card_set_dev(card, &pci->dev);
1602
1603         *ratc = atc;
1604         return 0;
1605
1606 error1:
1607         ct_atc_destroy(atc);
1608         printk(KERN_ERR "ctxfi: Something wrong!!!\n");
1609         return err;
1610 }