/* Copyright 1999 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * 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 "isapnp.h"

/* I am in hell. ISAPnP is the fifth circle. */

/* Default to new-style access. */
static int access_type = 1;
/* Needed for old access */
static int readport = 0;


static void isapnpFreeDevice(struct isapnpDevice *dev) {
	freeDevice((struct device *)dev);
	/* needs finished */
}

static void isapnpWriteDevice(FILE *file, struct isapnpDevice *dev) {
	writeDevice(file, (struct device *)dev);
	/* needs finished */
}

static int isapnpCompareDevice(struct isapnpDevice *dev1, struct isapnpDevice *dev2)
{
	int x=compareDevice((struct device *)dev1,(struct device *)dev2);
	if (x) return x;
	return devCmp( (void *)dev1, (void *)dev2 );
	/* needs finished */
}
			    
	

struct isapnpDevice * isapnpNewDevice(struct isapnpDevice *dev) {
	struct isapnpDevice *ret;
    
	ret = malloc(sizeof(struct isapnpDevice));
	memset(ret,'\0',sizeof(struct isapnpDevice));
	ret=(struct isapnpDevice *)newDevice((struct device *)dev,(struct device *)ret);
	ret->bus = BUS_ISAPNP;
	if (dev && dev->bus == BUS_isapnp) {
		ret->vendorId = dev->vendorId;
		ret->deviceId = dev->deviceId;

	}
	ret->newDevice = isapnpNewDevice;
	ret->freeDevice = isapnpFreeDevice;
	ret->writeDevice = isapnpWriteDevice;
	ret->compareDevice = isapnpCompareDevice;
	return ret;
}

struct device * isapnpProbeKernel(enum deviceClass probeClass, int probeFlags, struct device *devlist, int fd) {
	char *pnpbuf;
	char buf[2048];
	int x, len=0;
	
	while ( (x=read(fd,buf,2048))==2048 ) {
		pnpbuf=realloc(pnpbuf,len+2048);
		strncpy(pnpbuf+len,buf,2048);
		len+=2048
	}
	if (x && x!=-1) {
		pnpbuf=realloc(pnpbuf,len+x);
		strncpy(pnpbuf+len,buf,x);
		len+=x;
		pnpbuf[len]='\0'
	}
	if (!pnpbuf) return devlist;
	
	return devlist;
}

struct device * isapnpProbeOld(enum deviceClass probeClass, int probeFlags, struct device *devlist) {
	return devlist;
}


struct device * isapnpProbe(enum deviceClass probeClass, int probeFlags, struct device *devlist) {
	int fd;
	
	if (
	    (probeClass == CLASS_UNSPEC) ||
	    (probeClass == CLASS_OTHER) ||
	    (probeClass == CLASS_NETWORK) ||
	    (probeClass == CLASS_MODEM) ||
	    (probeClass == CLASS_AUDIO)
	    ) {
		fd = open("/proc/isapnp",O_RDONLY);
		if (fd!=-1) {
			devlist = isapnpProbeKernel(probeClass, probeFlags, devlist, fd);
		} else {
			if (probeFlags & PROBE_SAFE) return devlist;
			access_type = 0;
			devlist = isapnpProbeOld(probeClass, probeFlags, devlist);
		}
	}
	return devlist;
}

