/* 
** 
** The module performs I/O functions.
**
*/

#include 	"../include/includes.h"
#include    <stdarg.h>        /* ANSI C header file */
#include    <syslog.h>
#include    "../include/iofunc.h"

/*
void
InitParport(char *prog_name,  u_short partport_range)
{
	if (iopl(3) ) {
		log_sys("%s: %s", prog_name, FUNCTION_MACRO);
	}
}
*/

void
InitParport(char *prog_name, u_short parport_range)
{
	if (ioperm(parport_range, 3, 1) ) {
		log_sys("%s: %s: ioperm", prog_name, FUNCTION_MACRO);
	}
}

void
SetParport(u_short parport_range, u_char flag)
{
	outb(flag, parport_range);
}

void
SetParportOn(char ledbit,u_short parport_range)
{
	BYTE inbyte,outbyte;
        BYTE ledstatus = 0x01;
        ledstatus >>= ledbit;
	inbyte = inb(parport_range);
	outbyte = inbyte | ledstatus;
	outb(outbyte, parport_range);
}

void
SetParportOff(char ledbit, u_short parport_range)
{
	BYTE  inbyte,outbyte;
	inbyte = inb(parport_range);
	outbyte = inbyte & 0xbf;
	outb(outbyte, parport_range);
}
	
void
SetNonblock(int id)
{
	fcntl(id, F_SETFL, O_NONBLOCK);
}


/*	+++++++++++++++++++++++++++++	*/
/*	Write parallel port data		*/
/*	+++++++++++++++++++++++++++++	*/
void SetParalPort(BYTE flag,BYTE data)
{	// Parallel port address: flag=0,address=0x37a B1;flag=1,address=0x37a B2
	// flag=2--9,address=0x378 B0--B7
    BYTE	ii,jj;

	if (flag > 9)
		return;
	if (data != 0 && data != 1)
		return;
    if (flag == 0 || flag == 1) // read or write 0x37a
    {
        ii = inb(0x37a);
/*
        jj = 1 << (flag + 1);
        ii = ii & (~jj);
        if (flag == 0)
            jj = (~data & 0x01) << (flag + 1);
        else
            jj = data << (flag + 1);
*/
		jj = 1<<flag;
		ii = ii & (~jj);
		jj = data<<flag;
        ii = ii | jj;
        outb(ii,0x37a);
    }
	else
	{
		ii = inb(0x378);
		jj = 1 << (flag - 2);
		ii = ii & (~jj);
		jj = data << (flag - 2);
		ii = ii | jj;
		outb(ii,0x378);
/*		
		ii = inb(0x378);
		jj = 1<<(flag-2);
	    if(data){
		  ii = ii | jj;
		}else{
		  ii = ii & (~jj);   
		}
		outb(ii,0x378);
*/
	}
}
