/* 40 Watt FMBCB IPA control v1.9 */ #include #include #include #include #include #include #ifdef __FreeBSD__ #include #else #include /* GNU/Linux */ #endif #define BASE_ADDRESS 0x278 /* 0x378 or 0x278 or 0x3BC */ #define IPASTATUS "/usr/local/etc/.ipa_status" /* status file */ #define PASSWD "motor0lasux" /* Password */ FILE *fp; char in, buf[BUFSIZ], buf1[BUFSIZ]; extern int errno; int a, c, fd; /* pin 432 on the parallel port */ unsigned char data_0 = 0; /* Send 00000000b HIGH */ unsigned char data_1 = 1; /* Send 00000001b LOW */ unsigned char data_2 = 2; /* Send 00000010b MIDLOW */ unsigned char data_4 = 4; /* Send 00000100b MID */ unsigned char data_8 = 8; /* Send 00001000b MIDHIGH */ int read_input (void); int ipa_high (void); int ipa_low (void); int ipa_midlow (void); int ipa_mid (void); int ipa_midhigh (void); int contin (void); int Getpass (void); int porttest (void); 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); } while ((c = getopt (argc, argv, "BbmLl")) != EOF) { switch (c) { case 'B': ipa_high (); exit (0); case 'b': ipa_midhigh (); exit (0); case 'm': ipa_mid (); exit (0); case 'l': ipa_midlow (); exit (0); case 'L': ipa_low (); exit (0); default: fprintf (stderr, "\nWhatsup? Try one of these:\n\n"); fprintf (stderr, "\t-B\tSet IPA to HIGH (maximum output power)\n"); fprintf (stderr, "\t-b\tSet IPA to MIDHIGH\n"); fprintf (stderr, "\t-m\tSet IPA to MID (medium output power)\n"); fprintf (stderr, "\t-l\tSet IPA to MIDLOW\n"); fprintf (stderr, "\t-L\tSet IPA to LOW (lowest output power)\n\n"); exit (1); } } Getpass (); read_input (); return (0); } int read_input (void) { for (;;) { printf ("\33[H\33[J"); printf ("Options\n-------\n\n"); printf ("\t1 = Set IPA to HIGH (maximum output power)\n"); printf ("\t2 = Set IPA to MIDHIGH\n"); printf ("\t3 = Set IPA to MID (medium output power)\n"); printf ("\t4 = Set IPA to MIDLOW\n"); printf ("\t5 = Set IPA to LOW (lowest output power)\n"); printf ("\t6 = Quit\n"); printf ("\n\tCurrent system status: "); if ((fd = open (IPASTATUS, O_RDWR, 0)) < 0) printf ("status unavailable"); else { if ((fp = fopen (IPASTATUS, "r")) == NULL) { fprintf (stderr, "\nCould not open file: %s\n\n", IPASTATUS); exit (errno); } while ((in = fgetc (fp)) != EOF) putchar (in); fclose (fp); } printf ("\n\n--> "); fflush (stdout); fgets (buf, sizeof buf, stdin); switch (a = atoi (buf)) { case 1: ipa_high (); contin (); break; case 2: ipa_midhigh (); contin (); break; case 3: ipa_mid (); contin (); break; case 4: ipa_midlow (); contin (); break; case 5: ipa_low (); contin (); break; case 6: printf ("\nLeaving...\n\n"); exit (0); default: read_input (); break; } } } int ipa_high (void) { #ifdef VERBOSE printf ("\nSetting the IPA output power to HIGH\n\n"); fflush (stdout); #endif porttest (); #ifdef __FreeBSD__ outb (BASE_ADDRESS, data_0); #else outb (data_0, BASE_ADDRESS); ioperm (BASE_ADDRESS, 3, 0); #endif if ((fp = fopen (IPASTATUS, "w")) == NULL) printf ("Unable to update IPA status\n"); fputc ('1', fp); fclose (fp); syslog (LOG_NOTICE, "IPA output to HIGH"); return (0); } int ipa_midhigh (void) { #ifdef VERBOSE printf ("\nSetting the IPA output power to MIDHIGH\n\n"); fflush (stdout); #endif porttest (); #ifdef __FreeBSD__ outb (BASE_ADDRESS, data_8); #else outb (data_8, BASE_ADDRESS); ioperm (BASE_ADDRESS, 3, 0); #endif if ((fp = fopen (IPASTATUS, "w")) == NULL) printf ("Unable to update IPA status\n"); fputc ('2', fp); fclose (fp); syslog (LOG_NOTICE, "IPA output to MIDHIGH"); return (0); } int ipa_mid (void) { #ifdef VERBOSE printf ("\nSetting the IPA output power to MID\n\n"); fflush (stdout); #endif porttest (); #ifdef __FreeBSD__ outb (BASE_ADDRESS, data_4); #else outb (data_4, BASE_ADDRESS); ioperm (BASE_ADDRESS, 3, 0); #endif if ((fp = fopen (IPASTATUS, "w")) == NULL) printf ("Unable to update IPA status\n"); fputc ('3', fp); fclose (fp); syslog (LOG_NOTICE, "IPA output to MID"); return (0); } int ipa_midlow (void) { #ifdef VERBOSE printf ("\nSetting the IPA output power to MIDLOW\n\n"); fflush (stdout); #endif porttest (); #ifdef __FreeBSD__ outb (BASE_ADDRESS, data_2); #else outb (data_2, BASE_ADDRESS); ioperm (BASE_ADDRESS, 3, 0); #endif if ((fp = fopen (IPASTATUS, "w")) == NULL) printf ("Unable to update IPA status\n"); fputc ('4', fp); fclose (fp); syslog (LOG_NOTICE, "IPA output to MIDLOW"); return (0); } int ipa_low (void) { #ifdef VERBOSE printf ("\nSetting the IPA output power to LOW\n\n"); fflush (stdout); #endif porttest (); #ifdef __FreeBSD__ outb (BASE_ADDRESS, data_1); #else outb (data_1, BASE_ADDRESS); ioperm (BASE_ADDRESS, 3, 0); #endif if ((fp = fopen (IPASTATUS, "w")) == NULL) printf ("Unable to update IPA status\n"); fputc ('5', fp); fclose (fp); syslog (LOG_NOTICE, "IPA output to LOW"); return (0); } int contin (void) { printf ("\nContinue? [y|n] "); fflush (stdout); fgets (buf1, sizeof buf1, stdin); switch (buf1[0]) { case 'n': case 'N': printf ("\nLeaving...\n\n"); exit (0); case 'y': case 'Y': break; default: contin (); break; } return (0); } int Getpass () { char *ptr; char *getpass (const char *); printf ("\nIPA Control v1.9\n\n\n"); fflush (stdout); if ((ptr = getpass ("Enter password: ")) == NULL) { fprintf (stderr, "Can't get password\n"); exit (1); } if (strncmp (PASSWD, ptr, strlen (PASSWD)) == 0) { syslog (LOG_NOTICE, "Successful password received"); while (*ptr != 0) *ptr++ = 0; return (0); } else { printf ("\nIncorrect\n\n"); syslog (LOG_WARNING, "WARNING incorrect password given: %s", ptr); while (*ptr != 0) *ptr++ = 0; exit (1); } } int porttest (void) { #ifdef __FreeBSD__ if (open ("/dev/io", O_RDONLY) < 0) #else if (ioperm (BASE_ADDRESS, 3, 1) < 0) #endif { fprintf (stderr, "\nERROR: Can't open port 0x%x\n\n", BASE_ADDRESS); perror ("port"); exit (errno); } return (0); }