/*
################################################################################
# Open Source implimentation of the MDE8255LPT-1's controller's shared library
#
# Created by: Travis Sidelinger
# License: LGPL
# Version History:
#   2005Oct24 : fixed several bugs with the input functions
#   2005Oct20 : initial release
#
# Copyright:
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
################################################################################
*/
 
#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>
#include "mdedriverdll.h"
 
 
// This delay was added to limit possible errors by sending data to fast
#define DELAY 1000          // 1 ms delay
 
/* Global Variables */
static unsigned short intBasePort = 0;
 
/*
///////////////////////////////////////
//
// Dummy function that does nothing
//
///////////////////////////////////////
*/
int MDEOpenDriver(void)  { return(0); }
 
/*
///////////////////////////////////////
//
// Open access to the IO port
//
///////////////////////////////////////
*/
int MDEOpenLPTPort(int baseaddress)
{
    intBasePort = (unsigned short)(baseaddress);
    int ret = 0;
 
    ret = (int)getuid();
    if(ret != 0)
    {
        if(ret > 0) { perror("getuid"); }
        else { printf("You must be root to open I/O ports."); }
        return(ret);
        }
 
    ret = ioperm(intBasePort, 3, 1);
    if(ret)
    {
        perror("ioperm");
        return(ret);
        }
 
    return(ret);
    }
 
/*
///////////////////////////////////////
//
// Close access to the IO port
//
///////////////////////////////////////
*/
int MDECloseDriver(void)
{
    int ret = 0;
 
    ret = ioperm(intBasePort, 3, 0);
    if(ret)
    {
        perror("ioperm");
        return(ret);
        }
 
    return(ret);
    }
 
/*
///////////////////////////////////////
//
// Configure the 8255 device
//
///////////////////////////////////////
*/
int MDEConfigPort(int baseaddress, int value)
{
    // Variables //
    int ret = 0;
    intBasePort = (unsigned short)(baseaddress);
 
    // check the input
    if(! intBasePort ) { return(-1); }
 
    /* Initialize the device */
    outb(value, intBasePort);
    outb(0x0F,  intBasePort+2);
    outb(0x07,  intBasePort+2);
    outb(0x0F,  intBasePort+2);
 
    /* Sleep for a while (100 ms) */
    usleep(DELAY);
 
    return(ret);
    }
 
/*
///////////////////////////////////////
//
// Output functions
//
///////////////////////////////////////
*/
int MDEOutPA(int baseaddress, int value)
{
    // Variables //
    static unsigned int ret = 0;
    intBasePort = (unsigned short)(baseaddress);
 
    // check the input
    if(! intBasePort ) { return(-1); }
 
    /* Initialize the device */
    outb(value, intBasePort);   usleep(DELAY);
    outb(0x0C,  intBasePort+2); usleep(DELAY);
    outb(0x04,  intBasePort+2); usleep(DELAY);
    outb(0x0C,  intBasePort+2); usleep(DELAY);
 
    return(ret);
    }
 
int MDEOutPB(int baseaddress, int value)
{
    // Variables //
    static unsigned int ret = 0;
    intBasePort = (unsigned short)(baseaddress);
 
    // check the input
    if(! intBasePort ) { return(-1); }
 
    /* Initialize the device */
    outb(value, intBasePort);   usleep(DELAY);
    outb(0x0E,  intBasePort+2); usleep(DELAY);
    outb(0x06,  intBasePort+2); usleep(DELAY);
    outb(0x0E,  intBasePort+2); usleep(DELAY);
 
    return(ret);
    }
 
int MDEOutPC(int baseaddress, int value)
{
    // Variables //
    static unsigned int ret = 0;
    intBasePort = (unsigned short)(baseaddress);
 
    // check the input
    if(! intBasePort ) { return(-1); }
 
    /* Initialize the device */
    outb(value, intBasePort);   usleep(10000);
    outb(0x0D,  intBasePort+2); usleep(DELAY);
    outb(0x05,  intBasePort+2); usleep(DELAY);
    outb(0x0D,  intBasePort+2); usleep(DELAY);
 
    return(ret);
    }
 
/*
///////////////////////////////////////
//
// Input functions
//
///////////////////////////////////////
*/
int MDEInPA(int baseaddress)
{
    // Variables //
    static unsigned char ret = 0;
    intBasePort = (unsigned short)(baseaddress);
 
    // check the input
    if(! intBasePort ) { return(-1); }
 
    /* Get the device data */
    outb(0x0E, intBasePort+2); usleep(DELAY);
    outb(0x28, intBasePort+2); usleep(DELAY);
    ret = inb(intBasePort);    usleep(DELAY);
    outb(0x2E, intBasePort+2); usleep(DELAY);
    outb(0x0E, intBasePort+2); usleep(DELAY);
 
    return((int)(ret));
    }
 
int MDEInPB(int baseaddress)
{
    // Variables //
    static unsigned char ret = 0;
    intBasePort = (unsigned short)(baseaddress);
 
    // check the input
    if(! intBasePort ) { return(-1); }
 
    /* Get the device data */
    outb(0x0E, intBasePort+2); usleep(DELAY);
    outb(0xA2, intBasePort+2); usleep(DELAY);
    ret = inb(intBasePort);    usleep(DELAY);
    outb(0x2E, intBasePort+2); usleep(DELAY);
    outb(0x0E, intBasePort+2); usleep(DELAY);
 
    return((int)(ret));
    }
 
int MDEInPC(int baseaddress)
{
    // Variables //
    static unsigned char ret = 0;
    intBasePort = (unsigned short)(baseaddress);
 
    // check the input
    if(! intBasePort ) { return(-1); }
 
    /* Get the device data */
    outb(0x0D, intBasePort+2); usleep(DELAY);
    outb(0x29, intBasePort+2); usleep(DELAY);
    ret = inb(intBasePort);    usleep(DELAY);
    outb(0x2D, intBasePort+2); usleep(DELAY);
    outb(0x0D, intBasePort+2); usleep(DELAY);
 
    return((int)(ret));
    }
 
/var/www/sites/dokuwiki/data/pages/projects/libmdedriver/mdedriverdll.c.txt · Last modified: 2009/04/11 22:23 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki