/*
 *  The driver for the Cirrus Logic's Sound Fusion CS461X based soundcards
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "driver.h"
#include "sndpci.h"
#include "cs461x.h"
#include "initval.h"

#ifdef __QNXNTO__
int snd_index[SND_CARDS] = SND_DEFAULT_IDX;	/* Index 1-MAX */
char *snd_id[SND_CARDS] = SND_DEFAULT_STR;	/* ID for this card */
int snd_dac_frame_size[SND_CARDS] = {[0 ... (SND_CARDS - 1)] = 128};
int snd_adc_frame_size[SND_CARDS] = {[0 ... (SND_CARDS - 1)] = 128};
#else
int snd_index[SND_CARDS] = {-1,-1,-1,-1,-1,-1,-1,-1};	/* Index 1-MAX */
char *snd_id[SND_CARDS];	/* ID for this card */
int snd_dac_frame_size[SND_CARDS] = {128,128,128,128,128,128,128,128};
int snd_adc_frame_size[SND_CARDS] = {128,128,128,128,128,128,128,128};
#endif

#if 0
MODULE_PARM(snd_index, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_index, "Index value for the CS461x soundcard.");
MODULE_PARM(snd_id, "1-" __MODULE_STRING(SND_CARDS) "s");
MODULE_PARM_DESC(snd_id, "ID string for the CS461x soundcard.");
MODULE_PARM(snd_dac_frame_size, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_dac_frame_size, "DAC frame size in kB for the CS461x soundcard.");
MODULE_PARM(snd_adc_frame_size, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_adc_frame_size, "ADC frame size in kB for the CS461x soundcard.");
#endif

struct snd_cs461x {
	struct snd_pci_dev pci;
	snd_irq_t *irqptr;
	snd_dma_t *dma1ptr;	/* DAC frame */
	snd_dma_t *dma2ptr;	/* ADC frame */
	snd_card_t *card;
	cs461x_t *codec;
	snd_pcm_t *pcm;
	snd_kmixer_t *mixer;
	snd_rawmidi_t *midi_uart;
};

#ifdef __QNXNTO__
static struct snd_cs461x *snd_cs461x_cards[SND_CARDS] = SND_DEFAULT_PTR;
#else
static struct snd_cs461x *snd_cs461x_cards[SND_CARDS];
#endif

static void snd_card_cs461x_interrupt(void *dev_id)
{
	struct snd_cs461x *scard = (struct snd_cs461x *) dev_id;

	if (!scard || !scard->codec)
		return;
	snd_cs461x_interrupt(scard->codec);
}

static int snd_card_cs461x_resources(snd_card_t * card,
                                  struct snd_cs461x *scard, int dev)
{
	int err;

	if ((err = snd_register_interrupt(card,
			"CS461x", scard->pci.irq,
			SND_IRQ_TYPE_PCI, snd_card_cs461x_interrupt,
			scard, NULL, &scard->irqptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card,
			"CS461x - DAC frame", 0,
			SND_DMA_TYPE_PCI, snd_dac_frame_size[dev],
			NULL, &scard->dma1ptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card,
			"CS461x - ADC frame", 2,
			SND_DMA_TYPE_PCI, snd_adc_frame_size[dev],
			NULL, &scard->dma2ptr)) < 0)
		return err;
	return 0;
}

static int snd_card_cs461x_probe(int dev, struct snd_cs461x *scard, int pci_id)
{
	snd_card_t *card;
	snd_pcm_t *pcm = NULL;
	snd_kmixer_t *mixer = NULL;
	snd_rawmidi_t *midi_uart = NULL;
	int err;
	
	card = snd_card_new(snd_index[dev], snd_id[dev]);
	
	if (!card) 
		return -ENOMEM;
	card->static_data = scard;
	card->type = SND_CARD_TYPE_CS461X;
	snd_printd("(1) scanning for %x\n", pci_id);
	if ((err = snd_card_cs461x_detect(card, &scard->pci, pci_id, dev)) < 0) {
		snd_card_free(card);
		return err;
	}
	snd_printd("(2) found -  allocing resources\n");
	if (snd_card_cs461x_resources(card, scard, dev) < 0) {
		snd_card_free(card);
		return -ENODEV;
	}
	snd_printd("(3) creating codec device\n");
	scard->codec = snd_cs461x_create(card, &scard->pci,
				         scard->dma1ptr,
				         scard->dma2ptr,
				         scard->irqptr);
	if (!scard->codec)
		goto __nodev;
	mixer = snd_cs461x_mixer(scard->codec, 0);
	if (!mixer)
		goto __nodev;
	pcm = snd_cs461x_pcm(scard->codec);
	if (!pcm)
		goto __nodev;
#if 0
	midi_uart = snd_cs461x_midi(scard->codec);
	if (!midi_uart)
		goto __nodev;
#else
	midi_uart = NULL;
#endif
	if (snd_mixer_register(mixer, 0) < 0)
		goto __nodev;
	if (snd_pcm_register(pcm, 0) < 0) {
		snd_mixer_unregister(mixer);
		mixer = NULL;
		goto __nodev;
	}
#if 0
	if (snd_rawmidi_register(midi_uart, 0) < 0) {
		snd_pcm_unregister(pcm);
		pcm = NULL;
		snd_mixer_unregister(mixer);
		mixer = NULL;
		goto __nodev;
	}
#endif
	snd_printd("(4) registering card\n");
	strcpy(card->abbreviation, "CS461x");
	strcpy(card->shortname, "Sound Fusion CS461x");
	sprintf(card->longname, "%s at 0x%lx/0x%lx, irq %i",
		card->shortname,
		PCI_IO_ADDR(scard->pci.base_address[0]),
		PCI_IO_ADDR(scard->pci.base_address[1]),
		scard->irqptr->irq);

	snd_printd("(5)\n");
	if (!snd_card_register(card)) {
		scard->card = card;
		scard->mixer = mixer;
		scard->pcm = pcm;
		scard->midi_uart = midi_uart;
		snd_enable_irq(scard->irqptr);
		return 0;
	}
	goto __nodev;

      __nodev:
	if (midi_uart)
		snd_rawmidi_free(midi_uart);
	if (mixer)
		snd_mixer_free(mixer);
	if (pcm)
		snd_pcm_free(pcm);
	if (scard->codec) {
		snd_cs461x_free(scard->codec);
		scard->codec = NULL;
	}
	snd_card_free(card);
	return -ENXIO;
}

static int snd_card_cs461x_free(int dev)
{
	struct snd_cs461x *scard;
	snd_pcm_t *pcm;

	scard = snd_cs461x_cards[dev];
	snd_cs461x_cards[dev] = NULL;
	if (scard) {
		snd_card_unregister(scard->card);
		if (scard->midi_uart)
			snd_rawmidi_unregister(scard->midi_uart);
		if (scard->mixer)
			snd_mixer_unregister(scard->mixer);
		if (scard->pcm) {
			pcm = scard->pcm;
			scard->pcm = NULL;	/* turn off interrupts */
			snd_pcm_unregister(pcm);
		}
		if (scard->codec) {
			snd_cs461x_free(scard->codec);
			scard->codec = NULL;
		}
		snd_card_free(scard->card);
		snd_free(scard, sizeof(struct snd_cs461x));
	}
	return 0;
}

int snd_cs461x_init(void)
{
	int dev=0, cards=0;
	struct snd_cs461x *scard;

	for (dev = cards = 0; dev < SND_CARDS; dev++) {
		scard = (struct snd_cs461x *)
				snd_malloc(sizeof(struct snd_cs461x));
		if (!scard)
			continue;
		memset(scard, 0, sizeof(struct snd_cs461x));
		if (snd_card_cs461x_probe(dev, scard, PCI_DEVICE_ID_CIRRUS_4612) < 0) {
			snd_free(scard, sizeof(struct snd_cs461x));
			break;
		}
		snd_cs461x_cards[cards] = scard;
		cards++;
		if ( cards > SND_CARDS)
			return 0;
	}
	for (dev = 0; dev < SND_CARDS; dev++) {
		scard = (struct snd_cs461x *)
				snd_malloc(sizeof(struct snd_cs461x));
		if (!scard)
			continue;
		memset(scard, 0, sizeof(struct snd_cs461x));
		if (snd_card_cs461x_probe(dev, scard, PCI_DEVICE_ID_CIRRUS_4610) < 0) {
			snd_free(scard, sizeof(struct snd_cs461x));
			break;
		}
		snd_cs461x_cards[dev] = scard;
		cards++;
		if ( cards > SND_CARDS)
			return 0;
	}
	for (dev = 0; dev < SND_CARDS; dev++) {
		scard = (struct snd_cs461x *)
				snd_malloc(sizeof(struct snd_cs461x));
		if (!scard)
			continue;
		memset(scard, 0, sizeof(struct snd_cs461x));
		if (snd_card_cs461x_probe(dev, scard, PCI_DEVICE_ID_CIRRUS_4615) < 0) {
			snd_free(scard, sizeof(struct snd_cs461x));
			break;
		}
		snd_cs461x_cards[dev] = scard;
		cards++;
		if ( cards > SND_CARDS)
			return 0;
	}

	if (!cards) {
		snd_printk("Sound Fusion CS461x soundcard #%i not found or device busy\n", dev + 1);
		return -ENODEV;
	}
	return 0;
}

void snd_cs461x_cleanup(void)
{
	int dev;

	for (dev = 0; dev < SND_CARDS; dev++)
		snd_card_cs461x_free(dev);
}
