/*
 *  Driver for the Via VT82C686A "SuperSouth" soundcard
 *  Copyright (c) by Jaroslav Kysela <perex@jcu.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.
 *
 */

#include "driver.h"
#include "sndpci.h"
#include "supersouth.h"

int snd_via686a_detect(snd_card_t * card, struct snd_pci_dev *pci,
				int pci_index, int device)
{
	if ( device == SND_CARD_TYPE_VIA82C686A 
	&&  !snd_pci_find_device(PCI_VENDOR_ID_VIA, 
				PCI_DEVICE_ID_VIA_82C686_5, 
				pci_index, pci)) {
		if( card ) card->type = SND_CARD_TYPE_VIA82C686A;
	} else {
		return -ENODEV;
	}

	return 0;
}

/* snd_supersouth_cmdline()
 * 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_supersouth_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_via686a_detect( NULL, pci, dev, SND_CARD_TYPE_VIA82C686A) == 0) {
                        if( verbose > 2 ) snd_printk("supersouth: Found Via VT82C686A @IO 0x%X\n",
						(unsigned int) PCI_IO_ADDR(pci->base_address[0]));
#ifdef __QNXNTO__
			if( pci_detach_device( pci->handle ) != PCI_SUCCESS )
				snd_printk("supersouth: Unable to detach PCI device.\n");
#endif
                        cards++;
//                        pci = NULL;
//                        if( cards > SND_CARDS)
//				break;
                } else
                        break;
        }
        snd_free( pci, sizeof(struct snd_pci_dev ));
        if (!cards) {
                if( verbose > 1 ) snd_printk("supersouth: Via VT82C686A 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-supersouth" );
		*count = idx + 1;
        }
        return 0;
}
