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