/*
 * Xbox Video Detector
 * Copyright (C) David "dmp" Pye (dmp@davidmpye.dyndns.org)
 * Copyright (C) Thomas "ShALLaX" Pedley (gentoox@shallax.com) - Maintainer
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#include <fcntl.h>
#include "i2c-dev.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "xbox.h"

int fd;
char *fname;
char *devfsname = "/dev/i2c/0";
char *nondevfsname = "/dev/i2c-0";

void checkPIC() {
	if ((fd = open(devfsname, O_RDWR)) < 0) {
		if ((fd=open(nondevfsname,O_RDWR))<0) {
			fprintf(stderr, "Error - failed to open EITHER /dev/i2c/0 or /dev/i2c-0\n    - you must be root and have the correct i2c modules loaded.\n");
			exit(1);
		} else {
			fname = malloc (strlen(nondevfsname));
			strncpy (fname, nondevfsname, strlen(nondevfsname));
		}
	} else {
		fname = malloc (strlen(devfsname));
		strncpy (fname, devfsname, strlen(devfsname));
	}
}

int main(int argc, char *argv[]) {
	unsigned char byte;
	
	checkPIC();
	fd = open(fname, O_RDWR);

	if (ioctl(fd,I2C_SLAVE_FORCE,CONEXANT_ADDRESS) < 0) {
		fprintf(stderr,"Error: Could not set address to %d: %s\n",CONEXANT_ADDRESS,strerror(errno));
		return 2;
	}

	byte=i2c_smbus_read_byte_data(fd,0x00);

	if (byte != 255) {
		printf("Conexant\n");
		return 0;
	}

	/* Not Conexant */
	if (ioctl(fd,I2C_SLAVE_FORCE,FOCUS_ADDRESS) < 0) {
		fprintf(stderr,"Error: Could not set address to %d: %s\n",FOCUS_ADDRESS,strerror(errno));
		return 2;
	}

	byte = i2c_smbus_read_byte_data(fd,0x00);
	if (byte != 255) {
		printf("Focus\n");
		return 0;
	}

	/* Not Focus */
	if (ioctl(fd,I2C_SLAVE_FORCE,XCALIBUR_ADDRESS) < 0) {
		fprintf(stderr,"Error: Could not set address to %d: %s\n",XCALIBUR_ADDRESS,strerror(errno));
		return 2;
	}

	byte = i2c_smbus_read_byte_data(fd,0x00);
	if (byte != 255) {
		printf("Xcalibur\n");
		return 0;
	}

	printf("Unknown\n");

	close(fd);

	return 0;
}
