/*
 *  Driver for sblive soundcard
 */

#include "driver.h"
#include "sndpci.h"
#include "emu10k1.h"
#include "initval.h"

int
snd_sblive_detect (snd_card_t * card, struct snd_pci_dev *pci, int pci_index, int device)
{
	if (device == SND_CARD_TYPE_SBLIVE && !snd_pci_find_device (PCI_VENDOR_ID_CREATIVE,
			PCI_DEVICE_ID_CREATIVE_EMU10K1,
			pci_index, pci))
	{
		if (card)
			card->type= SND_CARD_TYPE_SBLIVE;
	}
	else
	{
		return -ENODEV;
	}

	return 0;
}

/* snd_sblive_cmdline() -- Jan 6th, 2000
 * Used by audiotrap for detecting commandline options
 * All drivers use exactly the same function prototype.
 * Don't add any more options to this function or change
 * its behaviour without changing the others too.
 */
int
snd_sblive_cmdline (int *count, char **cmdline, int verbose)
{
	int     dev, cards= 0;
	struct snd_pci_dev *pci= NULL;

	*count= 0;

	for (dev= 0; dev < SND_CARDS; dev++)
	{
		if (cards > SND_CARDS)
			break;
		if (!pci)
		{
			pci= (struct snd_pci_dev *) snd_malloc (sizeof (struct snd_pci_dev));

			if (!pci)
				continue;
		}
		memset (pci, 0, sizeof (struct snd_pci_dev));

		if (snd_sblive_detect (NULL, pci, dev, SND_CARD_TYPE_SBLIVE) == 0)
		{
			if (verbose > 2)
				snd_printk ("sblive: Found SB-LIVE! @ 0x%lX\n",
					PCI_IO_ADDR (pci->base_address[0]));
#ifdef __QNXNTO__
			if (pci_detach_device (pci->handle) != PCI_SUCCESS)
				snd_printk ("sblive: Unable to detach PCI device.\n");
#endif
			cards++;
		}
		else
			break;
	}
	snd_free (pci, sizeof (struct snd_pci_dev));

	if (!cards)
	{
		if (verbose > 1)
			snd_printk ("sblive: SoundBlaster LIVE! interface not found or device busy\n");
		return -ENODEV;
	}
	*count= cards;

	/* Since there are no options, no matter how many cards we find we only have to return one
	 * simple commandline
	 */
	if (cards)
	{
		int     idx= 0;

		cmdline[idx]= (char *) malloc (PATH_MAX);
		if (!cmdline[idx])
			return -ENOMEM;
		sprintf (cmdline[idx], "deva-sblive");
		*count= idx + 1;
	}
	return 0;
}
