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