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