2 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
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.
11 * This file contains the implementation of the device resource management
21 #include "cthardware.h"
25 #include <linux/delay.h>
26 #include <sound/pcm.h>
27 #include <sound/control.h>
28 #include <sound/asoundef.h>
30 #define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
32 #define MAX_MULTI_CHN 8
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) \
39 | ((IEC958_AES3_CON_FS_48000) << 24))
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,
54 static struct ct_atc_chip_details atc_chip_details[] = {
55 {.vendor = PCI_VENDOR_ID_CREATIVE,
56 .device = PCI_DEVICE_ID_CREATIVE_20K1,
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"},
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,
74 .public_name = "Front/WaveIn"},
75 [REAR] = { .create = ct_alsa_pcm_create,
77 .public_name = "Rear"},
78 [CLFE] = { .create = ct_alsa_pcm_create,
80 .public_name = "Center/LFE"},
81 [SURROUND] = { .create = ct_alsa_pcm_create,
83 .public_name = "Surround"},
84 [IEC958] = { .create = ct_alsa_pcm_create,
86 .public_name = "IEC958 Non-audio"},
88 [MIXER] = { .create = ct_alsa_mix_create,
90 .public_name = "Mixer"}
93 typedef int (*create_t)(void *, void **);
94 typedef int (*destroy_t)(void *);
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 }
113 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
116 * Only mono and interleaved modes are supported now.
117 * Always allocates a contiguous channel block.
120 static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
122 struct snd_pcm_runtime *runtime;
125 if (NULL == apcm->substream)
128 runtime = apcm->substream->runtime;
131 apcm->vm_block = vm->map(vm, runtime->dma_area, runtime->dma_bytes);
133 if (NULL == apcm->vm_block)
139 static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
143 if (NULL == apcm->vm_block)
148 vm->unmap(vm, apcm->vm_block);
150 apcm->vm_block = NULL;
153 static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
157 unsigned long phys_addr;
160 kvirt_addr = vm->get_ptp_virt(vm, index);
161 if (kvirt_addr == NULL)
164 phys_addr = virt_to_phys(kvirt_addr);
169 static unsigned int convert_format(snd_pcm_format_t snd_format)
171 switch (snd_format) {
172 case SNDRV_PCM_FORMAT_U8:
173 case SNDRV_PCM_FORMAT_S8:
175 case SNDRV_PCM_FORMAT_S16_LE:
176 case SNDRV_PCM_FORMAT_U16_LE:
178 case SNDRV_PCM_FORMAT_S24_3LE:
180 case SNDRV_PCM_FORMAT_S24_LE:
181 case SNDRV_PCM_FORMAT_S32_LE:
184 printk(KERN_ERR "ctxfi: not recognized snd format is %d \n",
191 atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
193 unsigned int pitch = 0;
196 /* get pitch and convert to fixed-point 8.24 format. */
197 pitch = (input_rate / output_rate) << 24;
198 input_rate %= output_rate;
201 for (b = 31; ((b >= 0) && !(input_rate >> b)); )
205 input_rate <<= (31 - b);
206 input_rate /= output_rate;
219 static int select_rom(unsigned int pitch)
221 if ((pitch > 0x00428f5c) && (pitch < 0x01b851ec)) {
222 /* 0.26 <= pitch <= 1.72 */
224 } else if ((0x01d66666 == pitch) || (0x01d66667 == pitch)) {
225 /* pitch == 1.8375 */
227 } else if (0x02000000 == pitch) {
230 } else if ((pitch >= 0x0) && (pitch <= 0x08000000)) {
231 /* 0 <= pitch <= 8 */
238 static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
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;
247 int n_amixer = apcm->substream->runtime->channels, i = 0;
248 int device = apcm->substream->pcm->device;
249 unsigned int pitch = 0;
252 if (NULL != apcm->src) {
253 /* Prepared pcm playback */
257 /* Get SRC resource */
258 desc.multi = apcm->substream->runtime->channels;
261 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
265 pitch = atc_get_pitch(apcm->substream->runtime->rate,
266 (atc->rsr * atc->msr));
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));
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) {
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]);
290 /* Set up device virtual mem map */
291 err = ct_map_audio_buffer(atc, apcm);
295 /* Connect resources */
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);
311 atc_pcm_release_resources(atc, apcm);
316 atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
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;
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;
332 kfree(apcm->srcimps);
333 apcm->srcimps = NULL;
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;
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;
350 kfree(apcm->amixers);
351 apcm->amixers = NULL;
354 if (NULL != apcm->mono) {
355 sum_mgr->put_sum(sum_mgr, apcm->mono);
359 if (NULL != apcm->src) {
360 src_mgr->put_src(src_mgr, apcm->src);
364 if (NULL != apcm->vm_block) {
365 /* Undo device virtual mem map */
366 ct_unmap_audio_buffer(atc, apcm);
367 apcm->vm_block = NULL;
373 static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
375 unsigned int max_cisz = 0;
376 struct src *src = apcm->src;
378 max_cisz = src->multi * src->rsc.msr;
379 max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
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);
386 src->ops->set_bm(src, 1);
387 src->ops->set_state(src, SRC_STATE_INIT);
388 src->ops->commit_write(src);
393 static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
395 struct src *src = NULL;
399 src->ops->set_bm(src, 0);
400 src->ops->set_state(src, SRC_STATE_OFF);
401 src->ops->commit_write(src);
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);
418 atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
420 struct src *src = apcm->src;
421 u32 size = 0, max_cisz = 0;
424 position = src->ops->get_ca(src);
426 size = apcm->vm_block->size;
427 max_cisz = src->multi * src->rsc.msr;
428 max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
430 return (position + size - max_cisz - apcm->vm_block->addr) % size;
433 struct src_node_conf_t {
436 unsigned int mix_msr:8;
437 unsigned int imp_msr:8;
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)
444 unsigned int pitch = 0;
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);
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;
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;
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;
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;
476 *n_srcc = apcm->substream->runtime->channels;
482 atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
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} };
499 /* The numbers of converting SRCs and SRCIMPs should be determined
502 multi = apcm->substream->runtime->channels;
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);
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;
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;
522 apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
523 if (NULL == apcm->srccs)
527 apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
528 if (NULL == apcm->amixers) {
533 apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
534 if (NULL == apcm->srcimps) {
539 /* Allocate SRCs for sample rate conversion if needed */
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]);
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);
558 /* Allocate AMIXERs for routing SRCs of conversion if needed */
559 for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
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;
567 err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
568 (struct amixer **)&apcm->amixers[i]);
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);
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++) {
586 srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
588 srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
592 err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
596 apcm->srcimps[i] = srcimp;
600 /* Allocate a SRC for writing data to host memory */
601 src_dsc.multi = apcm->substream->runtime->channels;
603 src_dsc.mode = MEMWR;
604 err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
609 src->ops->set_pitch(src, pitch);
611 /* Set up device virtual mem map */
612 err = ct_map_audio_buffer(atc, apcm);
619 atc_pcm_release_resources(atc, apcm);
623 static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
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;
635 if (NULL != apcm->src) {
636 /* Prepared pcm capture */
640 /* Get needed resources. */
641 err = atc_pcm_capture_get_resources(atc, apcm);
645 /* Connect resources */
646 mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
647 &out_ports[0], &out_ports[1]);
649 multi = apcm->substream->runtime->channels;
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);
657 out_ports[0] = &mono->rsc;
659 mix_base = n_sum * 2;
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;
671 pitch = atc_get_pitch((atc->rsr * atc->msr),
672 apcm->substream->runtime->rate);
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]);
687 srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
688 srcimp->ops->map(srcimp, apcm->src,
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]);
702 static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
704 struct src *src = NULL;
705 struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
706 int i = 0, multi = 0;
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);
720 /* Set up recording 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);
728 /* Disable relevant SRCs firstly */
729 src_mgr->commit_write(src_mgr);
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);
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);
744 /* Enable relevant SRCs synchronously */
745 src_mgr->commit_write(src_mgr);
751 atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
753 struct src *src = apcm->src;
755 return src->ops->get_ca(src) - apcm->vm_block->addr;
758 static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
759 struct ct_atc_pcm *apcm)
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;
767 int n_amixer = apcm->substream->runtime->channels, i = 0;
768 unsigned int pitch = 0, rsr = atc->pll_rate;
770 /* Get SRC resource */
771 desc.multi = apcm->substream->runtime->channels;
773 while (apcm->substream->runtime->rate > (rsr * desc.msr))
777 err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
781 pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
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);
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) {
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]);
806 /* Set up device virtual mem map */
807 err = ct_map_audio_buffer(atc, apcm);
814 atc_pcm_release_resources(atc, apcm);
819 spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
821 struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
823 unsigned int rate = apcm->substream->runtime->rate;
824 unsigned int status = 0;
826 unsigned char iec958_con_fs = 0;
830 iec958_con_fs = IEC958_AES3_CON_FS_48000;
833 iec958_con_fs = IEC958_AES3_CON_FS_44100;
836 iec958_con_fs = IEC958_AES3_CON_FS_32000;
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);
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;
854 spin_unlock_irqrestore(&atc->atc_lock, flags);
860 spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
862 struct src *src = NULL;
863 struct amixer *amixer = NULL;
864 struct dao *dao = NULL;
869 if (NULL != apcm->src)
872 /* Configure SPDIFOO and PLL to passthrough mode;
873 * determine pll_rate. */
874 err = spdif_passthru_playback_setup(atc, apcm);
878 /* Get needed resources. */
879 err = spdif_passthru_playback_get_resources(atc, apcm);
883 /* Connect resources */
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);
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);
904 static int atc_select_line_in(struct ct_atc *atc)
906 struct hw *hw = atc->hw;
907 struct ct_mixer *mixer = atc->mixer;
908 struct src *src = NULL;
910 if (hw->is_adc_source_selected(hw, ADC_LINEIN))
913 mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
914 mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
916 hw->select_adc_source(hw, ADC_LINEIN);
919 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
921 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
926 static int atc_select_mic_in(struct ct_atc *atc)
928 struct hw *hw = atc->hw;
929 struct ct_mixer *mixer = atc->mixer;
930 struct src *src = NULL;
932 if (hw->is_adc_source_selected(hw, ADC_MICIN))
935 mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
936 mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
938 hw->select_adc_source(hw, ADC_MICIN);
941 mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
943 mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
948 static int atc_have_digit_io_switch(struct ct_atc *atc)
950 struct hw *hw = atc->hw;
952 return hw->have_digit_io_switch(hw);
955 static int atc_select_digit_io(struct ct_atc *atc)
957 struct hw *hw = atc->hw;
959 if (hw->is_adc_source_selected(hw, ADC_NONE))
962 hw->select_adc_source(hw, ADC_NONE);
967 static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
969 struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
972 daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
974 daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
976 daio_mgr->commit_write(daio_mgr);
982 atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
984 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
985 return dao->ops->get_spos(dao, status);
989 atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
991 struct dao *dao = container_of(atc->daios[type], struct dao, daio);
993 dao->ops->set_spos(dao, status);
994 dao->ops->commit_write(dao);
998 static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
1000 return atc_daio_unmute(atc, state, LINEO1);
1003 static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
1005 return atc_daio_unmute(atc, state, LINEO4);
1008 static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
1010 return atc_daio_unmute(atc, state, LINEO3);
1013 static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
1015 return atc_daio_unmute(atc, state, LINEO2);
1018 static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
1020 return atc_daio_unmute(atc, state, LINEIM);
1023 static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
1025 return atc_daio_unmute(atc, state, SPDIFOO);
1028 static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
1030 return atc_daio_unmute(atc, state, SPDIFIO);
1033 static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
1035 return atc_dao_get_status(atc, status, SPDIFOO);
1038 static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
1040 return atc_dao_set_status(atc, status, SPDIFOO);
1043 static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
1045 unsigned long flags;
1046 struct dao_desc da_dsc = {0};
1047 struct dao *dao = NULL;
1049 struct ct_mixer *mixer = atc->mixer;
1050 struct rsc *rscs[2] = {NULL};
1051 unsigned int spos = 0;
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);
1059 spos = IEC958_DEFAULT_CON;
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,
1069 atc->pll_rate = err ? 0 : atc->rsr;
1072 dao->ops->set_spos(dao, spos);
1073 dao->ops->commit_write(dao);
1074 spin_unlock_irqrestore(&atc->atc_lock, flags);
1079 static int ct_atc_destroy(struct ct_atc *atc)
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;
1095 /* Stop hardware and disable all interrupts */
1096 if (NULL != atc->hw)
1097 ((struct hw *)atc->hw)->card_stop(atc->hw);
1099 /* Destroy internal mixer objects */
1100 if (NULL != 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);
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);
1120 dai = container_of(daio, struct dai, daio);
1121 /* some thing to do for dai ... */
1123 daio_mgr->put_daio(daio_mgr, daio);
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]);
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]);
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]);
1151 kfree(atc->srcimps);
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]);
1161 if (NULL != atc->hw)
1162 destroy_hw_obj((struct hw *)atc->hw);
1164 /* Destroy device virtual memory manager object */
1165 if (NULL != atc->vm) {
1166 ct_vm_destroy(atc->vm);
1175 static int atc_dev_free(struct snd_device *dev)
1177 struct ct_atc *atc = dev->device_data;
1178 return ct_atc_destroy(atc);
1181 static int atc_identify_card(struct ct_atc *atc)
1185 struct pci_dev *pci = atc->pci;
1186 const struct ct_atc_chip_details *d;
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)
1197 if (NULL == d->sub_details) {
1198 atc->chip_details = d;
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))) {
1209 if (i >= NUM_CTCARDS)
1212 atc->chip_details = d;
1214 /* not take revision into consideration now */
1222 static int ct_create_alsa_devs(struct ct_atc *atc)
1225 struct hw *hw = atc->hw;
1228 switch (hw->get_chip_type(hw)) {
1230 alsa_dev_funcs[MIXER].public_name = "20K1";
1233 alsa_dev_funcs[MIXER].public_name = "20K2";
1236 alsa_dev_funcs[MIXER].public_name = "Unknown";
1240 for (i = 0; i < NUM_CTALSADEVS; i++) {
1241 if (NULL == alsa_dev_funcs[i].create)
1244 err = alsa_dev_funcs[i].create(atc, i,
1245 alsa_dev_funcs[i].public_name);
1247 printk(KERN_ERR "ctxfi: "
1248 "Creating alsa device %d failed!\n", i);
1256 static int atc_create_hw_devs(struct ct_atc *atc)
1258 struct hw *hw = NULL;
1259 struct card_conf info = {0};
1262 err = create_hw_obj(atc->pci, &hw);
1264 printk(KERN_ERR "Failed to create hw obj!!!\n");
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);
1277 for (i = 0; i < NUM_RSCTYP; i++) {
1278 if (NULL == rsc_mgr_funcs[i].create)
1281 err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
1283 printk(KERN_ERR "ctxfi: "
1284 "Failed to create rsc_mgr %d!!!\n", i);
1292 static int atc_get_resources(struct ct_atc *atc)
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;
1303 unsigned short subsys_id;
1305 atc->daios = kzalloc(sizeof(void *)*(DAIONUM), GFP_KERNEL);
1306 if (NULL == atc->daios)
1309 atc->srcs = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1310 if (NULL == atc->srcs)
1313 atc->srcimps = kzalloc(sizeof(void *)*(2*2), GFP_KERNEL);
1314 if (NULL == atc->srcimps)
1317 atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
1318 if (NULL == atc->pcm)
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++) {
1325 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1326 (struct daio **)&atc->daios[i]);
1328 printk(KERN_ERR "ctxfi: Failed to get DAIO "
1329 "resource %d!!!\n", i);
1334 subsys_id = atc->pci->subsystem_device;
1335 if ((subsys_id == 0x0029) || (subsys_id == 0x0031)) {
1337 da_desc.type = SPDIFI1;
1339 da_desc.type = SPDIFIO;
1341 err = daio_mgr->get_daio(daio_mgr, &da_desc,
1342 (struct daio **)&atc->daios[i]);
1344 printk(KERN_ERR "ctxfi: Failed to get S/PDIF-in resource!!!\n");
1349 src_mgr = atc->rsc_mgrs[SRC];
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]);
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]);
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]);
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]);
1393 err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
1395 printk(KERN_ERR "ctxfi: Failed to create mixer obj!!!\n");
1403 atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
1404 struct src **srcs, struct srcimp **srcimps)
1406 struct rsc *rscs[2] = {NULL};
1407 struct src *src = NULL;
1408 struct srcimp *srcimp = NULL;
1411 rscs[0] = &dai->daio.rscl;
1412 rscs[1] = &dai->daio.rscr;
1413 for (i = 0; i < 2; i++) {
1415 srcimp = srcimps[i];
1416 srcimp->ops->map(srcimp, src, rscs[i]);
1417 src_mgr->src_disable(src_mgr, src);
1420 src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
1423 src->ops->set_pm(src, 1);
1424 for (i = 0; i < 2; 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);
1431 dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
1432 dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
1434 dai->ops->set_enb_src(dai, 1);
1435 dai->ops->set_enb_srt(dai, 1);
1436 dai->ops->commit_write(dai);
1438 src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
1441 static void atc_connect_resources(struct ct_atc *atc)
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};
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]);
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]);
1465 mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
1467 mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
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]);
1475 mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
1477 mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
1479 for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
1481 mixer->set_input_left(mixer, i, &sum->rsc);
1482 sum = atc->pcm[j+1];
1483 mixer->set_input_right(mixer, i, &sum->rsc);
1487 static void atc_set_ops(struct ct_atc *atc)
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;
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
1525 * Creates and initializes a hardware manager.
1527 * Creates kmallocated ct_atc structure. Initializes hardware.
1528 * Returns 0 if suceeds, or negative error code if fails.
1531 int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
1532 unsigned int rsr, unsigned int msr, struct ct_atc **ratc)
1534 struct ct_atc *atc = NULL;
1535 static struct snd_device_ops ops = {
1536 .dev_free = atc_dev_free,
1542 atc = kzalloc(sizeof(*atc), GFP_KERNEL);
1551 /* Set operations */
1554 spin_lock_init(&atc->atc_lock);
1556 /* Find card model */
1557 err = atc_identify_card(atc);
1559 printk(KERN_ERR "ctatc: Card not recognised\n");
1563 /* Set up device virtual memory management object */
1564 err = ct_vm_create(&atc->vm);
1568 /* Create all atc hw devices */
1569 err = atc_create_hw_devs(atc);
1574 err = atc_get_resources(atc);
1578 /* Build topology */
1579 atc_connect_resources(atc);
1581 atc->create_alsa_devs = ct_create_alsa_devs;
1583 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
1587 snd_card_set_dev(card, &pci->dev);
1593 ct_atc_destroy(atc);
1594 printk(KERN_ERR "ctxfi: Something wrong!!!\n");