/*
 * Copyright (c) 1998  Katsushi Kobayashi. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modifications, are permitted provided that the above copyright notice
 * and this paragraph are duplicated in all such forms and that any
 * documentation, advertising materials, and other materials related to
 * such distribution and use acknowledge that the software was developed
 * by Katsushi Kobayashi, Japan. The name of the developer may not be
 * used to endorse or promote products derived from this software without
 * specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS''
 * AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE.
 */

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <i386/include/lynx.h>
#include <sys/errno.h>

#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_fw.h>

#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <stdio.h>
#include <strings.h>

usage(char *progname)
{
	printf("Usage : %s options\n", progname);
        printf("<options>\n");
        printf("-s number : sync ring buffer for frame, 6 is best..\n");
        printf("-I ifname : use interface \"ifname\"\n");
}
main(argc, argv)
        int argc;
        char *argv[];
{
	char ch;
	struct fwintreq ifr;
	char dname[] = "/dev/lynx0";
	extern char *optarg;
	extern int optind;
	int d;
	int start = 1, copy = 0, sync = -1;

	while ((ch = getopt(argc, argv, "s:ceI:")) != -1){
		switch(ch) {
			case 's':
				sscanf(optarg, "%d", &sync);
		     		break;
			case 'I':
				strncpy(dname, optarg, IFNAMSIZ);
		     		break;
			case 'e':
				start = 0;
				break;
			case 'c':
				copy = 1;
				break;
			case '?':
			default:
				usage(argv[0]);
				break;
		}
	}
	argc -= optind;
	argv += optind;
	if((d = open(dname, O_RDWR)) < 0 ){
		perror("open");
		exit(1);
	}
	if( sync != -1){
		if ( sync = 0 ) sync = -1;
		ioctl(d, LYNX_DV_SYNC, &sync);
		exit(0);
	}
	if( copy ){
		int i_data, j_data, k_data=0;
		int s;
		u_long data[512/4 * 250];
		for( i_data = 0 ; i_data < 250 ; i_data ++){
			for( j_data = 0 ; j_data < 512/4 ; j_data ++){
				data[i_data*512/4 + j_data] = htonl(i_data << 16 | j_data);
			}
			data[i_data*512/4] = htonl(488 << 16);
		}
		write(d, data, 64*512);
		exit(0);
	}
	if( start ){
		dvstart(d);
	} else {
		dvstop(d);
	}
}

dvstop(int d)
{
	int i, s;
	int  lynx;

#ifdef LYNX_DV_TXSTOP
	if( ioctl(d, LYNX_DV_TXSTOP, &lynx) < 0) {
#else
	if( ioctl(d, LYNX_DV_STOP, &lynx) < 0) {
#endif
             err(1, "ioctl (END)");
        }
}
dvstart(int d)
{
	int i, s;
	int lynx;

#ifdef LYNX_DV_TXSTART
	if( ioctl(d, LYNX_DV_TXSTART, &lynx) < 0) {
#else
	if( ioctl(d, LYNX_DV_START, &lynx) < 0) {
#endif
             err(1, "ioctl (START)");
        }
}

