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