/*
 *  The driver for the ForteMedia FM801 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 "../include/driver.h"
#include "../include/fm801.h"
#include "../include/mpu401.h"
#include "../include/opl3.h"
#include "../include/initval.h"

MODULE_DESCRIPTION("\
Driver: ForteMedia FM801\n\
PCI: 0x1319=0x0801\n\
");

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_dac_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 the FM801 soundcard.");
MODULE_PARM(snd_id, "1-" __MODULE_STRING(SND_CARDS) "s");
MODULE_PARM_DESC(snd_id, "ID string for the FM801 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 FM801 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 FM801 soundcard.");

struct snd_fm801 {
	struct pci_dev *pci;
	snd_irq_t *irqptr;
	snd_dma_t *dma1ptr;	/* DAC frame */
	snd_dma_t *dma2ptr;	/* ADC frame */
	snd_card_t *card;
	fm801_t *codec;
	snd_pcm_t *pcm;
	snd_kmixer_t *mixer;
	snd_rawmidi_t *midi_uart;
	snd_hwdep_t *synth;
};

static struct snd_fm801 *snd_fm801_cards[SND_CARDS] = SND_DEFAULT_PTR;

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

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

static int __init snd_card_fm801_detect(snd_card_t * card, struct snd_fm801 *scard)
{
	if ((scard->pci = pci_find_device(PCI_VENDOR_ID_FORTEMEDIA,
					  PCI_DEVICE_ID_FORTEMEDIA_FM801,
					  scard->pci)) == NULL)
		return -ENODEV;
	if (snd_register_ioport(card, pci_resource_start(scard->pci, 0), 0x80, "FM801", NULL) < 0)
		return -ENODEV;
	return 0;
}

static void snd_card_fm801_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct snd_fm801 *scard = (struct snd_fm801 *) dev_id;
	fm801_t *codec;
	unsigned int status;

	if (scard == NULL || (codec = scard->codec) == NULL)
		return;
	status = inw(FM801_REG(codec, IRQ_STATUS));
	if (status & (FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME)) {
		if (status & (FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE))
			snd_fm801_interrupt(codec, status);
		if (status & FM801_IRQ_MPU) {
			if (scard->midi_uart)
				snd_mpu401_uart_interrupt(scard->midi_uart);
			outw(FM801_IRQ_MPU, FM801_REG(codec, IRQ_STATUS));
		}
		if (status & FM801_IRQ_VOLUME) {
			/* TODO */
			outw(FM801_IRQ_VOLUME, FM801_REG(codec, IRQ_STATUS));
		}
	}
}

static int __init snd_card_fm801_resources(snd_card_t * card,
					   struct snd_fm801 *scard,
					   int dev)
{
	int err;

	if ((err = snd_register_interrupt(card,
			"FM801", scard->pci->irq,
			SND_IRQ_TYPE_PCI, snd_card_fm801_interrupt,
			scard, NULL, &scard->irqptr)) < 0)
		return err;
	if ((err = snd_register_dma_channel(card,
			"FM801 - 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,
			"FM801 - ADC frame", 2,
			SND_DMA_TYPE_PCI, snd_adc_frame_size[dev],
			NULL, &scard->dma2ptr)) < 0)
		return err;
	return 0;
}

static int __init snd_card_fm801_probe(int dev, struct snd_fm801 *scard)
{
	snd_card_t *card;
	snd_pcm_t *pcm = NULL;
	snd_kmixer_t *mixer = NULL;
	snd_rawmidi_t *midi_uart = NULL;
	snd_hwdep_t *synth = NULL;

	card = snd_card_new(snd_index[dev], snd_id[dev],
			    snd_card_fm801_use_inc, snd_card_fm801_use_dec);
	if (card == NULL)
		return -ENOMEM;
	card->type = SND_CARD_TYPE_CS461X;
	scard->pci = NULL;
	do {
		if (!snd_card_fm801_detect(card, scard))
			break;
	} while (scard->pci);
	if (scard->pci == NULL) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (pci_enable_device(scard->pci)) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (snd_card_fm801_resources(card, scard, dev) < 0) {
		snd_card_free(card);
		return -ENODEV;
	}
	if (snd_fm801_create(card, scard->pci,
			     scard->dma1ptr,
			     scard->dma2ptr,
			     scard->irqptr,
			     &scard->codec) < 0)
		goto __nodev;
	if (snd_fm801_pcm(scard->codec, 0, &pcm) < 0)
		goto __nodev;
	if (snd_fm801_mixer(scard->codec, 0, pcm, &mixer) < 0)
		goto __nodev;
	if (snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
				FM801_REG(scard->codec, MPU401_DATA),
				scard->irqptr->irq, &midi_uart) < 0)
		goto __nodev;
	if (snd_opl3_new(card, 0, FM801_REG(scard->codec, OPL3_BANK0),
			 FM801_REG(scard->codec, OPL3_BANK1),
			 OPL3_HW_OPL3_FM801, -1, &synth) < 0)
		goto __nodev;
 
	strcpy(card->abbreviation, "FM801");
	strcpy(card->shortname, "ForteMedia FM801");
	sprintf(card->longname, "%s at 0x%lx, irq %li",
		card->shortname,
		pci_resource_start(scard->pci, 0),
		scard->irqptr->irq);

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

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

#ifdef MODULE

static int __exit snd_card_fm801_free(int dev)
{
	struct snd_fm801 *scard;

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

#endif

static int __init alsa_card_fm801_init(void)
{
	int dev, cards;
	struct snd_fm801 *scard;

	for (dev = cards = 0; dev < SND_CARDS; dev++) {
		scard = (struct snd_fm801 *)
				snd_kcalloc(sizeof(struct snd_fm801), GFP_KERNEL);
		if (scard == NULL)
			continue;
		if (snd_card_fm801_probe(dev, scard) < 0) {
			snd_kfree(scard);
			break;
		}
		snd_fm801_cards[dev] = scard;
		cards++;
	}
	if (!cards) {
		snd_printk("ForteMedia FM801 soundcard #%i not found or device busy\n", dev + 1);
		return -ENODEV;
	}
	return 0;
}

static void __exit alsa_card_fm801_exit(void)
{
	int dev;

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

module_init(alsa_card_fm801_init)
module_exit(alsa_card_fm801_exit)
