kstars
port.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef PORT_H
00031 #define PORT_H
00032
00033
00034
00035 #include <unistd.h>
00036
00037 #ifdef __linux__
00038 #if !defined(arm) && !defined(__hppa__) && !defined(__sparc__) && !defined(__ppc__) && !defined(__powerpc__) && !defined(__s390__) && !defined(__s390x__) && !defined(__mips__) && !defined(__mc68000__)
00039 #include <sys/io.h>
00040 #endif
00041 #elif defined(QNX)
00042 #include <conio.h>
00043 #elif defined(__FreeBSD__)
00044 #include <machine/cpufunc.h>
00045 #include <stdio.h>
00046 #elif defined(BSDI)
00047 #include <machine/inline.h>
00048 #elif defined(OPENBSD)
00049 #include <machine/pio.h>
00050 #elif defined(LYNX)
00051 #include "lynx-io.h"
00052 #elif defined(SOLARIS)
00053 #include "solaris-io.h"
00054 #else
00055 #error Please define a platform in the Makefile
00056 #endif
00057
00058 #if defined(arm) || defined(__hppa__) || defined(__sparc__) || defined(__ppc__) || defined(__powerpc__) || defined(__s390__) || defined(__s390x__) || defined(__mips__) || defined(__mc68000__)
00059 static char ports_temp;
00060
00061 #ifdef inb
00062 #undef inb
00063 #endif
00064 #define inb(port) \
00065 lseek(devport, port, SEEK_SET), \
00066 read(devport, &ports_temp, 1), \
00067 ports_temp
00068
00069 #ifdef outb
00070 #undef outb
00071 #endif
00072 #define outb(data, port) \
00073 lseek(devport, port, SEEK_SET); \
00074 ports_temp = data; \
00075 write(devport, &ports_temp, 1);
00076
00077 #endif
00078
00079 class port_t {
00080 public:
00081 port_t(int iport);
00082 ~port_t(void);
00083
00084 inline int read_data(void) { return inb(port); }
00085 inline int read_status(void) { return inb(port1); }
00086 inline int read_control(void) { return inb(port2); }
00087
00088 #if defined(LINUX) || defined(LYNX)
00089 inline void write_data(int data) { outb(data, port); }
00090 inline void write_control(int data) { outb(control_reg = data, port2); }
00091 inline void setbit_control(int data) { outb(control_reg |= data, port2); }
00092 inline void clearbit_control(int data) { outb(control_reg &= ~data, port2); }
00093 #else // Solaris, QNX, and *BSD use (port, data) instead
00094 inline void write_data(int data) { outb(port, data); }
00095 inline void write_control(int data) { outb(port2, control_reg = data); }
00096 inline void setbit_control(int data) { outb(port2, control_reg |= data); }
00097 inline void clearbit_control(int data) { outb(port2, control_reg &= ~data); }
00098 #endif
00099
00100 inline int get_port() { return port; }
00101 inline operator bool () const { return port != -1; }
00102
00103 private:
00104 int port;
00105 int port1;
00106 int port2;
00107 int control_reg;
00108 #ifdef LOCKING
00109 int lock_fd;
00110 int lock(int portnum);
00111 void unlock(int portnum);
00112 #endif
00113
00114 #ifdef FREEBSD
00115 FILE *devio;
00116 #endif
00117 #if defined(__linux__) && (defined(arm) || defined(__hppa__) || defined(__sparc__) || defined(__ppc__) || defined(__powerpc__) || defined(__s390__) || defined(__s390x__) || defined(__mips__) || defined(__mc68000__))
00118 int devport;
00119 #endif
00120 };
00121
00122 #endif