/*
    card-es968.c - driver for ESS AudioDrive ES968 based soundcards.
    Copyright (C) 1999 by Massimo Piccioni <piccio@caronte.csr.unibo.it>

    Thanks to Pierfrancesco 'qM2' Passerini.

    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/initval.h"
#include "../include/sb.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_port[SND_CARDS] = SND_DEFAULT_PORT;		/* PnP setup */
int snd_irq[SND_CARDS] = SND_DEFAULT_IRQ;		/* Pnp setup */
int snd_dma8[SND_CARDS] = SND_DEFAULT_DMA;		/* PnP setup */
int snd_dma8_size[SND_CARDS] = SND_DEFAULT_DMA_SIZE;	/* 8,16,32,64,128 */

#ifdef MODULE_PARM
MODULE_PARM(snd_index, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_index, "Index value for es968 based soundcard.");
MODULE_PARM(snd_id, "1-" __MODULE_STRING(SND_CARDS) "s");
MODULE_PARM_DESC(snd_id, "ID string for es968 based soundcard.");
MODULE_PARM(snd_port, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_port, "Port # for es968 driver.");
MODULE_PARM(snd_irq, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_irq, "IRQ # for es968 driver.");
MODULE_PARM(snd_dma8, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_dma8, "8-bit DMA # for es968 driver.");
MODULE_PARM(snd_dma8_size, "1-" __MODULE_STRING(SND_CARDS) "i");
MODULE_PARM_DESC(snd_dma8_size, "8-bit DMA size for es968 driver.");
#endif	/* MODULE_PARM */

static struct snd_card_es968 {
	snd_irq_t *irqptr;
	snd_dma_t *dma8ptr;
	snd_card_t *card;
	snd_pcm_t *pcm;
	snd_kmixer_t *mixer;
	snd_rawmidi_t *rmidi;
#ifdef CONFIG_ISAPNP
	struct isapnp_dev *dev;
#endif	/* CONFIG_ISAPNP */
} *snd_card_es968_cards[SND_CARDS] = SND_DEFAULT_PTR;

#ifdef CONFIG_ISAPNP
static unsigned int snd_card_es968_pnpids[] = {
	(ISAPNP_VENDOR('E','S','S')<<16)|ISAPNP_DEVICE(0x0968),    /* device */
	(ISAPNP_VENDOR('E','S','S')<<16)|ISAPNP_FUNCTION(0x0968),  /* audio */
	0	/* end */
};
#endif	/* CONFIG_ISAPNP */

#define	DRIVER_NAME	"snd-card-es968"

#define snd_es968_printk(args...)	snd_printk(__FILE__": " ##args)


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

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

static void snd_card_es968_interrupt(int irq, void *dev_id,
							struct pt_regs *regs)
{
	struct snd_card_es968 *acard;
	snd_pcm_t *pcm;
	sbdsp_t *codec;

	if (!(acard = (struct snd_card_es968 *) dev_id))
		return;

	if (!(pcm = acard->pcm))
		return;

	if (!(codec = (sbdsp_t *) pcm->private_data))
		return;

	if (codec->open8 & SB_OPEN_PCM)
		snd_sb8dsp_interrupt(pcm);
	else if (acard->rmidi)
		snd_sb8dsp_midi_interrupt(acard->rmidi);
}

static int snd_card_es968_resources(int dev, struct snd_card_es968 *acard,
							snd_card_t *card)
{
	int error;
	static int no_alternatives[] = {-1};

	if ((error = snd_register_ioport(card, snd_port[dev], 16,
			DRIVER_NAME" - DSP", NULL)) < 0)
		return error;
	if ((error = snd_register_interrupt(card,
			DRIVER_NAME" - DSP", snd_irq[dev],
			SND_IRQ_TYPE_ISA, snd_card_es968_interrupt,
			acard, no_alternatives, &acard->irqptr)) < 0)
		return error;
	if ((error = snd_register_dma_channel(card,
			DRIVER_NAME" - DSP", snd_dma8[dev],
			SND_DMA_TYPE_ISA, snd_dma8_size[dev],
			no_alternatives, &acard->dma8ptr)) < 0)
		return error;

	return 0;
}

#ifdef CONFIG_ISAPNP
static int snd_card_es968_isapnp(int dev, struct snd_card_es968 *acard)
{
	static int idx = 0;
	static struct isapnp_card *card = NULL;
        unsigned int tmp;
	struct isapnp_dev *pdev;

      __again:
      	acard->dev = NULL;
	while ((tmp = snd_card_es968_pnpids[idx]) != 0) {
		if ((card = isapnp_find_card(tmp >> 16, tmp & 0xffff, card)))
			break;
		idx += 2;
	}

	if (!card)
		return -ENODEV;

	tmp = snd_card_es968_pnpids[idx+1];
	acard->dev = pdev = isapnp_find_dev(card, tmp >> 16, tmp & 0xffff, NULL);
	if (!pdev || pdev->active)
		goto __again;

	if (pdev->prepare(pdev)<0)
		return -EAGAIN;

	if (snd_port[dev] != SND_AUTO_PORT)
		isapnp_resource_change(&pdev->resource[0], snd_port[dev], 16);
	if (snd_dma8[dev] != SND_AUTO_DMA)
		isapnp_resource_change(&pdev->dma_resource[0], snd_dma8[dev], 1);
	if (snd_irq[dev] != SND_AUTO_IRQ)
		isapnp_resource_change(&pdev->irq_resource[0], snd_irq[dev], 1);

	if (pdev->activate(pdev)<0) {
		snd_es968_printk("AUDIO isapnp configure failure\n");
		return -EBUSY;
	}

	snd_port[dev] = pdev->resource[0].start;
	snd_dma8[dev] = pdev->dma_resource[0].start;
	snd_irq[dev] = pdev->irq_resource[0].start;

	return 0;
}

static void snd_card_es968_deactivate(struct snd_card_es968 *acard)
{
	if (acard->dev)
		acard->dev->deactivate(acard->dev);
}
#endif	/* CONFIG_ISAPNP */

static int snd_card_es968_probe(int dev, struct snd_card_es968 *acard)
{
	int error;
	sbdsp_t *codec;
	snd_card_t *card;
	snd_pcm_t *pcm;
	snd_kmixer_t *mixer;
	snd_rawmidi_t *rmidi;

	if (!(card = snd_card_new(snd_index[dev], snd_id[dev],
			snd_card_es968_use_inc, snd_card_es968_use_dec)))
		return -ENOMEM;
	card->type = SND_CARD_TYPE_SB_10;

#ifdef CONFIG_ISAPNP
	if ((error = snd_card_es968_isapnp(dev, acard)))
		goto __on_error;
#else
	snd_es968_printk("you have to enable PnP support ...\n");
	return -ENOSYS;
#endif	/* CONFIG_ISAPNP */

	if ((error = snd_card_es968_resources(dev, acard, card)))
		goto __on_error;

	if (!(pcm = snd_sb8dsp_new_device(card, snd_port[dev],
			acard->irqptr, acard->dma8ptr, SB_HW_AUTO)))
		goto __on_error;
	codec = (sbdsp_t *) pcm->private_data;

	if (!(mixer = snd_sb8dsp_new_mixer(card,
			&codec->mixer, codec->hardware, 0))) {
		snd_pcm_free(pcm);
		goto __on_error;
	}
	if ((error = snd_mixer_register(mixer, 0))) {
		snd_mixer_free(mixer);
		snd_pcm_free(pcm);
		goto __on_error;
	}

	if (!(rmidi = snd_sb8dsp_midi_new_device(card, pcm))) {
		snd_mixer_unregister(mixer);
		snd_pcm_free(pcm);
		goto __on_error;
	}
	if ((error = snd_rawmidi_register(rmidi, 0))) {
		snd_rawmidi_free(rmidi);
		snd_mixer_unregister(mixer);
		snd_pcm_free(pcm);
		goto __on_error;
	}
	if ((error = snd_pcm_register(pcm, 0))) {
		snd_rawmidi_unregister(rmidi);
		snd_mixer_unregister(mixer);
		snd_pcm_free(pcm);
		goto __on_error;
	}

	strcpy(card->abbreviation, "ES968");
	strcpy(card->shortname, "ESS ES968");
	sprintf(card->longname, "%s soundcard, %s at 0x%x, irq %i, dma %i",
		card->shortname, codec->name, codec->port,
		acard->irqptr->irq, acard->dma8ptr->dma);

	if (!(error = snd_card_register(card))) {
		acard->card = card;
		acard->pcm = pcm;
		acard->mixer = mixer;
		acard->rmidi = rmidi;
		return 0;
	}

	snd_rawmidi_unregister(rmidi);
	snd_mixer_unregister(mixer);
	snd_pcm_free(pcm);

      __on_error:
	snd_card_free(card);
	return error ? error : -ENXIO;
}

static void snd_card_es968_free(int dev)
{
	struct snd_card_es968 *acard;

	acard = snd_card_es968_cards[dev];
	if (acard) {
		snd_card_es968_cards[dev] = NULL;
		snd_card_unregister(acard->card);
		if (acard->rmidi)
			snd_rawmidi_unregister(acard->rmidi);
		if (acard->mixer)
			snd_mixer_unregister(acard->mixer);
		if (acard->pcm)
			snd_pcm_unregister(acard->pcm);
		snd_card_free(acard->card);
#ifdef CONFIG_ISAPNP
		snd_card_es968_deactivate(acard);
#endif	/* CONFIG_ISAPNP */
		snd_kfree(acard);
	}
}

int init_module(void)
{
	int cards, dev;
	struct snd_card_es968 *acard;

	for (dev = cards = 0; dev < SND_CARDS; dev++) {
		acard = (struct snd_card_es968 *) snd_kcalloc(
			sizeof(struct snd_card_es968), GFP_KERNEL);
		if (!acard)
			break;

		if (snd_card_es968_probe(dev, acard)) {
#ifdef CONFIG_ISAPNP
			snd_card_es968_deactivate(acard);
#endif	/* CONFIG_ISAPNP */
			snd_kfree(acard);
			continue;
		}
		snd_card_es968_cards[cards++] = acard;
	}

	if (!cards)
		snd_es968_printk("no ES968 based soundcards found\n");
	return cards ? 0 : -ENODEV;
}

void cleanup_module(void)
{
	int dev;

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

