/* setup_plip.c: Diagnostic program for parallel ports. */
/*
    Written 1993 by Donald Becker.

    Copyright 1993 United States Government as represented by the
    Director, National Security Agency.  This software may be used and
    distributed according to the terms of the GNU Public License,
    incorporated herein by reference.

    The Author may be reached as becker@super.org or
    C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
*/

static char *version =
    "setup_plip.c:v0.00 8/11/93 Donald Becker (becker@super.org)\n";

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <asm/io.h>
#include <getopt.h>


#define PAR_DATA	0
#define PAR_STATUS	1
#define PAR_CONTROL	2
#define PLIP_MTU 1600
#define PLIP_HEADER_TYPE1 0xfd
#define PLIP_HEADER_TYPE2 0xfc

struct option longopts[] = {
 /* { name,  has_arg,  *flag,  val, }, */
    {"base-address", 1, 0, 'p'},
    {"help",       0, 0, 'h'},	/* Give help */
    {"interface",  0, 0, 'f'},	/* Interface cable type. */
    {"irq",	   1, 0, 'i'},	/* Interrupt number */
    {"verbose",    0, 0, 'v'},	/* Verbose mode */
    {"version",    0, 0, 'V'},	/* Display version number */
    { 0, 0, 0, 0 }
};


/*  Probe and test parallel ports. */

int
main(int argc, char *argv[])
{
    int port_base = 0x378, irq = -1;
    int errflag = 0, verbose = 0, interface = -1;
    int c;
    extern char *optarg;

    while ((c = getopt(argc, argv, "f:i:p:svw")) != -1)
	switch (c) {
	case 'f':
	    interface = atoi(optarg);
	    break;
	case 'i':
	    irq = atoi(optarg);
	    break;
	case 'p':
	    port_base = strtol(optarg, NULL, 16);
	    break;
	case 'v': verbose++;	 break;
	case '?':
	    errflag++;
	}
    if (errflag) {
	fprintf(stderr, "usage:");
	return 2;
    }

    if (verbose)
	printf(version);
    if (ioperm(port_base, 3, 1)) {
	perror("io-perm");
	return 1;
    }
    
    printf("Checking the parallel port at %#3x.\n", port_base);

    /* First clear the interruptss, and put in "loopback" mode. */
    outb(0x00, port_base + PAR_CONTROL);
    /* Put out some data. */
    outb(0x55, port_base + PAR_DATA);
    printf("  Output 0x55, input %#2.2x.\n", inb(port_base + PAR_DATA));
    return 0;
}


/*
 * Local variables:
 *  compile-command: "gcc -Wall -O6 -o setup_plip setup_plip.c"
 * End:
 */
