/*
 *  Driver for Ensoniq ES1370/ES1371 AudioPCI soundcard
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
 *		     Thomas Sailer <sailer@ife.ee.ethz.ch>
 *
 *   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 "../include/driver.h"
#include "../include/audiopci.h"
#include "../include/initval.h"

#ifndef CHIP1371
#undef CHIP1370
#define CHIP1370
#endif

#ifdef CHIP1370
MODULE_DESCRIPTION("\
Driver: Ensoniq AudioPCI ES1370\n\
Card: Sound Blaster PCI64\n\
Card: Sound Blaster PCI128\n\
PCI: 0x1274=0x5000\n\
");
#endif
#ifdef CHIP1371
MODULE_DESCRIPTION("\
Driver: Ensoniq/Creative AudioPCI ES1371+\n\
Card: Sound Blaster PCI64\n\
Card: Sound Blaster PCI128\n\
Card: Sound Blaster Vibra PCI\n\
Card: Ectiva EV1938\n\
PCI: 0x1102=0x8938\n\
PCI: 0x1274=0x1371\n\
PCI: 0x1274=0x5880\n\
");
#endif

int snd_index[SND_CARDS] = SND_DEFAULT_IDX;	/* Index 0-MAX */
char *snd_id[SND_CARDS] = SND_DEFAULT_STR;	/* ID for this card */
int snd_dac1_frame_size[SND_CARDS] = {[0 ... (SND_CARDS - 1)] = 128};
int snd_dac2_frame_size[SND_CARDS] = {[0 ... (SND_CARDS - 1)] = 128};
int snd_adc_frame_size[SND_CARDS] = {[0 ... (SND_CARDS - 1)] = 128};
MODULE_PARM(snd_index, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_index, "Index value for Ensoniq AudioPCI soundcard.");
MODULE_PARM(snd_id, "1-" __MODULE_STRING(SND_CARDS) "s");
MODULE_PARM_DESC(snd_id, "ID string for Ensoniq AudioPCI soundcard.");
MODULE_PARM(snd_dac1_frame_size, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_dac1_frame_size, "DAC1 frame size in kB for Ensoniq AudioPCI soundcard.");
MODULE_PARM(snd_dac2_frame_size, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_dac2_frame_size, "DAC2 frame size in kB for Ensoniq AudioPCI 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 Ensoniq AudioPCI soundcard.");

#ifdef CHIP1370
#define snd_ensoniq_interrupt snd_ensoniq_1370_interrupt
#define snd_ensoniq_create snd_ensoniq_1370_create
#define snd_ensoniq_free snd_ensoniq_1370_free
#define snd_ensoniq_pcm snd_ensoniq_1370_pcm
#define snd_ensoniq_pcm2 snd_ensoniq_1370_pcm2
#define snd_ensoniq_mixer snd_ensoniq_1370_mixer
#define snd_ensoniq_midi snd_ensoniq_1370_midi
#endif
#ifdef CHIP1371
#define snd_ensoniq_interrupt snd_ensoniq_1371_interrupt
#define snd_ensoniq_create snd_ensoniq_1371_create
#define snd_ensoniq_free snd_ensoniq_1371_free
#define snd_ensoniq_pcm snd_ensoniq_1371_pcm
#define snd_ensoniq_pcm2 snd_ensoniq_1371_pcm2
#define snd_ensoniq_mixer snd_ensoniq_1371_mixer
#define snd_ensoniq_midi snd_ensoniq_1371_midi
#endif

struct snd_audiopci {
	struct pci_dev *pci;
	snd_irq_t *irqptr;
	snd_dma_t *dma1ptr;	/* DAC1 frame */
	snd_dma_t *dma2ptr;	/* DAC2 frame */
	snd_dma_t *dma3ptr;	/* ADC frame */
	snd_card_t *card;
	ensoniq_t *ensoniq;
	snd_pcm_t *pcm;		/* DAC1/ADC */
	snd_pcm_t *pcm2;	/* DAC2 */
	snd_kmixer_t *mixer;
	snd_rawmidi_t *midi_uart;
};

static struct snd_audiopci *snd_audiopci_cards[SND_CARDS] = SND_DEFAULT_PTR;

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

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

static int __init snd_audiopci_detect(snd_card_t * card, struct snd_audiopci *scard,
				      unsigned short vendor, unsigned short device)
{
	if ((scard->pci = pci_find_device(vendor, device, scard->pci)) == NULL)
		return -ENODEV;
	if (snd_register_ioport(card, pci_resource_start(scard->pci, 0), 0x40, "Ensoniq AudioPCI", NULL) < 0)
		goto __nodev;
	return 0;
      __nodev:
	snd_unregister_ioports(card);
	return -ENODEV;
}

static void snd_audiopci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct snd_audiopci *scard = (struct snd_audiopci *) dev_id;

	if (scard == NULL || scard->ensoniq == NULL)
		return;
	snd_ensoniq_interrupt(scard->ensoniq);
}

static int __init snd_audiopci_resources(snd_card_t * card,
					 struct snd_audiopci *scard, int dev)
{
	int err;

	if ((err = snd_register_interrupt(card,
			"Ensoniq AudioPCI", scard->pci->irq,
			SND_IRQ_TYPE_PCI, snd_audiopci_interrupt,
			scard, NULL, &scard->irqptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card,
			"Ensoniq AudioPCI - DAC1 frame", 0,
			SND_DMA_TYPE_PCI, snd_dac1_frame_size[dev],
			NULL, &scard->dma1ptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card,
			"Ensoniq AudioPCI - DAC2 frame", 1,
			SND_DMA_TYPE_PCI, snd_dac2_frame_size[dev],
			NULL, &scard->dma2ptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card,
			"Ensoniq AudioPCI - ADC frame", 2,
			SND_DMA_TYPE_PCI, snd_adc_frame_size[dev],
			NULL, &scard->dma3ptr)) < 0)
		return err;
	return 0;
}

static int __init snd_audiopci_probe(int dev, struct snd_audiopci *scard)
{
	snd_card_t *card;
	snd_pcm_t *pcm = NULL, *pcm2 = NULL;
	snd_kmixer_t *mixer = NULL;
	snd_rawmidi_t *midi_uart = NULL;
	int pcm_devs[2];

	card = snd_card_new(snd_index[dev], snd_id[dev],
			    snd_audiopci_use_inc, snd_audiopci_use_dec);
	if (card == NULL)
		return -ENOMEM;
#ifdef CHIP1370
	card->type = SND_CARD_TYPE_ENS1370;
#else
	card->type = SND_CARD_TYPE_ENS1371;
#endif
	do {
#ifdef CHIP1370
		if (!snd_audiopci_detect(card, scard, PCI_VENDOR_ID_ENSONIQ, PCI_DEVICE_ID_ENSONIQ_ES1370))
#else
		if (!snd_audiopci_detect(card, scard, PCI_VENDOR_ID_ENSONIQ, PCI_DEVICE_ID_ENSONIQ_ES1371))
#endif
			break;
	} while (scard->pci);
#ifdef CHIP1371
	if (scard->pci == NULL) {
		do {
			if (!snd_audiopci_detect(card, scard, PCI_VENDOR_ID_ENSONIQ, PCI_DEVICE_ID_ENSONIQ_CT5880))
				break;
		} while (scard->pci);
	}
	if (scard->pci == NULL) {
		do {
			if (!snd_audiopci_detect(card, scard, PCI_VENDOR_ID_ECTIVA, PCI_DEVICE_ID_ECTIVA_EV1938))
				break;
		} while (scard->pci);
	}
#endif
	if (scard->pci == NULL) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (pci_enable_device(scard->pci)) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (snd_audiopci_resources(card, scard, dev) < 0) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (snd_ensoniq_create(card, scard->pci,
			       scard->dma1ptr, scard->dma2ptr,
			       scard->dma3ptr, scard->irqptr,
			       &scard->ensoniq) < 0)
		goto __nodev;
	pcm_devs[0] = 0; pcm_devs[1] = 1;
	if (snd_ensoniq_mixer(scard->ensoniq, 0, 2, pcm_devs, &mixer) < 0)
		goto __nodev;
	if (snd_ensoniq_pcm(scard->ensoniq, 0, &pcm) < 0)
		goto __nodev;
	if (snd_ensoniq_pcm2(scard->ensoniq, 1, &pcm2) < 0)
		goto __nodev;
	if (snd_ensoniq_midi(scard->ensoniq, 0, &midi_uart) < 0)
		goto __nodev;
	strcpy(card->abbreviation, "AudioPCI");
	strcpy(card->shortname, "Ensoniq AudioPCI");
	sprintf(card->longname, "%s %s at 0x%lx, irq %li",
		card->shortname,
		card->type == SND_CARD_TYPE_ENS1370 ? "ES1370" : "ES1371",
		pci_resource_start(scard->pci, 0),
		scard->irqptr->irq);

	if (!snd_card_register(card)) {
		scard->card = card;
		scard->mixer = mixer;
		scard->pcm = pcm;
		scard->pcm2 = pcm2;
		scard->midi_uart = scard->ensoniq->rmidi = midi_uart;
		return 0;
	}
	goto __nodev;

      __nodev:
	snd_card_free(card);
	return -ENXIO;
}

#ifdef MODULE

static int __exit snd_audiopci_free(int dev)
{
	struct snd_audiopci *scard;

	scard = snd_audiopci_cards[dev];
	snd_audiopci_cards[dev] = NULL;
	if (scard) {
		snd_card_unregister(scard->card);
		snd_kfree(scard);
	}
	return 0;
}

#endif

static int __init alsa_card_ens137x_init(void)
{
	int dev, cards;
	struct snd_audiopci *scard;

	for (dev = cards = 0; dev < SND_CARDS; dev++) {
		scard = (struct snd_audiopci *)
				snd_kcalloc(sizeof(struct snd_audiopci), GFP_KERNEL);
		if (scard == NULL)
			continue;
		if (snd_audiopci_probe(dev, scard) < 0) {
			snd_kfree(scard);
			break;
		}
		snd_audiopci_cards[dev] = scard;
		cards++;
	}
	if (!cards) {
#ifdef MODULE
		snd_printk("Ensoniq AudioPCI soundcard #%i not found or device busy\n", dev + 1);
#endif
		return -ENODEV;
	}
	return 0;
}

static void __exit alsa_card_ens137x_exit(void)
{
	int dev;

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

module_init(alsa_card_ens137x_init)
module_exit(alsa_card_ens137x_exit)
