/*
 *  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"



int snd_solo_detect(snd_card_t * card, struct snd_pci_dev *pci, int pci_index)
{
	int idx;

	if (snd_pci_find_device(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ES1938, pci_index, pci) < 0)
		return -ENODEV;
	for (idx = 0; idx < 5; idx++) {
		if (pci->base_address[idx] == 0 ||
		    !PCI_IS_IO(pci->base_address[idx]))
			return -ENODEV;
	}

	return 0;
}


/* snd_esssolo_cmdline() -- Oct 25th, 1999
 * 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_esssolo_cmdline( int *count, char **cmdline, int verbose )
{
	int dev, cards;
	struct snd_pci_dev *pci;

	*count = 0;

	for (dev = cards = 0; dev < SND_CARDS; dev++) {
		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_solo_detect( NULL, pci, dev ) == 0) {
			if( verbose > 2 ) snd_printd("esssolo: Found ESS SOLO1 @IO 0x%X\n",
						(unsigned int) PCI_IO_ADDR(pci->base_address[0]));
			cards++;
#ifdef __QNXNTO__
			if( pci_detach_device( pci->handle ) != PCI_SUCCESS )
				snd_printk("esssolo: Unable to detach PCI device.\n");
#endif
		} else
			break;
	}
	if (!cards) {
		if( verbose > 1 ) snd_printk("esssolo: ESS Solo-1 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-esssolo" );
                *count = idx + 1;
        }
	return 0;
}
