/*  Linux FRS Radio Control                                       */
/*  GBPPR							  */
/*                                                                */
/*  Compile using: gcc -O2 frs_control.c -o frs_control           */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <asm/io.h>

#define BASE_ADDRESS    0x378   /* 0x378 is 1st parallel port */
                                /* 0x278 is 2nd parallel port */

int c;
extern int errno;
                            /*       pin 432  on the parallel port */
unsigned char data_0 = 0;   /* Send 00000000b */
unsigned char data_1 = 1;   /* Send 00000001b */
unsigned char data_2 = 2;   /* Send 00000010b */
unsigned char data_4 = 4;   /* Send 00000100b */

void usage (char *);
int txtime, verbose;

int
main (int argc, char **argv)
{

  if (geteuid () && getuid ())
    {
      fprintf (stderr, "\n\n%s must be run as root to access hardware ports.\n", argv[0]);
      fprintf (stderr, "Leaving...\n\n");
      exit (1);
    }

  if (argc < 2)
    {
      usage (argv[0]);
    }

  while ((c = getopt (argc, argv, "t:rum")) != EOF)
    {
      switch (c)
	{
	  case 't':
	    txtime = atoi(optarg);
	    TX ();
	    exit (0);
	  case 'r':
	    RESET ();
	    exit (0);
	  case 'u':
	    UP ();
	    exit (0);
	  case 'm':
	    MODE ();
	    exit ();
	  default:
	    usage (argv[0]);
	}
    }

  return (0);
}

int
TX (void)
{

  porttest ();

  outb (data_1, BASE_ADDRESS);
  sleep (txtime);
  outb (data_0, BASE_ADDRESS);
  usleep (200000);
  ioperm (BASE_ADDRESS, 3, 0);

  return (0);
}

int
RESET (void)
{

  porttest ();

  outb (data_0, BASE_ADDRESS);
  printf ("Parallel Port Reset: 0x%x hex\t(%d decimal)\n", BASE_ADDRESS, BASE_ADDRESS);
  printf ("Sending '00000000' ...\n");
  usleep (200000);
  ioperm (BASE_ADDRESS, 3, 0);

  return (0);
}

int
UP (void)
{

  porttest ();

  outb (data_2, BASE_ADDRESS);
  usleep (500000);
  outb (data_0, BASE_ADDRESS);
  usleep (200000);
  ioperm (BASE_ADDRESS, 3, 0);
  
  return (0);
}

int
MODE (void)
{
  
  porttest ();

  outb (data_4, BASE_ADDRESS);
  usleep (500000);
  outb (data_0, BASE_ADDRESS);
  usleep (200000);
  ioperm (BASE_ADDRESS, 3, 0);

  return (0);
}

int
porttest (void)
{

  if (ioperm (BASE_ADDRESS, 3, 1) < 0)
    {
      fprintf (stderr, "\nERROR: Can not open port 0x%x\n\n", BASE_ADDRESS);
      perror ("port");
      exit (errno);
    }

  usleep (200000);

  return (0);
}

void
usage (char *pname)
{
  fprintf (stderr, "\n\nUsage: %s\n\n", pname);
  fprintf (stderr, "\t-t x\tEnable Transmit for 'x' seconds\n");
  fprintf (stderr, "\t-r\tParallel port reset\n");
  fprintf (stderr, "\t-u\tEnable Up Arrow\n");
  fprintf (stderr, "\t-m\tEnable Mode Function\n");
  exit (1);
}


syntax highlighted by Code2HTML, v. 0.9.1