/*
 *  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.
 *
 */

#define SND_MAIN_OBJECT_FILE
#include "driver.h"
#include "cs461x.h"
#include "initval.h"

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};
#ifdef MODULE_PARM
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;
};

static struct snd_cs461x *snd_cs461x_cards[SND_CARDS] = SND_DEFAULT_PTR;

static void snd_card_cs461x_use_inc(snd_card_t * card)
{
	MOD_INC_USE_COUNT;
}

static void snd_card_cs461x_use_dec(snd_card_t * card)
{
	MOD_DEC_USE_COUNT;
}

static int snd_card_cs461x_detect(snd_card_t * card, struct snd_cs461x *scard, int pci_index)
{
	int idx;

	if (snd_pci_find_device(PCI_VENDOR_ID_CIRRUS,
			         PCI_DEVICE_ID_CIRRUS_4610,
			         pci_index, &scard->pci) && 
	    snd_pci_find_device(PCI_VENDOR_ID_CIRRUS,
			         PCI_DEVICE_ID_CIRRUS_4612,
			         pci_index, &scard->pci) && 
	    snd_pci_find_device(PCI_VENDOR_ID_CIRRUS,
			         PCI_DEVICE_ID_CIRRUS_4615,
			         pci_index, &scard->pci))
		return -ENODEV;
	for (idx = 0; idx < SND_CARDS; idx++) {
		if (snd_cards[idx]) {
			if (snd_cs461x_cards[idx]->pci.base_address[0] == scard->pci.base_address[0])
				return -EBUSY;
		}
	}
	return 0;
}

static void snd_card_cs461x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	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)
{
	snd_card_t *card;
	snd_pcm_t *pcm = NULL;
	snd_kmixer_t *mixer = NULL;
	snd_rawmidi_t *midi_uart = NULL;
	int pci_index;

	card = snd_card_new(snd_index[dev], snd_id[dev],
			    snd_card_cs461x_use_inc, snd_card_cs461x_use_dec);
	if (!card)
		return -ENOMEM;
	card->static_data = scard;
	card->type = SND_CARD_TYPE_CS461X;
	for (pci_index = 0; pci_index < SND_CARDS; pci_index++) {
		if (!snd_card_cs461x_detect(card, scard, pci_index))
			break;
	}
	if (pci_index >= SND_CARDS) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (snd_card_cs461x_resources(card, scard, dev) < 0) {
		snd_card_free(card);
		return -ENODEV;
	}
	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
	strcpy(card->abbreviation, "CS461x");
	strcpy(card->shortname, "Sound Fusion CS461x");
	sprintf(card->longname, "%s at 0x%lx/0x%lx, irq %i",
		card->shortname,
		scard->pci.base_address[0] & ~PCI_BASE_ADDRESS_SPACE,
		scard->pci.base_address[1] & ~PCI_BASE_ADDRESS_SPACE,
		scard->irqptr->irq);

	if (!snd_card_register(card)) {
		scard->card = card;
		scard->mixer = mixer;
		scard->pcm = pcm;
		scard->midi_uart = midi_uart;
		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 init_module(void)
{
	int dev, cards;
	struct snd_cs461x *scard;

#ifndef LINUX_2_1
	register_symtab(NULL);
#endif
	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) < 0) {
			snd_free(scard, sizeof(struct snd_cs461x));
			break;
		}
		snd_cs461x_cards[dev] = scard;
		cards++;
	}
	if (!cards) {
		snd_printk("Sound Fusion CS461x soundcard #%i not found or device busy\n", dev + 1);
		return -ENODEV;
	}
	return 0;
}

void cleanup_module(void)
{
	int dev;

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