/*
 *  Driver for ESS Solo-1 (ES1938, ES1946) soundcard
 *  Copyright (c) by Jaromir Koutek <miri@punknet.cz>,
 *                   Jaroslav Kysela <perex@suse.cz>
 * 
 * Rewritted from sonicvibes.c source.
 *
 *
 *   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 "esssolo1.h"
#include "opl3.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 */
#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 */
#endif

struct snd_solo {
	struct snd_pci_dev pci;
	snd_irq_t *irqptr;
	snd_dma_t *dma1ptr;
	snd_dma_t *dma2ptr;
	snd_card_t *card;
	esssolo_t *solo;
	snd_pcm_t *pcm;
	snd_pcm_t *pcm2;
	snd_kmixer_t *mixer;
	snd_rawmidi_t *midi_uart;
	snd_synth_t *synth;
};

#ifdef __QNXNTO__
static struct snd_solo *snd_solo_cards[SND_CARDS] = SND_DEFAULT_PTR;
#else
static struct snd_solo *snd_solo_cards[SND_CARDS];
#endif

static void snd_solo_interrupt2(void *dev_id)
{
	struct snd_solo *scard = (struct snd_solo *) dev_id;

	if (!scard || !scard->solo)
		return;
	snd_solo_interrupt(scard->solo);
}

#define SOLO_DMA_SIZE 32

static int snd_solo_resources(snd_card_t * card, struct snd_solo *scard, int dev)
{
	int err;

/*  if (( err = snd_register_ioport( card, 
			scard -> pci.base_address[ 1 ] & ~PCI_BASE_ADDRESS_SPACE, 
			0x10, "ESS Solo-1 I/O" )) < 0 ) 
		return err;
*/
	if (( err = snd_register_ioport(card, 
			PCI_IO_ADDR(scard->pci.base_address[1]), 
			0x10, "ESS Solo-1 SB", NULL)) < 0)
		return err;
	if (( err = snd_register_ioport(card, 
			PCI_IO_ADDR(scard->pci.base_address[2]), 
			0x10, "ESS Solo-1 VC (DMA)", NULL)) < 0)
		return err;
	if (( err = snd_register_ioport(card, 
			PCI_IO_ADDR(scard->pci.base_address[3]), 
			4, "ESS Solo-1 MIDI", NULL)) < 0)
		return err;
	if (( err = snd_register_ioport(card, 
			PCI_IO_ADDR(scard->pci.base_address[4]), 
			4, "ESS Solo-1 Game port", NULL)) < 0)
		return err;
	if ((err = snd_register_interrupt(card, "ESS Solo-1", scard->pci.irq,
			    SND_IRQ_TYPE_PCI, snd_solo_interrupt2, scard,
					  NULL, &scard->irqptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card, "ESS Solo-1 DMA 1", 0,
					    SND_DMA_TYPE_PCI_16MB,
			      SOLO_DMA_SIZE, NULL, &scard->dma1ptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card, "ESS Solo-1 DMA 2", 1,
					    SND_DMA_TYPE_PCI_16MB,
			      SOLO_DMA_SIZE, NULL, &scard->dma2ptr)) < 0)
		return err;
	return 0;
}

static int snd_solo_probe(int dev, struct snd_solo *scard)
{
	snd_card_t *card;
	snd_pcm_t *pcm = NULL;
	snd_pcm_t *pcm2 = NULL;
	snd_kmixer_t *mixer = NULL;
	snd_rawmidi_t *midi_uart = NULL;
	snd_synth_t *synth = NULL;
	int pci_index;

	card = snd_card_new(snd_index[dev], snd_id[dev]);
	if (!card)
		return -ENOMEM;
	card->type = SND_CARD_TYPE_ESS_ES1938;
	card->static_data = scard;

	// MH 000518 don't always test for the same device
	for (pci_index = dev; pci_index < SND_CARDS; pci_index++) {
		if (!snd_solo_detect(card, &scard->pci, pci_index))
			break;
	}
	if (pci_index >= SND_CARDS) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (snd_solo_resources(card, scard, dev) < 0) {
		snd_card_free(card);
		return -ENODEV;
	}
	scard->solo = snd_solo_create(card, &scard->pci, scard->dma1ptr,
				    scard->dma2ptr, scard->irqptr, 0, 0);
	if (!scard->solo)
		goto __nodev;
	mixer = snd_solo_mixer(scard->solo);
	if (!mixer)
		goto __nodev;
	pcm = snd_solo_pcm(scard->solo);
	if (!pcm)
		goto __nodev;
#undef USE_PCM2
#ifdef USE_PCM2
	pcm2 = snd_solo_pcm2(scard->solo);
	if (!pcm2)
		goto __nodev;
#endif
	if (snd_mixer_register(mixer, 0) < 0)
		goto __nodev;
	if (snd_pcm_register(pcm, 0) < 0) {
		snd_mixer_unregister(mixer);
		mixer = NULL;	/* !!! FIXME !!! */
		goto __nodev;
	}
#ifdef USE_PCM2
	if (snd_pcm_register(pcm2, 1) < 0) {
		snd_pcm_unregister(pcm);
		pcm = NULL;	/* !!! FIXME !!! */
		snd_mixer_unregister(mixer);
		mixer = NULL;	/* !!! FIXME !!! */
		goto __nodev;
	}
#endif

	strcpy(card->abbreviation, "Solo-1");
	strcpy(card->shortname, "ESS Solo-1");
	sprintf(card->longname, "%s rev %i, irq %i",
		card->shortname,
		scard->solo->revision,
		scard->irqptr->irq);

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

      __nodev:
	if (synth)
		snd_synth_free(synth);
	if (midi_uart)
		snd_rawmidi_free(midi_uart);
	if (mixer)
		snd_mixer_free(mixer);
	if (pcm)
		snd_pcm_free(pcm);
	if (pcm2)
		snd_pcm_free(pcm2);
	if (scard->solo) {
		snd_solo_free(scard->solo);
		scard->solo = NULL;
	}
	snd_card_free(card);
	return -ENXIO;
}

static int snd_solo_free2(int dev)
{
	struct snd_solo *scard;
	snd_pcm_t *pcm, *pcm2;

	scard = snd_solo_cards[dev];
	snd_solo_cards[dev] = NULL;
	if (scard) {
		snd_card_unregister(scard->card);
		if (scard->synth)
			snd_synth_unregister(scard->synth);
		if (scard->midi_uart) {
			scard->solo->rmidi = NULL;
			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->pcm2) {
			pcm2 = scard->pcm2;
			scard->pcm2 = NULL;	/* turn off interrupts */
			snd_pcm_unregister(pcm2);
		}
		if (scard->solo) {
			snd_solo_free(scard->solo);
			scard->solo = NULL;
		}
		snd_card_free(scard->card);
		snd_free(scard, sizeof(struct snd_solo));
	}
	return 0;
}

int snd_solo_init(void)
{
	int dev, cards;
	struct snd_solo *scard;

	for (dev = cards = 0; dev < SND_CARDS; dev++) {
		scard = (struct snd_solo *) snd_malloc(sizeof(struct snd_solo));
		if (!scard)
			continue;
		memset(scard, 0, sizeof(struct snd_solo));
		if (snd_solo_probe(dev, scard) < 0) {
			snd_free(scard, sizeof(struct snd_solo));
			break;
		}
		snd_solo_cards[dev] = scard;
		cards++;
	}
	if (!cards) {
		snd_printk("ESS Solo-1 soundcard #%i not found or device busy\n", dev + 1);
		return -ENODEV;
	}
	return 0;
}

void ess_solo_cleanup(void)
{
	int dev;

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