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